[mirror] Scalable static site server for Git forges (like GitHub Pages)
at main 1.8 kB view raw
1package git_pages 2 3import ( 4 "net/http" 5 "strings" 6 "testing" 7) 8 9func checkHost(t *testing.T, host string, expectOk string, expectErr string) { 10 host, err := GetHost(&http.Request{Host: host}) 11 if expectErr != "" { 12 if err == nil || !strings.HasPrefix(err.Error(), expectErr) { 13 t.Errorf("%s: expect err %s, got err %s", host, expectErr, err) 14 } 15 } 16 if expectOk != "" { 17 if err != nil { 18 t.Errorf("%s: expect ok %s, got err %s", host, expectOk, err) 19 } else if host != expectOk { 20 t.Errorf("%s: expect ok %s, got ok %s", host, expectOk, host) 21 } 22 } 23} 24 25func TestHelloName(t *testing.T) { 26 config = &Config{Features: []string{}} 27 28 checkHost(t, "foo.bar", "foo.bar", "") 29 checkHost(t, "foo-baz.bar", "foo-baz.bar", "") 30 checkHost(t, "foo--baz.bar", "foo--baz.bar", "") 31 checkHost(t, "foo.bar.", "foo.bar", "") 32 checkHost(t, ".foo.bar", "", "reserved host name") 33 checkHost(t, "..foo.bar", "", "reserved host name") 34 35 checkHost(t, "ß.bar", "xn--zca.bar", "") 36 checkHost(t, "xn--zca.bar", "xn--zca.bar", "") 37 38 checkHost(t, "foo-.bar", "", "malformed host name") 39 checkHost(t, "-foo.bar", "", "malformed host name") 40 checkHost(t, "foo_.bar", "", "malformed host name") 41 checkHost(t, "_foo.bar", "", "malformed host name") 42 checkHost(t, "foo_baz.bar", "", "malformed host name") 43 checkHost(t, "foo__baz.bar", "", "malformed host name") 44 checkHost(t, "*.foo.bar", "", "malformed host name") 45 46 config = &Config{Features: []string{"relaxed-idna"}} 47 48 checkHost(t, "foo-.bar", "", "malformed host name") 49 checkHost(t, "-foo.bar", "", "malformed host name") 50 checkHost(t, "foo_.bar", "foo_.bar", "") 51 checkHost(t, "_foo.bar", "", "reserved host name") 52 checkHost(t, "foo_baz.bar", "foo_baz.bar", "") 53 checkHost(t, "foo__baz.bar", "foo__baz.bar", "") 54 checkHost(t, "*.foo.bar", "", "malformed host name") 55}