-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve_test.go
44 lines (41 loc) · 1.17 KB
/
serve_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func Test_routes_ok(t *testing.T) {
cases := map[string]struct {
method string
path string
contentType string
}{
"search": {http.MethodGet, "/search?q=development", ApplicationJson},
"file": {http.MethodGet, "/files/testdata/hello.html", TextHtml},
"root": {http.MethodGet, "/", TextHtml},
"main.js": {http.MethodGet, "/main.js", ApplicationJs},
}
index := New(10)
index.Update(&Document{Name: "index.md", WordCount: map[string]int{"development": 1}})
for name, tc := range cases {
tc := tc
t.Run(name, func(t *testing.T) {
url := fmt.Sprintf("http://localhost%s", tc.path)
w := httptest.NewRecorder()
r, err := http.NewRequest(tc.method, url, nil)
if err != nil {
t.Errorf("NewRequest(%s, %s, ...) error=%v, want nil", tc.method, url, err)
}
mux := BuildRoutes([]string{"testdata"}, index)
mux.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("w.Code=%d, want 200", w.Code)
}
actual := w.Header().Get(HeaderContentType)
if actual != tc.contentType {
t.Errorf("Content-type=<%s>, want <%s>", actual, tc.contentType)
}
})
}
}