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
7package http
8
9import (
10	"bytes"
11	"internal/testenv"
12	"os/exec"
13	"reflect"
14	"testing"
15	"time"
16)
17
18func init() {
19	shutdownPollInterval = 5 * time.Millisecond
20}
21
22func TestForeachHeaderElement(t *testing.T) {
23	tests := []struct {
24		in   string
25		want []string
26	}{
27		{"Foo", []string{"Foo"}},
28		{" Foo", []string{"Foo"}},
29		{"Foo ", []string{"Foo"}},
30		{" Foo ", []string{"Foo"}},
31
32		{"foo", []string{"foo"}},
33		{"anY-cAsE", []string{"anY-cAsE"}},
34
35		{"", nil},
36		{",,,,  ,  ,,   ,,, ,", nil},
37
38		{" Foo,Bar, Baz,lower,,Quux ", []string{"Foo", "Bar", "Baz", "lower", "Quux"}},
39	}
40	for _, tt := range tests {
41		var got []string
42		foreachHeaderElement(tt.in, func(v string) {
43			got = append(got, v)
44		})
45		if !reflect.DeepEqual(got, tt.want) {
46			t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want)
47		}
48	}
49}
50
51func TestCleanHost(t *testing.T) {
52	tests := []struct {
53		in, want string
54	}{
55		{"www.google.com", "www.google.com"},
56		{"www.google.com foo", "www.google.com"},
57		{"www.google.com/foo", "www.google.com"},
58		{" first character is a space", ""},
59		{"[1::6]:8080", "[1::6]:8080"},
60
61		// Punycode:
62		{"гофер.рф/foo", "xn--c1ae0ajs.xn--p1ai"},
63		{"bücher.de", "xn--bcher-kva.de"},
64		{"bücher.de:8080", "xn--bcher-kva.de:8080"},
65		// Verify we convert to lowercase before punycode:
66		{"BÜCHER.de", "xn--bcher-kva.de"},
67		{"BÜCHER.de:8080", "xn--bcher-kva.de:8080"},
68		// Verify we normalize to NFC before punycode:
69		{"gophér.nfc", "xn--gophr-esa.nfc"},            // NFC input; no work needed
70		{"goph\u0065\u0301r.nfd", "xn--gophr-esa.nfd"}, // NFD input
71	}
72	for _, tt := range tests {
73		got := cleanHost(tt.in)
74		if tt.want != got {
75			t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want)
76		}
77	}
78}
79
80// Test that cmd/go doesn't link in the HTTP server.
81//
82// This catches accidental dependencies between the HTTP transport and
83// server code.
84func TestCmdGoNoHTTPServer(t *testing.T) {
85	t.Parallel()
86	goBin := testenv.GoToolPath(t)
87	out, err := exec.Command(goBin, "tool", "nm", goBin).CombinedOutput()
88	if err != nil {
89		t.Fatalf("go tool nm: %v: %s", err, out)
90	}
91	wantSym := map[string]bool{
92		// Verify these exist: (sanity checking this test)
93		"net/http.(*Client).Get":          true,
94		"net/http.(*Transport).RoundTrip": true,
95
96		// Verify these don't exist:
97		"net/http.http2Server":           false,
98		"net/http.(*Server).Serve":       false,
99		"net/http.(*ServeMux).ServeHTTP": false,
100		"net/http.DefaultServeMux":       false,
101	}
102	for sym, want := range wantSym {
103		got := bytes.Contains(out, []byte(sym))
104		if !want && got {
105			t.Errorf("cmd/go unexpectedly links in HTTP server code; found symbol %q in cmd/go", sym)
106		}
107		if want && !got {
108			t.Errorf("expected to find symbol %q in cmd/go; not found", sym)
109		}
110	}
111}
112