forked from gogearbox/gearbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
53 lines (44 loc) · 1.14 KB
/
context_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
package gearbox
import (
"net/http"
"testing"
)
// Test passing the request from middleware to handler
func TestNext(t *testing.T) {
// testing routes
routes := []struct {
path string
middleware handlerFunc
}{
{path: "/ok", middleware: emptyMiddleware},
{path: "/unauthorized", middleware: unAuthorizedHandler},
}
// get instance of gearbox
gb := setupGearbox()
// register routes according to method
for _, r := range routes {
gb.Get(r.path, r.middleware, emptyHandler)
}
// start serving
startGearbox(gb)
// Requests that will be tested
testCases := []struct {
path string
statusCode int
}{
{path: "/ok", statusCode: StatusOK},
{path: "/unauthorized", statusCode: StatusUnauthorized},
}
for _, tc := range testCases {
// create and make http request
req, _ := http.NewRequest(MethodGet, tc.path, nil)
response, err := makeRequest(req, gb)
if err != nil {
t.Fatalf("%s(%s): %s", MethodGet, tc.path, err.Error())
}
// check status code
if response.StatusCode != tc.statusCode {
t.Fatalf("%s(%s): returned %d expected %d", MethodGet, tc.path, response.StatusCode, tc.statusCode)
}
}
}