@@ -4,11 +4,15 @@ package events
44
55import (
66 "encoding/json"
7+ "errors"
78 "io/ioutil" //nolint: staticcheck
9+ "net/http"
10+ "strings"
811 "testing"
912
1013 "github.com/aws/aws-lambda-go/events/test"
1114 "github.com/stretchr/testify/assert"
15+ "github.com/stretchr/testify/require"
1216)
1317
1418func TestApiGatewayRequestMarshaling (t * testing.T ) {
@@ -83,6 +87,80 @@ func TestApiGatewayResponseMalformedJson(t *testing.T) {
8387 test .TestMalformedJson (t , APIGatewayProxyResponse {})
8488}
8589
90+ func TestAPIGatewayProxyStreamingResponseMarshaling (t * testing.T ) {
91+ for _ , test := range []struct {
92+ name string
93+ response * APIGatewayProxyStreamingResponse
94+ expectedHead string
95+ expectedBody string
96+ }{
97+ {
98+ "empty" ,
99+ & APIGatewayProxyStreamingResponse {},
100+ `{}` ,
101+ "" ,
102+ },
103+ {
104+ "just the status code" ,
105+ & APIGatewayProxyStreamingResponse {
106+ StatusCode : http .StatusTeapot ,
107+ },
108+ `{"statusCode":418}` ,
109+ "" ,
110+ },
111+ {
112+ "status and headers and cookies and body" ,
113+ & APIGatewayProxyStreamingResponse {
114+ StatusCode : http .StatusTeapot ,
115+ Headers : map [string ]string {"hello" : "world" },
116+ MultiValueHeaders : map [string ][]string {"hi" : {"1" , "2" }},
117+ Cookies : []string {"cookies" , "are" , "yummy" },
118+ Body : strings .NewReader (`<html>Hello Hello</html>` ),
119+ },
120+ `{"statusCode":418, "headers":{"hello":"world"}, "multiValueHeaders":{"hi":["1","2"]}, "cookies":["cookies","are","yummy"]}` ,
121+ `<html>Hello Hello</html>` ,
122+ },
123+ } {
124+ t .Run (test .name , func (t * testing.T ) {
125+ response , err := ioutil .ReadAll (test .response )
126+ require .NoError (t , err )
127+ sep := "\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
128+ responseParts := strings .Split (string (response ), sep )
129+ require .Len (t , responseParts , 2 )
130+ head := string (responseParts [0 ])
131+ body := string (responseParts [1 ])
132+ assert .JSONEq (t , test .expectedHead , head )
133+ assert .Equal (t , test .expectedBody , body )
134+ assert .NoError (t , test .response .Close ())
135+ })
136+ }
137+ }
138+
139+ func TestAPIGatewayProxyStreamingResponsePropogatesInnerClose (t * testing.T ) {
140+ for _ , test := range []struct {
141+ name string
142+ closer * readCloser
143+ err error
144+ }{
145+ {
146+ "closer no err" ,
147+ & readCloser {},
148+ nil ,
149+ },
150+ {
151+ "closer with err" ,
152+ & readCloser {err : errors .New ("yolo" )},
153+ errors .New ("yolo" ),
154+ },
155+ } {
156+ t .Run (test .name , func (t * testing.T ) {
157+ response := & APIGatewayProxyStreamingResponse {Body : test .closer }
158+ assert .Equal (t , test .err , response .Close ())
159+ assert .True (t , test .closer .closed )
160+ })
161+ }
162+ }
163+
86164func TestApiGatewayCustomAuthorizerRequestMarshaling (t * testing.T ) {
87165
88166 // read json from file
0 commit comments