| 1 |
package handlers |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"log" |
| 7 |
"net/http" |
| 8 |
"net/http/httptest" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestCleanHost(t *testing.T) { |
| 15 |
tests := []struct { |
| 16 |
in, want string |
| 17 |
}{ |
| 18 |
{"www.google.com", "www.google.com"}, |
| 19 |
{"www.google.com foo", "www.google.com"}, |
| 20 |
{"www.google.com/foo", "www.google.com"}, |
| 21 |
{" first character is a space", ""}, |
| 22 |
} |
| 23 |
for _, tt := range tests { |
| 24 |
got := cleanHost(tt.in) |
| 25 |
if tt.want != got { |
| 26 |
t.Errorf("cleanHost(%q) = %q, want %q", tt.in, got, tt.want) |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestCanonicalHost(t *testing.T) { |
| 32 |
gorilla := "http://www.gorillatoolkit.org" |
| 33 |
|
| 34 |
rr := httptest.NewRecorder() |
| 35 |
r := newRequest(http.MethodGet, "http://www.example.com/") |
| 36 |
|
| 37 |
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 38 |
|
| 39 |
// Test a re-direct: should return a 302 Found. |
| 40 |
CanonicalHost(gorilla, http.StatusFound)(testHandler).ServeHTTP(rr, r) |
| 41 |
|
| 42 |
if rr.Code != http.StatusFound { |
| 43 |
t.Fatalf("bad status: got %v want %v", rr.Code, http.StatusFound) |
| 44 |
} |
| 45 |
|
| 46 |
if rr.Header().Get("Location") != gorilla+r.URL.Path { |
| 47 |
t.Fatalf("bad re-direct: got %q want %q", rr.Header().Get("Location"), gorilla+r.URL.Path) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
func TestKeepsQueryString(t *testing.T) { |
| 52 |
google := "https://www.google.com" |
| 53 |
|
| 54 |
rr := httptest.NewRecorder() |
| 55 |
querystring := url.Values{"q": {"golang"}, "format": {"json"}}.Encode() |
| 56 |
r := newRequest(http.MethodGet, "http://www.example.com/search?"+querystring) |
| 57 |
|
| 58 |
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 59 |
CanonicalHost(google, http.StatusFound)(testHandler).ServeHTTP(rr, r) |
| 60 |
|
| 61 |
want := google + r.URL.Path + "?" + querystring |
| 62 |
if rr.Header().Get("Location") != want { |
| 63 |
t.Fatalf("bad re-direct: got %q want %q", rr.Header().Get("Location"), want) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
func TestBadDomain(t *testing.T) { |
| 68 |
rr := httptest.NewRecorder() |
| 69 |
r := newRequest(http.MethodGet, "http://www.example.com/") |
| 70 |
|
| 71 |
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 72 |
|
| 73 |
// Test a bad domain - should return 200 OK. |
| 74 |
CanonicalHost("%", http.StatusFound)(testHandler).ServeHTTP(rr, r) |
| 75 |
|
| 76 |
if rr.Code != http.StatusOK { |
| 77 |
t.Fatalf("bad status: got %v want %v", rr.Code, http.StatusOK) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
func TestEmptyHost(t *testing.T) { |
| 82 |
rr := httptest.NewRecorder() |
| 83 |
r := newRequest(http.MethodGet, "http://www.example.com/") |
| 84 |
|
| 85 |
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) |
| 86 |
|
| 87 |
// Test a domain that returns an empty url.Host from url.Parse. |
| 88 |
CanonicalHost("hello.com", http.StatusFound)(testHandler).ServeHTTP(rr, r) |
| 89 |
|
| 90 |
if rr.Code != http.StatusOK { |
| 91 |
t.Fatalf("bad status: got %v want %v", rr.Code, http.StatusOK) |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
func TestHeaderWrites(t *testing.T) { |
| •96 |
if testing.Short() { |
| 97 |
t.Skip("skipping internet-connecting test in short mode") |
| 98 |
} |
| 99 |
|
| 100 |
gorilla := "http://www.gorillatoolkit.org" |
| 101 |
|
| 102 |
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 103 |
w.WriteHeader(http.StatusOK) |
| 104 |
}) |
| 105 |
|
| 106 |
// Catch the log output to ensure we don't write multiple headers. |
| 107 |
var b bytes.Buffer |
| 108 |
buf := bufio.NewWriter(&b) |
| 109 |
tl := log.New(buf, "test: ", log.Lshortfile) |
| 110 |
|
| 111 |
srv := httptest.NewServer( |
| 112 |
CanonicalHost(gorilla, http.StatusFound)(testHandler)) |
| 113 |
defer srv.Close() |
| 114 |
srv.Config.ErrorLog = tl |
| 115 |
|
| 116 |
_, err := http.Get(srv.URL) |
| 117 |
if err != nil { |
| 118 |
t.Fatal(err) |
| 119 |
} |
| 120 |
|
| 121 |
err = buf.Flush() |
| 122 |
if err != nil { |
| 123 |
t.Fatal(err) |
| 124 |
} |
| 125 |
|
| 126 |
// We rely on the error not changing: net/http does not export it. |
| 127 |
if strings.Contains(b.String(), "multiple response.WriteHeader calls") { |
| 128 |
t.Fatalf("re-direct did not return early: multiple header writes") |
| 129 |
} |
| 130 |
} |