-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware_test.go
200 lines (178 loc) · 6.04 KB
/
middleware_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
package goMiddlewareChain
import (
"context"
"fmt"
"net/http"
"testing"
"net/http/httptest"
"encoding/json"
"reflect"
"github.com/julienschmidt/httprouter"
)
// response handler which write response to writer
var responseHandler = func(response *Response, writer http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
bodyMap := map[string]interface{}{"data": response.Data, "status": response.Status.Code}
bodyJSON, _ := json.Marshal(bodyMap)
fmt.Fprintf(writer, "%s", bodyJSON)
}
func TestMain(t *testing.T) {
var tests = []struct {
handler Handler
restrictHandler RestrictHandler
expectedData interface{}
}{
// Test 0:
// Check if chain works
{
handler: func(response *Response, _ *http.Request, _ httprouter.Params) {
response.Data = "data"
},
expectedData: "data",
},
// Test 1:
// Check if restrictedChain works with restriction true
{
handler: func(response *Response, _ *http.Request, _ httprouter.Params) {
response.Data = "data"
},
restrictHandler: func(response *Response, request *http.Request, _ httprouter.Params) bool {
return true
},
expectedData: "data",
},
}
// run all tests
for index, test := range tests {
// recorder and request to simulate test
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "", nil)
// build chain
if reflect.ValueOf(test.restrictHandler).IsNil() {
handlerChain := RequestChainHandler(responseHandler, test.handler)
handlerChain(recorder, request, nil)
} else {
restrictedHandleChain := RestrictedRequestChainHandler(test.restrictHandler, responseHandler, test.handler)
restrictedHandleChain(recorder, request, nil)
}
// check result
var responseBody map[string]interface{}
json.Unmarshal([]byte(recorder.Body.String()), &responseBody)
if err != nil || responseBody["data"] != test.expectedData {
t.Errorf("Test %v failed: expected data is not equals response.data; expected: %v; response: %v", index, test.expectedData, responseBody["data"])
}
}
}
func TestContextHandler(t *testing.T) {
var key = struct {
key string
}{
key: "key",
}
var tests = []struct {
handler []ContextHandler
restrictHandler RestrictHandler
expectedData interface{}
}{
// Test 0:
// Check if chain works
{
handler: []ContextHandler{
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
return context.WithValue(ctx, key, "data")
},
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Data = ctx.Value(key).(string)
return ctx
},
},
expectedData: "data",
},
}
// run all tests
for index, test := range tests {
// recorder and request to simulate test
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "", nil)
// build chain
handlerChain := RequestChainContextHandler(responseHandler, test.handler...)
handlerChain(recorder, request, nil)
// check result
var responseBody map[string]interface{}
json.Unmarshal([]byte(recorder.Body.String()), &responseBody)
if err != nil || responseBody["data"] != test.expectedData {
t.Errorf("Test %v failed: expected data is not equals response.data; expected: %v; response: %v", index, test.expectedData, responseBody["data"])
}
}
}
func TestContextFailHandler(t *testing.T) {
var tests = []struct {
handler []ContextHandler
restrictHandler RestrictContextHandler
expectedStatus int
checkResponseOfEveryHandler bool
}{
// Test 0:
// Check expected code is 500
{
handler: []ContextHandler{
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Status.Code = http.StatusOK
return ctx
},
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Status.Code = http.StatusInternalServerError
return ctx
},
// This handler should not run!
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Status.Code = http.StatusOK
return ctx
},
},
restrictHandler: func(ctx context.Context, _ *Response, _ *http.Request, _ httprouter.Params) (context.Context, bool) {
return ctx, true
},
expectedStatus: 500,
checkResponseOfEveryHandler: true,
},
// Test 1:
// Check expected code == 200
{
handler: []ContextHandler{
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Status.Code = http.StatusOK
return ctx
},
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Status.Code = http.StatusInternalServerError
return ctx
},
// This handler should not run!
func(ctx context.Context, response *Response, _ *http.Request, _ httprouter.Params) context.Context {
response.Status.Code = http.StatusOK
return ctx
},
},
restrictHandler: func(ctx context.Context, _ *Response, _ *http.Request, _ httprouter.Params) (context.Context, bool) {
return ctx, true
},
expectedStatus: 200,
checkResponseOfEveryHandler: false,
},
}
// run all tests
for index, test := range tests {
// recorder and request to simulate test
recorder := httptest.NewRecorder()
request, err := http.NewRequest("POST", "", nil)
// build chain
handlerChain := RestrictedRequestChainContextHandlerWithResponseCheck(test.checkResponseOfEveryHandler, test.restrictHandler, responseHandler, test.handler...)
handlerChain(recorder, request, nil)
// check result
var responseBody map[string]interface{}
json.Unmarshal([]byte(recorder.Body.String()), &responseBody)
if err != nil || int(responseBody["status"].(float64)) != test.expectedStatus {
t.Errorf("Test %v failed: expected data is not equals response.status.code; expected: %v; response: %v", index, test.expectedStatus, responseBody["status"])
}
}
}