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("GET", "http://www.example.com/")
36
37 testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
38
39
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
52 func TestKeepsQueryString(t *testing.T) {
53 google := "https://www.google.com"
54
55 rr := httptest.NewRecorder()
56 querystring := url.Values{"q": {"golang"}, "format": {"json"}}.Encode()
57 r := newRequest("GET", "http://www.example.com/search?"+querystring)
58
59 testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
60 CanonicalHost(google, http.StatusFound)(testHandler).ServeHTTP(rr, r)
61
62 want := google + r.URL.Path + "?" + querystring
63 if rr.Header().Get("Location") != want {
64 t.Fatalf("bad re-direct: got %q want %q", rr.Header().Get("Location"), want)
65 }
66 }
67
68 func TestBadDomain(t *testing.T) {
69 rr := httptest.NewRecorder()
70 r := newRequest("GET", "http://www.example.com/")
71
72 testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
73
74
75 CanonicalHost("%", http.StatusFound)(testHandler).ServeHTTP(rr, r)
76
77 if rr.Code != http.StatusOK {
78 t.Fatalf("bad status: got %v want %v", rr.Code, http.StatusOK)
79 }
80 }
81
82 func TestEmptyHost(t *testing.T) {
83 rr := httptest.NewRecorder()
84 r := newRequest("GET", "http://www.example.com/")
85
86 testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
87
88
89 CanonicalHost("hello.com", http.StatusFound)(testHandler).ServeHTTP(rr, r)
90
91 if rr.Code != http.StatusOK {
92 t.Fatalf("bad status: got %v want %v", rr.Code, http.StatusOK)
93 }
94 }
95
96 func TestHeaderWrites(t *testing.T) {
97 gorilla := "http://www.gorillatoolkit.org"
98
99 testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
100 w.WriteHeader(200)
101 })
102
103
104 var b bytes.Buffer
105 buf := bufio.NewWriter(&b)
106 tl := log.New(buf, "test: ", log.Lshortfile)
107
108 srv := httptest.NewServer(
109 CanonicalHost(gorilla, http.StatusFound)(testHandler))
110 defer srv.Close()
111 srv.Config.ErrorLog = tl
112
113 _, err := http.Get(srv.URL)
114 if err != nil {
115 t.Fatal(err)
116 }
117
118 err = buf.Flush()
119 if err != nil {
120 t.Fatal(err)
121 }
122
123
124 if strings.Contains(b.String(), "multiple response.WriteHeader calls") {
125 t.Fatalf("re-direct did not return early: multiple header writes")
126 }
127 }
128
View as plain text