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