@@ -4,10 +4,14 @@ package events
4
4
5
5
import (
6
6
"encoding/json"
7
+ "errors"
7
8
"io/ioutil" //nolint: staticcheck
9
+ "net/http"
10
+ "strings"
8
11
"testing"
9
12
10
13
"github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/require"
11
15
)
12
16
13
17
func TestLambdaFunctionURLResponseMarshaling (t * testing.T ) {
@@ -55,3 +59,91 @@ func TestLambdaFunctionURLRequestMarshaling(t *testing.T) {
55
59
56
60
assert .JSONEq (t , string (inputJSON ), string (outputJSON ))
57
61
}
62
+
63
+ func TestLambdaFunctionURLStreamingResponseMarshaling (t * testing.T ) {
64
+ for _ , test := range []struct {
65
+ name string
66
+ response * LambdaFunctionURLStreamingResponse
67
+ expectedHead string
68
+ expectedBody string
69
+ }{
70
+ {
71
+ "empty" ,
72
+ & LambdaFunctionURLStreamingResponse {},
73
+ `{"statusCode":200}` ,
74
+ "" ,
75
+ },
76
+ {
77
+ "just the status code" ,
78
+ & LambdaFunctionURLStreamingResponse {
79
+ StatusCode : http .StatusTeapot ,
80
+ },
81
+ `{"statusCode":418}` ,
82
+ "" ,
83
+ },
84
+ {
85
+ "status and headers and cookies and body" ,
86
+ & LambdaFunctionURLStreamingResponse {
87
+ StatusCode : http .StatusTeapot ,
88
+ Headers : map [string ]string {"hello" : "world" },
89
+ Cookies : []string {"cookies" , "are" , "yummy" },
90
+ Body : strings .NewReader (`<html>Hello Hello</html>` ),
91
+ },
92
+ `{"statusCode":418, "headers":{"hello":"world"}, "cookies":["cookies","are","yummy"]}` ,
93
+ `<html>Hello Hello</html>` ,
94
+ },
95
+ } {
96
+ t .Run (test .name , func (t * testing.T ) {
97
+ response , err := ioutil .ReadAll (test .response )
98
+ require .NoError (t , err )
99
+ sep := "\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 "
100
+ responseParts := strings .Split (string (response ), sep )
101
+ require .Len (t , responseParts , 2 )
102
+ head := string (responseParts [0 ])
103
+ body := string (responseParts [1 ])
104
+ assert .JSONEq (t , test .expectedHead , head )
105
+ assert .Equal (t , test .expectedBody , body )
106
+ assert .NoError (t , test .response .Close ())
107
+ })
108
+ }
109
+ }
110
+
111
+ type readCloser struct {
112
+ closed bool
113
+ err error
114
+ reader * strings.Reader
115
+ }
116
+
117
+ func (r * readCloser ) Read (p []byte ) (int , error ) {
118
+ return r .reader .Read (p )
119
+ }
120
+
121
+ func (r * readCloser ) Close () error {
122
+ r .closed = true
123
+ return r .err
124
+ }
125
+
126
+ func TestLambdaFunctionURLStreamingResponsePropogatesInnerClose (t * testing.T ) {
127
+ for _ , test := range []struct {
128
+ name string
129
+ closer * readCloser
130
+ err error
131
+ }{
132
+ {
133
+ "closer no err" ,
134
+ & readCloser {},
135
+ nil ,
136
+ },
137
+ {
138
+ "closer with err" ,
139
+ & readCloser {err : errors .New ("yolo" )},
140
+ errors .New ("yolo" ),
141
+ },
142
+ } {
143
+ t .Run (test .name , func (t * testing.T ) {
144
+ response := & LambdaFunctionURLStreamingResponse {Body : test .closer }
145
+ assert .Equal (t , test .err , response .Close ())
146
+ assert .True (t , test .closer .closed )
147
+ })
148
+ }
149
+ }
0 commit comments