-
Notifications
You must be signed in to change notification settings - Fork 4
/
event_test.go
182 lines (173 loc) · 4.16 KB
/
event_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package agw
import (
"net/url"
"reflect"
"testing"
)
var ev = `
{
"resource": "/test1/{proxy+}",
"path": "/test1/test",
"httpMethod": "POST",
"headers": {
"Accept": "*/*",
"CloudFront-Forwarded-Proto": "https",
"Content-Type": "application/json"
},
"queryStringParameters": {
"k1": "v1",
"k2": "v2"
},
"pathParameters": {
"proxy": "test"
},
"stageVariables": {
"lbAlias": "current"
},
"requestContext": {
"path": "/test1/{proxy+}",
"accountId": "095615327118",
"resourceId": "ybki7l",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"cognitoIdentityId": null,
"apiKey": "test-invoke-api-key",
"cognitoAuthenticationType": null,
"userArn": "arn:aws:iam::095615327118:root",
"apiKeyId": "test-invoke-api-key-id",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_144)",
"accountId": "095615327118",
"caller": "095615327118",
"sourceIp": "test-invoke-source-ip",
"accessKey": "ASIAJTPDCBBJQKRD3FMQ",
"cognitoAuthenticationProvider": null,
"user": "095615327118"
},
"resourcePath": "/test1/{proxy+}",
"httpMethod": "POST",
"apiId": "uorto7w779"
},
"body": "{\"key1\":\"value1\"}",
"isBase64Encoded": false
}
`[1:]
func Test_apiGateParser_queryStringParameters(t *testing.T) {
var ev2 = `
{
"queryStringParameters": null
}
`
uv1 := make(url.Values)
uv1.Add("k1", "v1")
uv1.Add("k2", "v2")
type fields struct {
content []byte
}
tests := []struct {
name string
fields fields
want url.Values
}{
{"t1", fields{[]byte(ev)}, uv1},
{"t2", fields{[]byte(ev2)}, make(url.Values)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agp := &APIGateParser{
content: tt.fields.content,
}
if got := agp.QueryStringParameters(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("apiGateParser.queryStringParameters() = %v, want %v", got, tt.want)
}
})
}
}
func Test_apiGateParser_url(t *testing.T) {
type fields struct {
content []byte
}
tests := []struct {
name string
fields fields
want string
}{
{"t1", fields{[]byte(ev)}, "/test1/test?k1=v1&k2=v2"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agp := &APIGateParser{
content: tt.fields.content,
}
if got := agp.Url(); got != tt.want {
t.Errorf("apiGateParser.url() = %v, want %v", got, tt.want)
}
})
}
}
func Test_apiGateParser_body(t *testing.T) {
type fields struct {
content []byte
}
tests := []struct {
name string
fields fields
want []byte
}{
{"t1", fields{[]byte(ev)}, []byte("{\"key1\":\"value1\"}")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agp := &APIGateParser{
content: tt.fields.content,
}
if got := agp.Body(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("apiGateParser.body() = %v, want %v", got, tt.want)
}
})
}
}
func TestAPIGateParser_StageVariables(t *testing.T) {
var ev3 = `
{
"stageVariables": null
},`
type fields struct {
content []byte
}
tests := []struct {
name string
fields fields
want map[string]string
}{
{"t1", fields{[]byte(ev1)}, map[string]string{"lbAlias": "current"}},
{"t2", fields{[]byte(ev3)}, map[string]string{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
agp := &APIGateParser{
content: tt.fields.content,
}
if got := agp.StageVariables(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("APIGateParser.StageVariables() = %v, want %v", got, tt.want)
}
})
}
}
func Test_apiGateParser_headers(t *testing.T) {
name := "t1"
rawJSON := []byte(ev)
want := make(map[string]string)
want["Accept"] = "*/*"
want["CloudFront-Forwarded-Proto"] = "https"
want["Content-Type"] = "application/json"
t.Run(name, func(t *testing.T) {
agp := &APIGateParser{
content: rawJSON,
}
if got := agp.Headers(); !reflect.DeepEqual(got, want) {
t.Errorf("apiGateParser.body() = %v, want %v", got, want)
}
})
}