github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/net/http/http_test.go (about) 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. 4 5 // Tests of internal functions and things with no better homes. 6 7 package http 8 9 import ( 10 "bytes" 11 "internal/testenv" 12 "net/url" 13 "os/exec" 14 "reflect" 15 "testing" 16 ) 17 18 func TestForeachHeaderElement(t *testing.T) { 19 tests := []struct { 20 in string 21 want []string 22 }{ 23 {"Foo", []string{"Foo"}}, 24 {" Foo", []string{"Foo"}}, 25 {"Foo ", []string{"Foo"}}, 26 {" Foo ", []string{"Foo"}}, 27 28 {"foo", []string{"foo"}}, 29 {"anY-cAsE", []string{"anY-cAsE"}}, 30 31 {"", nil}, 32 {",,,, , ,, ,,, ,", nil}, 33 34 {" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}}, 35 } 36 for _, tt := range tests { 37 var got []string 38 foreachHeaderElement(tt.in, func(v string) { 39 got = append(got, v) 40 }) 41 if !reflect.DeepEqual(got, tt.want) { 42 t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want) 43 } 44 } 45 } 46 47 func TestCleanHost(t *testing.T) { 48 tests := []struct { 49 in, want string 50 }{ 51 {"www.google.com", "www.google.com"}, 52 {"www.google.com foo", "www.google.com"}, 53 {"www.google.com/foo", "www.google.com"}, 54 {" first character is a space", ""}, 55 {"[1::6]:8080", "[1::6]:8080"}, 56 57 // Punycode: 58 {"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"}, 59 {"bücher.de", "xn--bcher-kva.de"}, 60 {"bücher.de:8080", "xn--bcher-kva.de:8080"}, 61 // Verify we convert to lowercase before punycode: 62 {"BÜCHER.de", "xn--bcher-kva.de"}, 63 {"BÜCHER.de:8080", "xn--bcher-kva.de:8080"}, 64 // Verify we normalize to NFC before punycode: 65 {"gophér.nfc", "xn--gophr-esa.nfc"}, // NFC input; no work needed 66 {"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input 67 } 68 for _, tt := range tests { 69 got := cleanHost(tt.in) 70 if tt.want != got { 71 t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want) 72 } 73 } 74 } 75 76 // Test that cmd/go doesn't link in the HTTP server. 77 // 78 // This catches accidental dependencies between the HTTP transport and 79 // server code. 80 func TestCmdGoNoHTTPServer(t *testing.T) { 81 t.Parallel() 82 goBin := testenv.GoToolPath(t) 83 out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput() 84 if err != nil { 85 t.Fatalf("go tool nm: %v: %s", err, out) 86 } 87 wantSym := map[string]bool{ 88 // Verify these exist: (sanity checking this test) 89 "net/http.(*Client).do": true, 90 "net/http.(*Transport).RoundTrip": true, 91 92 // Verify these don't exist: 93 "net/http.http2Server": false, 94 "net/http.(*Server).Serve": false, 95 "net/http.(*ServeMux).ServeHTTP": false, 96 "net/http.DefaultServeMux": false, 97 } 98 for sym, want := range wantSym { 99 got := bytes.Contains(out, []byte(sym)) 100 if !want && got { 101 t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym) 102 } 103 if want && !got { 104 t.Errorf("expected to find symbol %q in cmd/go; not found", sym) 105 } 106 } 107 } 108 109 // Tests that the nethttpomithttp2 build tag doesn't rot too much, 110 // even if there's not a regular builder on it. 111 func TestOmitHTTP2(t *testing.T) { 112 if testing.Short() { 113 t.Skip("skipping in short mode") 114 } 115 t.Parallel() 116 goTool := testenv.GoToolPath(t) 117 out, err := exec.Command(goTool, "test", "-short", "-tags=nethttpomithttp2", "net/http").CombinedOutput() 118 if err != nil { 119 t.Fatalf("go test -short failed: %v, %s", err, out) 120 } 121 } 122 123 // Tests that the nethttpomithttp2 build tag at least type checks 124 // in short mode. 125 // The TestOmitHTTP2 test above actually runs tests (in long mode). 126 func TestOmitHTTP2Vet(t *testing.T) { 127 t.Parallel() 128 goTool := testenv.GoToolPath(t) 129 out, err := exec.Command(goTool, "vet", "-tags=nethttpomithttp2", "net/http").CombinedOutput() 130 if err != nil { 131 t.Fatalf("go vet failed: %v, %s", err, out) 132 } 133 } 134 135 var valuesCount int 136 137 func BenchmarkCopyValues(b *testing.B) { 138 b.ReportAllocs() 139 src := url.Values{ 140 "a": {"1", "2", "3", "4", "5"}, 141 "b": {"2", "2", "3", "4", "5"}, 142 "c": {"3", "2", "3", "4", "5"}, 143 "d": {"4", "2", "3", "4", "5"}, 144 "e": {"1", "1", "2", "3", "4", "5", "6", "7", "abcdef", "l", "a", "b", "c", "d", "z"}, 145 "j": {"1", "2"}, 146 "m": nil, 147 } 148 for i := 0; i < b.N; i++ { 149 dst := url.Values{"a": {"b"}, "b": {"2"}, "c": {"3"}, "d": {"4"}, "j": nil, "m": {"x"}} 150 copyValues(dst, src) 151 if valuesCount = len(dst["a"]); valuesCount != 6 { 152 b.Fatalf(`%d items in dst["a"] but expected 6`, valuesCount) 153 } 154 } 155 if valuesCount == 0 { 156 b.Fatal("Benchmark wasn't run") 157 } 158 }