-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
179 lines (160 loc) · 4.4 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
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
package mux
import (
"embed"
"encoding/json"
"errors"
"io"
"io/fs"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
)
//go:embed testdata
var testdataFS embed.FS
type testData struct {
N int `json:"n"`
}
func (f testData) Validate() error {
if f.N != 1 {
return errors.New("n must be 1")
}
return nil
}
func testLogger(req *http.Request, err error) {}
func testHandler(w http.ResponseWriter, req *http.Request) error {
_, err := io.WriteString(w, req.RequestURI)
return err
}
func newTestRequest(method, path string, body io.Reader) *http.Request {
rc := &requestContext{}
req := httptest.NewRequest(method, path, body)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json; charset=utf-8")
return setContext(req, rc)
}
func TestAutomaticHEAD(t *testing.T) {
h := New()
h.Add("/", testHandler, WithMethod(http.MethodGet))
server := httptest.NewServer(h)
defer server.Close()
client := server.Client()
resp, err := client.Head(server.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp.Body.Close()
}
func TestAutomaticOPTIONS(t *testing.T) {
h := New()
h.Add("/", testHandler, WithMethod(http.MethodGet, http.MethodDelete))
server := httptest.NewServer(h)
defer server.Close()
client := server.Client()
req, err := http.NewRequest(http.MethodOptions, server.URL, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp.Body.Close()
assertStatus(t, resp, http.StatusOK)
assertHeader(t, resp, "Allow", "DELETE, GET, HEAD, OPTIONS")
}
func TestMethodNotAllowed(t *testing.T) {
h := New()
h.Add("/", testHandler, WithMethod(http.MethodGet, http.MethodDelete))
server := httptest.NewServer(h)
defer server.Close()
client := server.Client()
resp, err := client.Post(server.URL, "text/plain", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
resp.Body.Close()
assertStatus(t, resp, http.StatusMethodNotAllowed)
assertHeader(t, resp, "Allow", "DELETE, GET, HEAD, OPTIONS")
}
func TestPanic(t *testing.T) {
req := newTestRequest(http.MethodGet, "https://example.com/", nil)
testPanic(t, req)
}
func TestPanicWithoutHeaders(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "https://example.com/", nil)
testPanic(t, req)
}
func testPanic(t *testing.T, req *http.Request) {
h := New(WithLogger(testLogger))
h.Add("/", func(w http.ResponseWriter, req *http.Request) error {
panic(errors.New("test"))
return nil
}, WithMethod(http.MethodGet))
server := httptest.NewServer(h)
defer server.Close()
client := server.Client()
req, err := http.NewRequest(http.MethodGet, server.URL, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
req.Header.Set("X-Request-ID", "test")
resp, err := client.Do(req)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()
assertStatus(t, resp, http.StatusInternalServerError)
var view ErrorView
err = json.NewDecoder(resp.Body).Decode(&view)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if view.RequestID == "" {
t.Fatalf("request id should be set")
}
}
func TestFileServer(t *testing.T) {
h := New()
testdata, err := fs.Sub(testdataFS, "testdata")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
h.FileServer("/prefix/*", testdata)
server := httptest.NewServer(h)
defer server.Close()
client := server.Client()
resp, err := client.Get(server.URL + "/prefix/base.ext")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertString(t, "file", strings.TrimSpace(string(b)), "OK")
}
func assertInt(t *testing.T, what string, have, want int) {
if have != want {
t.Fatalf("%s\nhave %d\nwant %d", what, have, want)
}
}
func assertString(t *testing.T, what, have, want string) {
if have != want {
t.Fatalf("%s\nhave '%s'\nwant '%s'", what, have, want)
}
}
func assertDeepEqual(t *testing.T, what string, have, want interface{}) {
if !reflect.DeepEqual(have, want) {
t.Fatalf("%s\nhave %#v\nwant %#v", what, have, want)
}
}
func assertStatus(t *testing.T, resp *http.Response, want int) {
assertInt(t, "status code", resp.StatusCode, want)
}
func assertHeader(t *testing.T, resp *http.Response, key, want string) {
have := resp.Header.Get(key)
assertString(t, "headers", have, want)
}