-
Notifications
You must be signed in to change notification settings - Fork 576
Expand file tree
/
Copy pathlambda_function_urls_test.go
More file actions
170 lines (147 loc) · 4.29 KB
/
lambda_function_urls_test.go
File metadata and controls
170 lines (147 loc) · 4.29 KB
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
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
package events
import (
"encoding/json"
"errors"
"io/ioutil" //nolint: staticcheck
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLambdaFunctionURLResponseMarshaling(t *testing.T) {
// read json from file
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-response.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}
// de-serialize into Go object
var inputEvent LambdaFunctionURLResponse
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
// serialize to json
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}
assert.JSONEq(t, string(inputJSON), string(outputJSON))
}
func TestLambdaFunctionURLRequestMarshaling(t *testing.T) {
// read json from file
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-request.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}
// de-serialize into Go object
var inputEvent LambdaFunctionURLRequest
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
// serialize to json
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}
assert.JSONEq(t, string(inputJSON), string(outputJSON))
}
func TestLambdaFunctionURLResponseMultiValueHeadersMarshaling(t *testing.T) {
inputJSON, err := ioutil.ReadFile("./testdata/lambda-urls-response-multi-value-headers.json")
if err != nil {
t.Errorf("could not open test file. details: %v", err)
}
var inputEvent LambdaFunctionURLResponse
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
t.Errorf("could not unmarshal event. details: %v", err)
}
require.Equal(t, []string{"a=1; Path=/", "b=2; Path=/"}, inputEvent.MultiValueHeaders["Set-Cookie"])
outputJSON, err := json.Marshal(inputEvent)
if err != nil {
t.Errorf("could not marshal event. details: %v", err)
}
assert.JSONEq(t, string(inputJSON), string(outputJSON))
}
func TestLambdaFunctionURLStreamingResponseMarshaling(t *testing.T) {
for _, test := range []struct {
name string
response *LambdaFunctionURLStreamingResponse
expectedHead string
expectedBody string
}{
{
"empty",
&LambdaFunctionURLStreamingResponse{},
`{"statusCode":200}`,
"",
},
{
"just the status code",
&LambdaFunctionURLStreamingResponse{
StatusCode: http.StatusTeapot,
},
`{"statusCode":418}`,
"",
},
{
"status and headers and cookies and body",
&LambdaFunctionURLStreamingResponse{
StatusCode: http.StatusTeapot,
Headers: map[string]string{"hello": "world"},
Cookies: []string{"cookies", "are", "yummy"},
Body: strings.NewReader(`<html>Hello Hello</html>`),
},
`{"statusCode":418, "headers":{"hello":"world"}, "cookies":["cookies","are","yummy"]}`,
`<html>Hello Hello</html>`,
},
} {
t.Run(test.name, func(t *testing.T) {
response, err := ioutil.ReadAll(test.response)
require.NoError(t, err)
sep := "\x00\x00\x00\x00\x00\x00\x00\x00"
responseParts := strings.Split(string(response), sep)
require.Len(t, responseParts, 2)
head := string(responseParts[0])
body := string(responseParts[1])
assert.JSONEq(t, test.expectedHead, head)
assert.Equal(t, test.expectedBody, body)
assert.NoError(t, test.response.Close())
})
}
}
type readCloser struct {
closed bool
err error
reader *strings.Reader
}
func (r *readCloser) Read(p []byte) (int, error) {
return r.reader.Read(p)
}
func (r *readCloser) Close() error {
r.closed = true
return r.err
}
func TestLambdaFunctionURLStreamingResponsePropogatesInnerClose(t *testing.T) {
for _, test := range []struct {
name string
closer *readCloser
err error
}{
{
"closer no err",
&readCloser{},
nil,
},
{
"closer with err",
&readCloser{err: errors.New("yolo")},
errors.New("yolo"),
},
} {
t.Run(test.name, func(t *testing.T) {
response := &LambdaFunctionURLStreamingResponse{Body: test.closer}
assert.Equal(t, test.err, response.Close())
assert.True(t, test.closer.closed)
})
}
}