-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler_test.go
142 lines (111 loc) · 2.64 KB
/
handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package forever
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func codeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int) {
if r.Code != expectedCode {
t.Errorf("Code %d expected, got: %d", expectedCode, r.Code)
}
}
func TestWrongVersion(t *testing.T) {
// wring version string, expect a 404
handler := NewStaticHandler(
http.Dir("."),
"1234567",
nil,
true,
)
urlObj, err := url.Parse("http://1.2.3.4/wrong_version/handler.go")
if err != nil {
t.Fatal(err)
}
r := http.Request{
Method: "GET",
URL: urlObj,
}
r.Header = http.Header{}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, &r)
codeIs(t, recorder, 404)
}
func TestDevelopement(t *testing.T) {
// right version string, expect a 200
handler := NewStaticHandler(
http.Dir("."),
"1234567",
nil,
true,
)
urlObj, err := url.Parse("http://1.2.3.4/1234567/handler.go")
if err != nil {
t.Fatal(err)
}
r := http.Request{
Method: "GET",
URL: urlObj,
}
r.Header = http.Header{}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, &r)
codeIs(t, recorder, 200)
}
func TestFirstRequest(t *testing.T) {
// expect a 200 with all the custom headers set
handler := NewStaticHandler(
http.Dir("."),
"1234567",
nil,
false,
)
handler = handler.(*staticHandler)
urlObj, err := url.Parse("http://1.2.3.4/1234567/handler.go")
if err != nil {
t.Fatal(err)
}
r := http.Request{
Method: "GET",
URL: urlObj,
}
r.Header = http.Header{}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, &r)
codeIs(t, recorder, 200)
expires := recorder.HeaderMap.Get("Expires")
if expires == "" {
t.Errorf("Expires expected, got: %s", expires)
}
control := recorder.HeaderMap.Get("Cache-Control")
if control != "public; max-age=31536000; s-maxage=31536000" {
t.Errorf("Cache-Control public; max-age=31536000; s-maxage=31536000 expected, got: %s", control)
}
last := recorder.HeaderMap.Get("Last-Modified")
if last != "Thu, 01 Jan 1970 00:00:00 GMT" {
t.Errorf("Last-Modified Thu, 01 Jan 1970 00:00:00 GMT expected, got: %s", last)
}
}
func TestSecondRequest(t *testing.T) {
// sent with If-Modified-Since, expect a 304
handler := NewStaticHandler(
http.Dir("."),
"1234567",
nil,
false,
)
handler = handler.(*staticHandler)
urlObj, err := url.Parse("http://1.2.3.4/1234567/handler.go")
if err != nil {
t.Fatal(err)
}
r := http.Request{
Method: "GET",
URL: urlObj,
}
r.Header = http.Header{}
r.Header.Set("If-Modified-Since", "Sat, 01 Apr 2113 04:15:01 GMT")
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, &r)
codeIs(t, recorder, 304)
}