github.com/gocuntian/go@v0.0.0-20160610041250-fee02d270bf8/src/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 "os/exec" 13 "reflect" 14 "testing" 15 ) 16 17 func TestForeachHeaderElement(t *testing.T) { 18 tests := []struct { 19 in string 20 want []string 21 }{ 22 {"Foo", []string{"Foo"}}, 23 {" Foo", []string{"Foo"}}, 24 {"Foo ", []string{"Foo"}}, 25 {" Foo ", []string{"Foo"}}, 26 27 {"foo", []string{"foo"}}, 28 {"anY-cAsE", []string{"anY-cAsE"}}, 29 30 {"", nil}, 31 {",,,, , ,, ,,, ,", nil}, 32 33 {" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}}, 34 } 35 for _, tt := range tests { 36 var got []string 37 foreachHeaderElement(tt.in, func(v string) { 38 got = append(got, v) 39 }) 40 if !reflect.DeepEqual(got, tt.want) { 41 t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want) 42 } 43 } 44 } 45 46 func TestCleanHost(t *testing.T) { 47 tests := []struct { 48 in, want string 49 }{ 50 {"www.google.com", "www.google.com"}, 51 {"www.google.com foo", "www.google.com"}, 52 {"www.google.com/foo", "www.google.com"}, 53 {" first character is a space", ""}, 54 } 55 for _, tt := range tests { 56 got := cleanHost(tt.in) 57 if tt.want != got { 58 t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want) 59 } 60 } 61 } 62 63 // Test that cmd/go doesn't link in the HTTP server. 64 // 65 // This catches accidental dependencies between the HTTP transport and 66 // server code. 67 func TestCmdGoNoHTTPServer(t *testing.T) { 68 goBin := testenv.GoToolPath(t) 69 out, err := exec.Command("go", "tool", "nm", goBin).CombinedOutput() 70 if err != nil { 71 t.Fatalf("go tool nm: %v: %s", err, out) 72 } 73 wantSym := map[string]bool{ 74 // Verify these exist: (sanity checking this test) 75 "net/http.(*Client).Get": true, 76 "net/http.(*Transport).RoundTrip": true, 77 78 // Verify these don't exist: 79 "net/http.http2Server": false, 80 "net/http.(*Server).Serve": false, 81 "net/http.(*ServeMux).ServeHTTP": false, 82 "net/http.DefaultServeMux": false, 83 } 84 for sym, want := range wantSym { 85 got := bytes.Contains(out, []byte(sym)) 86 if !want && got { 87 t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym) 88 } 89 if want && !got { 90 t.Errorf("expected to find symbol %q in cmd/go; not found", sym) 91 } 92 } 93 }