-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_test.go
186 lines (147 loc) · 6.19 KB
/
main_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
package Messiah
import (
"context"
"net/http"
"testing"
"github.com/aws/aws-lambda-go/events"
. "github.com/smartystreets/goconvey/convey"
)
var MockAPIGatewayProxyRequest = events.APIGatewayProxyRequest{
Resource: "/",
Path: "/",
HTTPMethod: "GET",
Headers: map[string]string{
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"DNT": "1",
"Host": "123abcdefg.execute-api.us-east-1.amazonaws.com",
"Referer": "https://google.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0",
"Via": "1.1 abc.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "abc",
"X-Amzn-Trace-Id": "Root=abc",
"X-Forwarded-For": "0.0.0.0, 0.0.0.0",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
},
QueryStringParameters: map[string]string{},
PathParameters: map[string]string{},
StageVariables: map[string]string{},
RequestContext: events.APIGatewayProxyRequestContext{
AccountID: "012345678912",
ResourceID: "abcdefg123",
Stage: "Prod",
RequestID: "iaaaaaaaa-bbbb-cccc-dddd-123456789abc",
Identity: events.APIGatewayRequestIdentity{
CognitoIdentityPoolID: "",
AccountID: "",
CognitoIdentityID: "",
Caller: "",
APIKey: "",
SourceIP: "0.0.0.0",
CognitoAuthenticationType: "",
CognitoAuthenticationProvider: "",
UserArn: "",
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:57.0) Gecko/20100101 Firefox/57.0",
User: "",
},
ResourcePath: "/",
HTTPMethod: "GET",
APIID: "123abcdefg",
},
}
var MockLambdaContext *context.Context
func TestParsingRequest(t *testing.T) {
Convey("given an ApiGatewayProxyRequest with JSON data in the req.RequestData", t, func() {
MockAPIGatewayProxyRequest.Body = "{\"Hello\":\"World\"}"
request := parseRequest(MockLambdaContext, MockAPIGatewayProxyRequest)
Convey("it should return a properly unmarshalled RequestData map[string]", func() {
So(request.RequestData, ShouldContainKey, "Hello")
So(request.RequestData["Hello"], ShouldEqual, "World")
})
Convey("it should return the rest of the properties from ApiGatewayProxyRequest", func() {
So(MockAPIGatewayProxyRequest, ShouldResemble, MockAPIGatewayProxyRequest)
})
})
Convey("given an ApiGatewayProxyRequest with a string in the req.Body", t, func() {
MockAPIGatewayProxyRequest.Body = "Hello World"
request := parseRequest(MockLambdaContext, MockAPIGatewayProxyRequest)
Convey("it should return the data as a string in the RequestData property", func() {
So(request.Body, ShouldEqual, "Hello World")
})
Convey("it should return the rest of the properties from ApiGatewayProxyRequest", func() {
So(MockAPIGatewayProxyRequest, ShouldResemble, MockAPIGatewayProxyRequest)
})
})
}
func TestParsingResponse(t *testing.T) {
var MockResponse Response
Convey("given a Response with a `res.headers` map[string]string property", t, func() {
MockResponse.Headers = map[string]string{"Hello": "World"}
APIGatewayProxyResponse := parseResponse(MockResponse)
Convey("it should return the headers as a map[string]string in `res.APIGatewayProxyResponse.Headers`", func() {
So(APIGatewayProxyResponse.Headers, ShouldContainKey, "Hello")
So(APIGatewayProxyResponse.Headers["Hello"], ShouldEqual, "World")
})
})
Convey("given a Response with a valid `res.StatusCode` property", t, func() {
MockResponse.StatusCode = 200
APIGatewayProxyResponse := parseResponse(MockResponse)
Convey("it should return the proper `res.APIGatewayProxyResponse.StatusCode", func() {
So(APIGatewayProxyResponse.StatusCode, ShouldEqual, 200)
})
})
Convey("given a Response with a valid `res.StatusCode` property set by the net/http package", t, func() {
MockResponse.StatusCode = http.StatusOK
APIGatewayProxyResponse := parseResponse(MockResponse)
Convey("it should return the proper `res.APIGatewayProxyResponse.StatusCode", func() {
So(APIGatewayProxyResponse.StatusCode, ShouldEqual, 200)
})
})
// @todo
// Convey("given a Response with an invalid `res.StatusCode` property", t, func() {
// // MockResponse.StatusCode = 10
// // APIGatewayProxyResponse := parseResponse(MockResponse)
// // Convey("it should return the proper `res.APIGatewayProxyResponse.StatusCode", func() {
// // So(APIGatewayProxyResponse.StatusCode, ShouldEqual, 500)
// // })
// })
Convey("given a Response with valid JSONifiable data in the `res.ResponseData` property", t, func() {
MockResponse.ResponseData = map[string]string{
"Hello": "World",
}
APIGatewayProxyResponse := parseResponse(MockResponse)
Convey("it should properly marshall the JSON and return it as a string in the `res.APIGatewayProxyResponse.Body`", func() {
So(APIGatewayProxyResponse.Body, ShouldEqual, "{\"Hello\":\"World\"}")
})
})
Convey("given a response with invalid JSON in the `res.ResponseData` property", t, func() {
MockResponse.ResponseData = "hello world"
APIGatewayProxyResponse := parseResponse(MockResponse)
Convey("it should return a string in the `res.APIGatewayProxyResponse.Body`", func() {
So(APIGatewayProxyResponse.Body, ShouldEqual, "\"hello world\"")
})
})
}
type MockHandler struct{}
func (handler MockHandler) Handle(req Request) interface{} {
res := Response{
StatusCode: 200,
ResponseData: map[string]interface{}{"hello": "world"},
}
return res
}
func TestGetLambdaHandler(t *testing.T) {
handler := MockHandler{}
Convey("given a MockHandler, should return a non-nil lambdaHandler function", t, func() {
So(GetLambdaHandler(handler), ShouldNotBeNil)
})
}