github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/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  		{"[1::6]:8080", "[1::6]:8080"},
    55  
    56  		// Punycode:
    57  		{"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"},
    58  		{"bücher.de", "xn--bcher-kva.de"},
    59  		{"bücher.de:8080", "xn--bcher-kva.de:8080"},
    60  		// Verify we convert to lowercase before punycode:
    61  		{"BÜCHER.de", "xn--bcher-kva.de"},
    62  		{"BÜCHER.de:8080", "xn--bcher-kva.de:8080"},
    63  		// Verify we normalize to NFC before punycode:
    64  		{"gophér.nfc", "xn--gophr-esa.nfc"},            // NFC input; no work needed
    65  		{"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input
    66  	}
    67  	for _, tt := range tests {
    68  		got := cleanHost(tt.in)
    69  		if tt.want != got {
    70  			t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
    71  		}
    72  	}
    73  }
    74  
    75  // Test that cmd/go doesn't link in the HTTP server.
    76  //
    77  // This catches accidental dependencies between the HTTP transport and
    78  // server code.
    79  func TestCmdGoNoHTTPServer(t *testing.T) {
    80  	goBin := testenv.GoToolPath(t)
    81  	out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
    82  	if err != nil {
    83  		t.Fatalf("go tool nm: %v: %s", err, out)
    84  	}
    85  	wantSym := map[string]bool{
    86  		// Verify these exist: (sanity checking this test)
    87  		"net/http.(*Client).Get":          true,
    88  		"net/http.(*Transport).RoundTrip": true,
    89  
    90  		// Verify these don't exist:
    91  		"net/http.http2Server":           false,
    92  		"net/http.(*Server).Serve":       false,
    93  		"net/http.(*ServeMux).ServeHTTP": false,
    94  		"net/http.DefaultServeMux":       false,
    95  	}
    96  	for sym, want := range wantSym {
    97  		got := bytes.Contains(out, []byte(sym))
    98  		if !want && got {
    99  			t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym)
   100  		}
   101  		if want && !got {
   102  			t.Errorf("expected to find symbol %q in cmd/go; not found", sym)
   103  		}
   104  	}
   105  }