1
1
package core_test
2
2
3
3
import (
4
+ "context"
4
5
"encoding/base64"
5
6
"io/ioutil"
6
7
"math/rand"
7
8
"os"
8
9
9
10
"github.com/aws/aws-lambda-go/events"
11
+ "github.com/aws/aws-lambda-go/lambdacontext"
10
12
"github.com/awslabs/aws-lambda-go-api-proxy/core"
11
13
12
14
. "github.com/onsi/ginkgo"
@@ -18,14 +20,15 @@ var _ = Describe("RequestAccessor tests", func() {
18
20
accessor := core.RequestAccessor {}
19
21
basicRequest := getProxyRequest ("/hello" , "GET" )
20
22
It ("Correctly converts a basic event" , func () {
21
- httpReq , err := accessor .ProxyEventToHTTPRequest ( basicRequest )
23
+ httpReq , err := accessor .EventToRequestWithContext ( context . Background (), basicRequest )
22
24
Expect (err ).To (BeNil ())
23
25
Expect ("/hello" ).To (Equal (httpReq .URL .Path ))
24
26
Expect ("GET" ).To (Equal (httpReq .Method ))
25
27
})
26
28
27
29
basicRequest = getProxyRequest ("/hello" , "get" )
28
30
It ("Converts method to uppercase" , func () {
31
+ // calling old method to verify reverse compatibility
29
32
httpReq , err := accessor .ProxyEventToHTTPRequest (basicRequest )
30
33
Expect (err ).To (BeNil ())
31
34
Expect ("/hello" ).To (Equal (httpReq .URL .Path ))
@@ -45,7 +48,7 @@ var _ = Describe("RequestAccessor tests", func() {
45
48
binaryRequest .IsBase64Encoded = true
46
49
47
50
It ("Decodes a base64 encoded body" , func () {
48
- httpReq , err := accessor .ProxyEventToHTTPRequest ( binaryRequest )
51
+ httpReq , err := accessor .EventToRequestWithContext ( context . Background (), binaryRequest )
49
52
Expect (err ).To (BeNil ())
50
53
Expect ("/hello" ).To (Equal (httpReq .URL .Path ))
51
54
Expect ("POST" ).To (Equal (httpReq .Method ))
@@ -63,7 +66,7 @@ var _ = Describe("RequestAccessor tests", func() {
63
66
"world" : {"2" , "3" },
64
67
}
65
68
It ("Populates query string correctly" , func () {
66
- httpReq , err := accessor .ProxyEventToHTTPRequest ( qsRequest )
69
+ httpReq , err := accessor .EventToRequestWithContext ( context . Background (), qsRequest )
67
70
Expect (err ).To (BeNil ())
68
71
Expect ("/hello" ).To (Equal (httpReq .URL .Path ))
69
72
Expect ("GET" ).To (Equal (httpReq .Method ))
@@ -83,7 +86,8 @@ var _ = Describe("RequestAccessor tests", func() {
83
86
84
87
It ("Stips the base path correct" , func () {
85
88
accessor .StripBasePath ("app1" )
86
- httpReq , err := accessor .ProxyEventToHTTPRequest (basePathRequest )
89
+ httpReq , err := accessor .EventToRequestWithContext (context .Background (), basePathRequest )
90
+
87
91
Expect (err ).To (BeNil ())
88
92
Expect ("/orders" ).To (Equal (httpReq .URL .Path ))
89
93
})
@@ -92,6 +96,7 @@ var _ = Describe("RequestAccessor tests", func() {
92
96
contextRequest .RequestContext = getRequestContext ()
93
97
94
98
It ("Populates context header correctly" , func () {
99
+ // calling old method to verify reverse compatibility
95
100
httpReq , err := accessor .ProxyEventToHTTPRequest (contextRequest )
96
101
Expect (err ).To (BeNil ())
97
102
Expect (2 ).To (Equal (len (httpReq .Header )))
@@ -123,16 +128,49 @@ var _ = Describe("RequestAccessor tests", func() {
123
128
contextRequest .RequestContext = getRequestContext ()
124
129
125
130
accessor := core.RequestAccessor {}
131
+ // calling old method to verify reverse compatibility
126
132
httpReq , err := accessor .ProxyEventToHTTPRequest (contextRequest )
127
133
Expect (err ).To (BeNil ())
128
134
129
- context , err := accessor .GetAPIGatewayContext (httpReq )
135
+ headerContext , err := accessor .GetAPIGatewayContext (httpReq )
136
+ Expect (err ).To (BeNil ())
137
+ Expect (headerContext ).ToNot (BeNil ())
138
+ Expect ("x" ).To (Equal (headerContext .AccountID ))
139
+ Expect ("x" ).To (Equal (headerContext .RequestID ))
140
+ Expect ("x" ).To (Equal (headerContext .APIID ))
141
+ proxyContext , ok := core .GetAPIGatewayContextFromContext (httpReq .Context ())
142
+ // should fail because using header proxy method
143
+ Expect (ok ).To (BeFalse ())
144
+
145
+ httpReq , err = accessor .EventToRequestWithContext (context .Background (), contextRequest )
130
146
Expect (err ).To (BeNil ())
131
- Expect (context ).ToNot (BeNil ())
132
- Expect ("x" ).To (Equal (context .AccountID ))
133
- Expect ("x" ).To (Equal (context .RequestID ))
134
- Expect ("x" ).To (Equal (context .APIID ))
135
- Expect ("prod" ).To (Equal (context .Stage ))
147
+ proxyContext , ok = core .GetAPIGatewayContextFromContext (httpReq .Context ())
148
+ Expect (ok ).To (BeTrue ())
149
+ Expect ("x" ).To (Equal (proxyContext .APIID ))
150
+ Expect ("x" ).To (Equal (proxyContext .RequestID ))
151
+ Expect ("x" ).To (Equal (proxyContext .APIID ))
152
+ Expect ("prod" ).To (Equal (proxyContext .Stage ))
153
+ runtimeContext , ok := core .GetRuntimeContextFromContext (httpReq .Context ())
154
+ Expect (ok ).To (BeTrue ())
155
+ Expect (runtimeContext ).To (BeNil ())
156
+
157
+ lambdaContext := lambdacontext .NewContext (context .Background (), & lambdacontext.LambdaContext {AwsRequestID : "abc123" })
158
+ httpReq , err = accessor .EventToRequestWithContext (lambdaContext , contextRequest )
159
+ Expect (err ).To (BeNil ())
160
+
161
+ headerContext , err = accessor .GetAPIGatewayContext (httpReq )
162
+ // should fail as new context method doesn't populate headers
163
+ Expect (err ).ToNot (BeNil ())
164
+ proxyContext , ok = core .GetAPIGatewayContextFromContext (httpReq .Context ())
165
+ Expect (ok ).To (BeTrue ())
166
+ Expect ("x" ).To (Equal (proxyContext .APIID ))
167
+ Expect ("x" ).To (Equal (proxyContext .RequestID ))
168
+ Expect ("x" ).To (Equal (proxyContext .APIID ))
169
+ Expect ("prod" ).To (Equal (proxyContext .Stage ))
170
+ runtimeContext , ok = core .GetRuntimeContextFromContext (httpReq .Context ())
171
+ Expect (ok ).To (BeTrue ())
172
+ Expect (runtimeContext ).ToNot (BeNil ())
173
+ Expect ("abc123" ).To (Equal (runtimeContext .AwsRequestID ))
136
174
})
137
175
138
176
It ("Populates stage variables correctly" , func () {
@@ -150,9 +188,29 @@ var _ = Describe("RequestAccessor tests", func() {
150
188
Expect (stageVars ["var2" ]).ToNot (BeNil ())
151
189
Expect ("value1" ).To (Equal (stageVars ["var1" ]))
152
190
Expect ("value2" ).To (Equal (stageVars ["var2" ]))
191
+
192
+ stageVars , ok := core .GetStageVarsFromContext (httpReq .Context ())
193
+ // not present in context
194
+ Expect (ok ).To (BeFalse ())
195
+
196
+ httpReq , err = accessor .EventToRequestWithContext (context .Background (), varsRequest )
197
+ Expect (err ).To (BeNil ())
198
+
199
+ stageVars , err = accessor .GetAPIGatewayStageVars (httpReq )
200
+ // should not be in headers
201
+ Expect (err ).ToNot (BeNil ())
202
+
203
+ stageVars , ok = core .GetStageVarsFromContext (httpReq .Context ())
204
+ Expect (ok ).To (BeTrue ())
205
+ Expect (2 ).To (Equal (len (stageVars )))
206
+ Expect (stageVars ["var1" ]).ToNot (BeNil ())
207
+ Expect (stageVars ["var2" ]).ToNot (BeNil ())
208
+ Expect ("value1" ).To (Equal (stageVars ["var1" ]))
209
+ Expect ("value2" ).To (Equal (stageVars ["var2" ]))
153
210
})
154
211
155
212
It ("Populates the default hostname correctly" , func () {
213
+
156
214
basicRequest := getProxyRequest ("orders" , "GET" )
157
215
accessor := core.RequestAccessor {}
158
216
httpReq , err := accessor .ProxyEventToHTTPRequest (basicRequest )
@@ -167,7 +225,7 @@ var _ = Describe("RequestAccessor tests", func() {
167
225
os .Setenv (core .CustomHostVariable , myCustomHost )
168
226
basicRequest := getProxyRequest ("orders" , "GET" )
169
227
accessor := core.RequestAccessor {}
170
- httpReq , err := accessor .ProxyEventToHTTPRequest ( basicRequest )
228
+ httpReq , err := accessor .EventToRequestWithContext ( context . Background (), basicRequest )
171
229
Expect (err ).To (BeNil ())
172
230
173
231
Expect (myCustomHost ).To (Equal ("http://" + httpReq .Host ))
@@ -180,7 +238,7 @@ var _ = Describe("RequestAccessor tests", func() {
180
238
os .Setenv (core .CustomHostVariable , myCustomHost + "/" )
181
239
basicRequest := getProxyRequest ("orders" , "GET" )
182
240
accessor := core.RequestAccessor {}
183
- httpReq , err := accessor .ProxyEventToHTTPRequest ( basicRequest )
241
+ httpReq , err := accessor .EventToRequestWithContext ( context . Background (), basicRequest )
184
242
Expect (err ).To (BeNil ())
185
243
186
244
Expect (myCustomHost ).To (Equal ("http://" + httpReq .Host ))
0 commit comments