github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/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 "time" 17 ) 18 19 func init() { 20 shutdownPollInterval = 5 * time.Millisecond 21 } 22 23 func TestForeachHeaderElement(t *testing.T) { 24 tests := []struct { 25 in string 26 want []string 27 }{ 28 {"Foo", []string{"Foo"}}, 29 {" Foo", []string{"Foo"}}, 30 {"Foo ", []string{"Foo"}}, 31 {" Foo ", []string{"Foo"}}, 32 33 {"foo", []string{"foo"}}, 34 {"anY-cAsE", []string{"anY-cAsE"}}, 35 36 {"", nil}, 37 {",,,, , ,, ,,, ,", nil}, 38 39 {" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}}, 40 } 41 for _, tt := range tests { 42 var got []string 43 foreachHeaderElement(tt.in, func(v string) { 44 got = append(got, v) 45 }) 46 if !reflect.DeepEqual(got, tt.want) { 47 t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want) 48 } 49 } 50 } 51 52 func TestCleanHost(t *testing.T) { 53 tests := []struct { 54 in, want string 55 }{ 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"}, 61 62 // Punycode: 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 72 } 73 for _, tt := range tests { 74 got := cleanHost(tt.in) 75 if tt.want != got { 76 t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want) 77 } 78 } 79 } 80 81 // Test that cmd/go doesn't link in the HTTP server. 82 // 83 // This catches accidental dependencies between the HTTP transport and 84 // server code. 85 func TestCmdGoNoHTTPServer(t *testing.T) { 86 t.Parallel() 87 goBin := testenv.GoToolPath(t) 88 out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput() 89 if err != nil { 90 t.Fatalf("go tool nm: %v: %s", err, out) 91 } 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, 96 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, 102 } 103 for sym, want := range wantSym { 104 got := bytes.Contains(out, []byte(sym)) 105 if !want && got { 106 t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym) 107 } 108 if want && !got { 109 t.Errorf("expected to find symbol %q in cmd/go; not found", sym) 110 } 111 } 112 } 113 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) { 117 if testing.Short() { 118 t.Skip("skipping in short mode") 119 } 120 t.Parallel() 121 goTool := testenv.GoToolPath(t) 122 out, err := exec.Command(goTool, "test", "-short", "-tags=nethttpomithttp2", "net/http").CombinedOutput() 123 if err != nil { 124 t.Fatalf("go test -short failed: %v, %s", err, out) 125 } 126 } 127 128 // Tests that the nethttpomithttp2 build tag at least type checks 129 // in short mode. 130 // The TestOmitHTTP2 test above actually runs tests (in long mode). 131 func TestOmitHTTP2Vet(t *testing.T) { 132 t.Parallel() 133 goTool := testenv.GoToolPath(t) 134 out, err := exec.Command(goTool, "vet", "-tags=nethttpomithttp2", "net/http").CombinedOutput() 135 if err != nil { 136 t.Fatalf("go vet failed: %v, %s", err, out) 137 } 138 } 139 140 var valuesCount int 141 142 func BenchmarkCopyValues(b *testing.B) { 143 b.ReportAllocs() 144 src := url.Values{ 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"}, 150 "j": {"1", "2"}, 151 "m": nil, 152 } 153 for i := 0; i < b.N; i++ { 154 dst := url.Values{"a": {"b"}, "b": {"2"}, "c": {"3"}, "d": {"4"}, "j": nil, "m": {"x"}} 155 copyValues(dst, src) 156 if valuesCount = len(dst["a"]); valuesCount != 6 { 157 b.Fatalf(`%d items in dst["a"] but expected 6`, valuesCount) 158 } 159 } 160 if valuesCount == 0 { 161 b.Fatal("Benchmark wasn't run") 162 } 163 }