-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
serve_test.go
74 lines (57 loc) · 1.49 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
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
package serve
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFileServerDefaults(t *testing.T) {
fs := NewFileServer()
_ = fs
}
func TestFileServerOptions(t *testing.T) {
fs := NewFileServer(Options{Directory: "test"})
_ = fs
}
func TestFileServerUse(t *testing.T) {
t.Parallel()
assert := assert.New(t)
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.NoError(err)
res := httptest.NewRecorder()
testMiddleware1 := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("start\n"))
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
testMiddleware2 := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("end\n"))
}
return http.HandlerFunc(fn)
}
fs := &FileServer{
handler: http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
t.Fail()
}),
}
fs.Use(testMiddleware2, testMiddleware1)
fs.ServeHTTP(res, req)
assert.Equal("start\nend\n", res.Body.String())
}
func TestFileServerServeHTTP(t *testing.T) {
t.Parallel()
assert := assert.New(t)
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.NoError(err)
res := httptest.NewRecorder()
fs := &FileServer{
handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("expected"))
}),
}
fs.ServeHTTP(res, req)
assert.Equal("expected", res.Body.String())
}