-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
254 lines (208 loc) · 6.93 KB
/
main_test.go
File metadata and controls
254 lines (208 loc) · 6.93 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestNewFileHandlerServesFilesAndCustomNotFoundPage(t *testing.T) {
tempDir := t.TempDir()
mustWriteFile(t, filepath.Join(tempDir, "index.html"), "<h1>index</h1>")
mustWriteFile(t, filepath.Join(tempDir, "404.html"), "<h1>not found</h1>")
handler := newFileHandler(tempDir, false, true, "404.html")
rootRequest := httptest.NewRequest(http.MethodGet, "/", nil)
rootRecorder := httptest.NewRecorder()
handler.ServeHTTP(rootRecorder, rootRequest)
if rootRecorder.Code != http.StatusOK {
t.Fatalf("expected root request to return 200, got %d", rootRecorder.Code)
}
if body := rootRecorder.Body.String(); body != "<h1>index</h1>" {
t.Fatalf("expected root body %q, got %q", "<h1>index</h1>", body)
}
missingRequest := httptest.NewRequest(http.MethodGet, "/missing", nil)
missingRecorder := httptest.NewRecorder()
handler.ServeHTTP(missingRecorder, missingRequest)
if missingRecorder.Code != http.StatusNotFound {
t.Fatalf("expected missing request to return 404, got %d", missingRecorder.Code)
}
if body := missingRecorder.Body.String(); body != "<h1>not found</h1>" {
t.Fatalf("expected missing body %q, got %q", "<h1>not found</h1>", body)
}
}
func TestNewFileHandlerFallsBackToDefaultNotFoundWhenCustomPageIsMissing(t *testing.T) {
handler := newFileHandler(t.TempDir(), false, true, "404.html")
request := httptest.NewRequest(http.MethodGet, "/missing", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusNotFound {
t.Fatalf("expected missing request to return 404, got %d", recorder.Code)
}
if body := recorder.Body.String(); body != "404 page not found\n" {
t.Fatalf("expected default not found body, got %q", body)
}
}
func TestValidateServerConfig(t *testing.T) {
testCases := []struct {
name string
enableHTTP bool
enableHTTPS bool
httpsCert string
httpsKey string
wantErr bool
}{
{
name: "http only",
enableHTTP: true,
enableHTTPS: false,
},
{
name: "https only",
enableHTTP: false,
enableHTTPS: true,
httpsCert: "server.crt",
httpsKey: "server.key",
},
{
name: "http and https",
enableHTTP: true,
enableHTTPS: true,
httpsCert: "server.crt",
httpsKey: "server.key",
},
{
name: "no listeners enabled",
enableHTTP: false,
enableHTTPS: false,
wantErr: true,
},
{
name: "missing cert",
enableHTTP: false,
enableHTTPS: true,
httpsKey: "server.key",
wantErr: true,
},
{
name: "missing key",
enableHTTP: false,
enableHTTPS: true,
httpsCert: "server.crt",
wantErr: true,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
err := validateServerConfig(testCase.enableHTTP, testCase.enableHTTPS, testCase.httpsCert, testCase.httpsKey)
if testCase.wantErr && err == nil {
t.Fatal("expected validation error, got nil")
}
if !testCase.wantErr && err != nil {
t.Fatalf("expected validation to pass, got %v", err)
}
})
}
}
func TestNewFileHandlerDisabledNotFound(t *testing.T) {
handler := newFileHandler(t.TempDir(), false, false, "404.html")
request := httptest.NewRequest(http.MethodGet, "/missing", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", recorder.Code)
}
}
func TestNewFileHandlerNotFoundFileIsDirectory(t *testing.T) {
tempDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(tempDir, "404.html"), 0o755); err != nil {
t.Fatal(err)
}
handler := newFileHandler(tempDir, false, true, "404.html")
request := httptest.NewRequest(http.MethodGet, "/missing", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusNotFound {
t.Fatalf("expected 404, got %d", recorder.Code)
}
if body := recorder.Body.String(); body != "404 page not found\n" {
t.Fatalf("expected default not found body, got %q", body)
}
}
func TestStaticStatusResponseWriterOverridesStatus(t *testing.T) {
recorder := httptest.NewRecorder()
w := &staticStatusResponseWriter{
ResponseWriter: recorder,
statusCode: http.StatusNotFound,
}
if _, err := w.Write([]byte("content")); err != nil {
t.Fatalf("unexpected write error: %v", err)
}
if recorder.Code != http.StatusNotFound {
t.Fatalf("expected status %d, got %d", http.StatusNotFound, recorder.Code)
}
if body := recorder.Body.String(); body != "content" {
t.Fatalf("expected body %q, got %q", "content", body)
}
}
func TestStaticStatusResponseWriterIgnoresDuplicateWriteHeader(t *testing.T) {
recorder := httptest.NewRecorder()
w := &staticStatusResponseWriter{
ResponseWriter: recorder,
statusCode: http.StatusNotFound,
}
w.WriteHeader(http.StatusOK)
w.WriteHeader(http.StatusInternalServerError)
if recorder.Code != http.StatusNotFound {
t.Fatalf("expected status %d, got %d", http.StatusNotFound, recorder.Code)
}
}
func TestNewFileHandlerDirListingDisabled(t *testing.T) {
tempDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(tempDir, "subdir"), 0o755); err != nil {
t.Fatal(err)
}
handler := newFileHandler(tempDir, false, true, "404.html")
request := httptest.NewRequest(http.MethodGet, "/subdir/", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusNotFound {
t.Fatalf("expected 404 for directory without index.html, got %d", recorder.Code)
}
}
func TestNewFileHandlerDirListingEnabled(t *testing.T) {
tempDir := t.TempDir()
if err := os.MkdirAll(filepath.Join(tempDir, "subdir"), 0o755); err != nil {
t.Fatal(err)
}
handler := newFileHandler(tempDir, true, true, "404.html")
request := httptest.NewRequest(http.MethodGet, "/subdir/", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200 for directory listing, got %d", recorder.Code)
}
}
func TestNewFileHandlerDirWithIndexServedWhenListingDisabled(t *testing.T) {
tempDir := t.TempDir()
subdir := filepath.Join(tempDir, "subdir")
if err := os.MkdirAll(subdir, 0o755); err != nil {
t.Fatal(err)
}
mustWriteFile(t, filepath.Join(subdir, "index.html"), "<h1>subdir</h1>")
handler := newFileHandler(tempDir, false, true, "404.html")
request := httptest.NewRequest(http.MethodGet, "/subdir/", nil)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("expected 200 for directory with index.html, got %d", recorder.Code)
}
if body := recorder.Body.String(); body != "<h1>subdir</h1>" {
t.Fatalf("expected body %q, got %q", "<h1>subdir</h1>", body)
}
}
func mustWriteFile(t *testing.T, path, contents string) {
t.Helper()
if err := os.WriteFile(path, []byte(contents), 0o644); err != nil {
t.Fatalf("write %s: %v", path, err)
}
}