Skip to content

Commit 9e2b352

Browse files
dirkniliusDirk Niliusbmoffatt
authored
Add API Gateway V2 custom authorizer simple response type (#386)
* add API Gateway Custom Authorizer V2 simple response type * set omitempty to APIGatewayV2CustomAuthorizerSimpleResponse:Context and add test cases * add missing test case to APIGatewayV2CustomAuthorizerSimpleResponse:Context * pin range variable * make test input more readable Co-authored-by: Dirk Nilius <[email protected]> Co-authored-by: Bryan Moffatt <[email protected]>
1 parent 2442858 commit 9e2b352

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

events/apigw.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ type APIGatewayCustomAuthorizerResponse struct {
285285
UsageIdentifierKey string `json:"usageIdentifierKey,omitempty"`
286286
}
287287

288+
// APIGatewayV2CustomAuthorizerSimpleResponse represents the simple format of an API Gateway V2 authorization response.
289+
type APIGatewayV2CustomAuthorizerSimpleResponse struct {
290+
IsAuthorized bool `json:"isAuthorized"`
291+
Context map[string]interface{} `json:"context,omitempty"`
292+
}
293+
288294
// APIGatewayCustomAuthorizerPolicy represents an IAM policy
289295
type APIGatewayCustomAuthorizerPolicy struct {
290296
Version string

events/apigw_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,67 @@ func TestApiGatewayCustomAuthorizerResponseMarshaling(t *testing.T) {
177177
assert.JSONEq(t, string(inputJSON), string(outputJSON))
178178
}
179179

180+
func TestAPIGatewayV2CustomAuthorizerSimpleResponseMarshalling(t *testing.T) {
181+
var tests = []struct {
182+
desc string
183+
in APIGatewayV2CustomAuthorizerSimpleResponse
184+
out string
185+
}{
186+
{
187+
"defaults",
188+
APIGatewayV2CustomAuthorizerSimpleResponse{},
189+
`{"isAuthorized":false}`,
190+
},
191+
{
192+
"without context",
193+
APIGatewayV2CustomAuthorizerSimpleResponse{IsAuthorized: true},
194+
`{"isAuthorized":true}`,
195+
},
196+
{
197+
"context is nil",
198+
APIGatewayV2CustomAuthorizerSimpleResponse{Context: nil},
199+
`{"isAuthorized":false}`,
200+
},
201+
{
202+
"context is empty",
203+
APIGatewayV2CustomAuthorizerSimpleResponse{Context: map[string]interface{}{}},
204+
`{"isAuthorized":false}`,
205+
},
206+
{
207+
"context with basic types",
208+
APIGatewayV2CustomAuthorizerSimpleResponse{Context: map[string]interface{}{"string": "value", "bool": true, "number": 1234}},
209+
`{"isAuthorized":false,"context":{"string":"value","bool":true,"number":1234}}`,
210+
},
211+
{
212+
"context with array",
213+
APIGatewayV2CustomAuthorizerSimpleResponse{Context: map[string]interface{}{"array": []string{"value1", "value2"}}},
214+
`{"isAuthorized":false,"context":{"array":["value1","value2"]}}`,
215+
},
216+
{
217+
"context with map",
218+
APIGatewayV2CustomAuthorizerSimpleResponse{Context: map[string]interface{}{"map": map[string]string{"key": "value"}}},
219+
`{"isAuthorized":false,"context":{"map":{"key":"value"}}}`,
220+
},
221+
{
222+
"context with nil value",
223+
APIGatewayV2CustomAuthorizerSimpleResponse{Context: map[string]interface{}{"nil": nil}},
224+
`{"isAuthorized":false,"context":{"nil":null}}`,
225+
},
226+
}
227+
228+
for _, tt := range tests {
229+
tt := tt
230+
t.Run(tt.desc, func(t *testing.T) {
231+
outputJSON, err := json.Marshal(tt.in)
232+
if err != nil {
233+
t.Errorf("could not marshal event. details: %v", err)
234+
}
235+
236+
assert.JSONEq(t, tt.out, string(outputJSON))
237+
})
238+
}
239+
}
240+
180241
func TestApiGatewayCustomAuthorizerResponseMalformedJson(t *testing.T) {
181242
test.TestMalformedJson(t, APIGatewayCustomAuthorizerResponse{})
182243
}

0 commit comments

Comments
 (0)