-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
218 lines (185 loc) · 6.19 KB
/
handlers_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
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
/*
TODO: port https://metacpan.org/source/DROLSKY/Web-Machine-0.17/t/010-resource-tests.t to this test
*/
package blackarachnia_test
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Tamarou/blackarachnia"
"github.com/Tamarou/blackarachnia/handlerMap"
types "github.com/Tamarou/blackarachnia/types"
"github.com/stretchr/testify/assert"
)
type TestResource struct {
disabled bool
methods []string
allowedMethods []string
malformed bool
unauthorized bool
forbidden bool
invalidContentHeaders bool
unknownContentType bool
blackarachnia.Resource
}
func (sr TestResource) KnownContentType(c string) bool { return !sr.unknownContentType }
func (sr TestResource) ServiceAvailable() bool { return !sr.disabled }
func (sr TestResource) KnownMethods() []string { return sr.methods }
func (sr TestResource) AllowedMethods() []string { return sr.allowedMethods }
func (sr TestResource) MalformedRequest(r *http.Request) bool { return sr.malformed }
func (sr TestResource) Authorized(w http.ResponseWriter, auth string) bool { return !sr.unauthorized }
func (sr TestResource) Forbidden() bool { return sr.forbidden }
func (sr TestResource) ValidContentHeaders(r *http.Request) bool { return !sr.invalidContentHeaders }
func (sr TestResource) LastModified() time.Time { return time.Now() }
func (sr TestResource) ContentTypesProvided() types.HandlerMap {
return handlerMap.NewHandlerMap(
handlerMap.Map("text/plain", sr.ToText),
)
}
func (sr TestResource) ToText(w http.ResponseWriter, r *http.Request) error {
io.WriteString(w, "Hello World")
return nil
}
func TestHandlers(t *testing.T) {
request, _ := http.NewRequest(http.MethodGet, "/", nil)
t.Run("Service Unavailble", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{disabled: true}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
got := rr.Code
want := http.StatusServiceUnavailable
if got != want {
t.Errorf("got %d wanted %d", got, want)
}
})
t.Run("Service Available", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
got := rr.Code
want := http.StatusServiceUnavailable
if got == want {
t.Errorf("got %d wanted anything but %d", got, want)
}
})
t.Run("Unknown Method", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
got := rr.Code
want := http.StatusNotImplemented
if got != want {
t.Errorf("got %d wanted %d", got, want)
}
})
t.Run("Known Method", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
methods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
got := rr.Code
want := http.StatusNotImplemented
if got == want {
t.Errorf("got %d wanted anything but %d", got, want)
}
})
t.Run("Allowed Method", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
methods: []string{"GET", "HEAD"},
allowedMethods: []string{"HEAD"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
got := rr.Code
want := http.StatusMethodNotAllowed
if got != want {
t.Errorf("got %d wanted %d", rr.Code, want)
}
if allowed := rr.Header().Get("Allow"); allowed != "HEAD" {
t.Errorf("got %v wanted %v", allowed, "HEAD")
}
})
t.Run("Malformed Request", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
malformed: true,
methods: []string{"GET"},
allowedMethods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
if code := rr.Code; code != http.StatusBadRequest {
t.Errorf("got %d wanted %d", code, http.StatusBadRequest)
}
})
t.Run("Unauthorized", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
unauthorized: true,
methods: []string{"GET"},
allowedMethods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
if code := rr.Code; code != http.StatusUnauthorized {
t.Errorf("got %d wanted %d", code, http.StatusUnauthorized)
}
})
t.Run("Forbidden", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
forbidden: true,
methods: []string{"GET"},
allowedMethods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
if code := rr.Code; code != http.StatusForbidden {
t.Errorf("got %d wanted %d", code, http.StatusForbidden)
}
})
t.Run("Valid Content Headers", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
invalidContentHeaders: true,
methods: []string{"GET"},
allowedMethods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
if code := rr.Code; code != http.StatusNotImplemented {
t.Errorf("got %d wanted %d", code, http.StatusNotImplemented)
}
})
t.Run("Unknown Content Type", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
unknownContentType: true,
methods: []string{"GET"},
allowedMethods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
assert.Equal(t, http.StatusUnsupportedMediaType, rr.Code)
assert.Equal(t, "Unsupported Media Type\n", rr.Body.String())
})
t.Run("200 OK", func(t *testing.T) {
rr := httptest.NewRecorder()
r := &TestResource{
methods: []string{"GET"},
allowedMethods: []string{"GET"},
}
handler := http.HandlerFunc(blackarachnia.NewHandler(r))
handler.ServeHTTP(rr, request)
assert.Equal(t, http.StatusOK, rr.Code, "got 200")
assert.Equal(t, "Hello World", rr.Body.String())
})
}