-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_test.go
155 lines (142 loc) · 3.29 KB
/
lambda_test.go
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
package main
import (
"bufio"
"fmt"
"go/token"
"io"
"net/http"
"reflect"
"strings"
"testing"
)
type respTest struct {
Raw string
Resp http.Response
Body string
}
func dummyReq(method string) *http.Request {
return &http.Request{Method: method}
}
func dummyReq11(method string) *http.Request {
return &http.Request{Method: method, Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1}
}
var respTests = []respTest{
{
"HTTP/1.1 200 OK\r\n" +
"\r\n" +
"{\"statusCode\":200, \"body\": \"\"}\n",
http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{"Content-Length": []string{"0"}},
Request: dummyReq("POST"),
Close: true,
ContentLength: 0,
},
"",
},
{
"HTTP/1.1 200 OK\r\n" +
"\r\n" +
`{"statusCode":200,"headers":{},"multiValueHeaders":null,"body":"","cookies":[]}
`,
http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{"Content-Length": []string{"0"}},
Request: dummyReq("POST"),
Close: true,
ContentLength: 0,
},
"",
},
{
"HTTP/1.1 200 OK\r\n" +
"\r\n" +
`{"statusCode":200,"headers":{},"multiValueHeaders":null,"body":"<html><head></head><body><h1>something</h1></body></html>","cookies":[]}
`,
http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Content-Length": []string{"57"},
},
Request: dummyReq("POST"),
Close: true,
ContentLength: 57,
},
"<html><head></head><body><h1>something</h1></body></html>",
},
}
func TestModifyResponse(t *testing.T) {
// body io.ReadCloser
for i, tt := range respTests {
resp, err := http.ReadResponse(bufio.NewReader(strings.NewReader(tt.Raw)), tt.Resp.Request)
if err != nil {
t.Errorf("#%d: %v", i, err)
continue
}
if transformLambdaBody(resp) != nil {
t.Errorf("#%d: %v", i, err)
continue
}
rbody := resp.Body
resp.Body = nil
diff(t, fmt.Sprintf("#%d Response", i), resp, &tt.Resp)
var bout strings.Builder
if rbody != nil {
_, err = io.Copy(&bout, rbody)
if err != nil {
t.Errorf("#%d: %v", i, err)
continue
}
rbody.Close()
}
body := bout.String()
if body != tt.Body {
t.Errorf("#%d: Body = %q want %q", i, body, tt.Body)
}
}
}
func TestWriteResponse(t *testing.T) {
for i, tt := range respTests {
resp, err := http.ReadResponse(bufio.NewReader(strings.NewReader(tt.Raw)), tt.Resp.Request)
if err != nil {
t.Errorf("#%d: %v", i, err)
continue
}
err = resp.Write(io.Discard)
if err != nil {
t.Errorf("#%d: %v", i, err)
continue
}
}
}
func diff(t *testing.T, prefix string, have, want any) {
t.Helper()
hv := reflect.ValueOf(have).Elem()
wv := reflect.ValueOf(want).Elem()
if hv.Type() != wv.Type() {
t.Errorf("%s: type mismatch %v want %v", prefix, hv.Type(), wv.Type())
}
for i := 0; i < hv.NumField(); i++ {
name := hv.Type().Field(i).Name
if !token.IsExported(name) {
continue
}
hf := hv.Field(i).Interface()
wf := wv.Field(i).Interface()
if !reflect.DeepEqual(hf, wf) {
t.Errorf("%s: %s = %v want %v", prefix, name, hf, wf)
}
}
}