1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Tests of internal functions and things with no better homes.
20 shutdownPollInterval = 5 * time.Millisecond
23 func TestForeachHeaderElement(t *testing.T) {
28 {"Foo", []string{"Foo"}},
29 {" Foo", []string{"Foo"}},
30 {"Foo ", []string{"Foo"}},
31 {" Foo ", []string{"Foo"}},
33 {"foo", []string{"foo"}},
34 {"anY-cAsE", []string{"anY-cAsE"}},
37 {",,,, , ,, ,,, ,", nil},
39 {" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}},
41 for _, tt := range tests {
43 foreachHeaderElement(tt.in, func(v string) {
46 if !reflect.DeepEqual(got, tt.want) {
47 t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want)
52 func TestCleanHost(t *testing.T) {
56 {"www.google.com", "www.google.com"},
57 {"www.google.com foo", "www.google.com"},
58 {"www.google.com/foo", "www.google.com"},
59 {" first character is a space", ""},
60 {"[1::6]:8080", "[1::6]:8080"},
63 {"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"},
64 {"bücher.de", "xn--bcher-kva.de"},
65 {"bücher.de:8080", "xn--bcher-kva.de:8080"},
66 // Verify we convert to lowercase before punycode:
67 {"BÜCHER.de", "xn--bcher-kva.de"},
68 {"BÜCHER.de:8080", "xn--bcher-kva.de:8080"},
69 // Verify we normalize to NFC before punycode:
70 {"gophér.nfc", "xn--gophr-esa.nfc"}, // NFC input; no work needed
71 {"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input
73 for _, tt := range tests {
74 got := cleanHost(tt.in)
76 t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
81 // Test that cmd/go doesn't link in the HTTP server.
83 // This catches accidental dependencies between the HTTP transport and
85 func TestCmdGoNoHTTPServer(t *testing.T) {
87 goBin := testenv.GoToolPath(t)
88 out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
90 t.Fatalf("go tool nm: %v: %s", err, out)
92 wantSym := map[string]bool{
93 // Verify these exist: (sanity checking this test)
94 "net/http.(*Client).Get": true,
95 "net/http.(*Transport).RoundTrip": true,
97 // Verify these don't exist:
98 "net/http.http2Server": false,
99 "net/http.(*Server).Serve": false,
100 "net/http.(*ServeMux).ServeHTTP": false,
101 "net/http.DefaultServeMux": false,
103 for sym, want := range wantSym {
104 got := bytes.Contains(out, []byte(sym))
106 t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym)
109 t.Errorf("expected to find symbol %q in cmd/go; not found", sym)
114 // Tests that the nethttpomithttp2 build tag doesn't rot too much,
115 // even if there's not a regular builder on it.
116 func TestOmitHTTP2(t *testing.T) {
118 t.Skip("skipping in short mode")
121 goTool := testenv.GoToolPath(t)
122 out, err := exec.Command(goTool, "test", "-short", "-tags=nethttpomithttp2", "net/http").CombinedOutput()
124 t.Fatalf("go test -short failed: %v, %s", err, out)
128 // Tests that the nethttpomithttp2 build tag at least type checks
130 // The TestOmitHTTP2 test above actually runs tests (in long mode).
131 func TestOmitHTTP2Vet(t *testing.T) {
133 goTool := testenv.GoToolPath(t)
134 out, err := exec.Command(goTool, "vet", "-tags=nethttpomithttp2", "net/http").CombinedOutput()
136 t.Fatalf("go vet failed: %v, %s", err, out)
142 func BenchmarkCopyValues(b *testing.B) {
145 "a": {"1", "2", "3", "4", "5"},
146 "b": {"2", "2", "3", "4", "5"},
147 "c": {"3", "2", "3", "4", "5"},
148 "d": {"4", "2", "3", "4", "5"},
149 "e": {"1", "1", "2", "3", "4", "5", "6", "7", "abcdef", "l", "a", "b", "c", "d", "z"},
153 for i := 0; i < b.N; i++ {
154 dst := url.Values{"a": {"b"}, "b": {"2"}, "c": {"3"}, "d": {"4"}, "j": nil, "m": {"x"}}
156 if valuesCount = len(dst["a"]); valuesCount != 6 {
157 b.Fatalf(`%d items in dst["a"] but expected 6`, valuesCount)
160 if valuesCount == 0 {
161 b.Fatal("Benchmark wasn't run")