-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
240 lines (223 loc) · 6.34 KB
/
main_test.go
File metadata and controls
240 lines (223 loc) · 6.34 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/aws/aws-lambda-go/events"
"github.com/mount-joy/thelist-lambda/cors"
"github.com/mount-joy/thelist-lambda/handlers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestFullFlow(t *testing.T) {
t.Run("Successful Request", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintf(w, "127.0.0.1")
}))
defer ts.Close()
h := handler{
router: handlers.NewRouter(),
allowedDomains: cors.NewOriginChecker(),
}
request := events.APIGatewayV2HTTPRequest{
Headers: map[string]string{"Origin": "thelist.app"},
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Method: "GET",
Path: "/hello",
},
},
QueryStringParameters: map[string]string{"name": "Joy"},
}
gotResponse, gotErr := h.doRequest(request)
expected := "{\"message\":\"Hello, Joy\"}"
assert.NoError(t, gotErr)
assert.Equal(t, expected, gotResponse.Body)
})
}
type mockRouter struct {
mock.Mock
}
func (mr *mockRouter) Route(request events.APIGatewayV2HTTPRequest) (interface{}, int) {
args := mr.Called(request)
return args.Get(0), args.Int(1)
}
type mockOriginChecker struct {
mock.Mock
}
func (mcd *mockOriginChecker) Options(request events.APIGatewayV2HTTPRequest) events.APIGatewayV2HTTPResponse {
args := mcd.Called(request)
return args.Get(0).(events.APIGatewayV2HTTPResponse)
}
func (mcd *mockOriginChecker) GetCorsHeaders(request events.APIGatewayV2HTTPRequest) map[string]string {
args := mcd.Called(request)
return args.Get(0).(map[string]string)
}
func TestHandler(t *testing.T) {
type mockRoute struct {
body interface{}
status int
}
type mockOptions struct {
response events.APIGatewayV2HTTPResponse
}
type mockGetCorsHeaders struct {
headers map[string]string
}
tests := []struct {
name string
request events.APIGatewayV2HTTPRequest
mockOptions *mockOptions
mockGetCorsHeaders *mockGetCorsHeaders
mockRoute *mockRoute
expectedBody string
expectedStatus int
expectedHeaders map[string]string
expectedErr error
}{
{
name: "Router doesn't error",
request: events.APIGatewayV2HTTPRequest{
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Path: "/test",
},
},
},
mockGetCorsHeaders: &mockGetCorsHeaders{},
mockRoute: &mockRoute{
body: map[string]string{"message": "huge success"},
status: 200,
},
expectedBody: "{\"message\":\"huge success\"}",
expectedStatus: 200,
},
{
name: "Route returns nil",
request: events.APIGatewayV2HTTPRequest{
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Path: "/test",
},
},
},
mockGetCorsHeaders: &mockGetCorsHeaders{},
mockRoute: &mockRoute{
body: nil,
status: 203,
},
expectedStatus: 203,
expectedBody: "null",
},
{
name: "Sets Access-Control-Allow-Origin when Origin Header is set",
request: events.APIGatewayV2HTTPRequest{
Headers: map[string]string{"Origin": "test-place"},
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Path: "/test",
Method: "GET",
},
},
},
mockGetCorsHeaders: &mockGetCorsHeaders{
headers: map[string]string{
"Access-Control-Allow-Origin": "test-place",
},
},
mockRoute: &mockRoute{
body: map[string]string{"message": "huge success"},
status: 200,
},
expectedBody: "{\"message\":\"huge success\"}",
expectedStatus: 200,
expectedHeaders: map[string]string{
"Access-Control-Allow-Origin": "test-place",
},
},
{
name: "If origin isn't allowed, don't add cors headers",
request: events.APIGatewayV2HTTPRequest{
Headers: map[string]string{"Origin": "some-not-allowed-domain"},
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Path: "/test",
Method: "GET",
},
},
},
mockGetCorsHeaders: &mockGetCorsHeaders{
headers: nil,
},
mockRoute: &mockRoute{
body: map[string]string{"message": "huge success"},
status: 200,
},
expectedBody: "{\"message\":\"huge success\"}",
expectedStatus: 200,
expectedHeaders: nil,
},
{
name: "OPTIONS request for allowed domain returns methods",
request: events.APIGatewayV2HTTPRequest{
Headers: map[string]string{"Origin": "test-place"},
RequestContext: events.APIGatewayV2HTTPRequestContext{
HTTP: events.APIGatewayV2HTTPRequestContextHTTPDescription{
Method: "OPTIONS",
},
},
},
mockOptions: &mockOptions{
response: events.APIGatewayV2HTTPResponse{
Headers: map[string]string{
"Access-Control-Allow-Methods": "DELETE, GET, PATCH, POST",
"Access-Control-Allow-Origin": "test-place",
},
StatusCode: 204,
},
},
expectedHeaders: map[string]string{
"Access-Control-Allow-Methods": "DELETE, GET, PATCH, POST",
"Access-Control-Allow-Origin": "test-place",
},
expectedStatus: 204,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
router := &mockRouter{}
router.Test(t)
defer router.AssertExpectations(t)
if tt.mockRoute != nil {
router.
On("Route", tt.request).
Return(tt.mockRoute.body, tt.mockRoute.status).
Once()
}
originChecker := &mockOriginChecker{}
originChecker.Test(t)
defer originChecker.AssertExpectations(t)
if tt.mockOptions != nil {
originChecker.On("Options", tt.request).
Return(tt.mockOptions.response).
Once()
}
if tt.mockGetCorsHeaders != nil {
originChecker.On("GetCorsHeaders", tt.request).
Return(tt.mockGetCorsHeaders.headers).
Once()
}
h := handler{
router: router,
allowedDomains: originChecker,
}
gotRes, gotErr := h.doRequest(tt.request)
assert.Equal(t, tt.expectedErr, gotErr)
assert.Equal(t, tt.expectedBody, gotRes.Body)
assert.Equal(t, tt.expectedStatus, gotRes.StatusCode)
assert.Equal(t, tt.expectedHeaders, gotRes.Headers)
})
}
}