-
Notifications
You must be signed in to change notification settings - Fork 0
/
method_test.go
147 lines (125 loc) · 3.04 KB
/
method_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
package web
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestMethod_Get_Any(t *testing.T) {
var getH http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "hello!", http.StatusOK)
}
var anyH http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "any", http.StatusOK)
}
var notFound http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "found, it was not", http.StatusNotFound)
}
m := &Method{
Get: getH,
Any: anyH,
MethodNotAllowed: notFound,
}
_, body := testMethod(t, "GET", m)
assert.Equal(t, "hello!\n", body)
_, body = testMethod(t, "POST", m)
assert.Equal(t, "any\n", body)
}
func TestMethod_Get_MethodNotFound(t *testing.T) {
var getH http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "hello!", http.StatusOK)
}
var putH http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "created!", http.StatusCreated)
}
m := &Method{
Get: getH,
Put: putH,
}
w := testRequest(t, "GET", m)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "hello!\n", w.Body.String())
w = testRequest(t, "POST", m)
assert.Equal(t, 405, w.Code)
assert.Equal(t, "405 method not allowed\n", w.Body.String())
assert.Equal(t, "GET, PUT", w.HeaderMap.Get("Allow"))
}
func TestMethod_Options(t *testing.T) {
var getH http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "hello!", http.StatusOK)
}
var putH http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "created!", http.StatusCreated)
}
m := &Method{
Get: getH,
Put: putH,
}
w := testRequest(t, "OPTIONS", m)
assert.Equal(t, 200, w.Code)
assert.Equal(t, "", w.Body.String())
assert.Equal(t, "GET, PUT", w.HeaderMap.Get("Allow"))
// Server implementation apparently takes care of this.
// assert.Equal(t, "0", w.HeaderMap.Get("Content-Length"))
}
func TestAllowedMethods(t *testing.T) {
handler := &Method{}
tests := []struct {
msg string
m *Method
exp []string
}{
{
"all",
&Method{
Delete: handler,
Get: handler,
Head: handler,
Options: handler,
Patch: handler,
Post: handler,
Put: handler,
},
[]string{
"DELETE",
"GET",
"HEAD",
"PATCH",
"POST",
"PUT",
},
},
{
"empty",
&Method{},
[]string{},
},
{
"get as head",
&Method{
GetAsHead: true,
Get: handler,
},
[]string{
"GET",
"HEAD",
},
},
}
for _, test := range tests {
assert.Equal(t, test.exp, allowedMethods(test.m), test.msg)
}
}
func testRequest(t testing.TB, method string, f http.Handler) *httptest.ResponseRecorder {
req, err := http.NewRequest(method, "http://example.com/foo", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
f.ServeHTTP(w, req)
return w
}
func testMethod(t testing.TB, method string, f http.Handler) (int, string) {
w := testRequest(t, method, f)
return w.Code, w.Body.String()
}