-
Notifications
You must be signed in to change notification settings - Fork 15
/
decoder_test.go
157 lines (142 loc) · 4.87 KB
/
decoder_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
package lmdrouter
import (
"errors"
"net/http"
"testing"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/jgroeneveld/trial/assert"
)
type stringAliasExample string
const aliasExample stringAliasExample = "world"
func Test_UnmarshalRequest(t *testing.T) {
t.Run("valid path&query input", func(t *testing.T) {
var input mockListRequest
err := UnmarshalRequest(
events.APIGatewayProxyRequest{
PathParameters: map[string]string{
"id": "fake-scan-id",
},
QueryStringParameters: map[string]string{
"page": "2",
"page_size": "30",
"const": "two",
"bool": "true",
"pbool1": "0",
"time": "2021-11-01T11:11:11.000Z",
"alias": "hello",
"alias_ptr": "world",
"commaSplit": "one,two,three",
},
MultiValueQueryStringParameters: map[string][]string{
"terms": []string{"one", "two"},
"numbers": []string{"1.2", "3.5", "666.666"},
},
Headers: map[string]string{
"Accept-Language": "en-us",
},
MultiValueHeaders: map[string][]string{
"Accept-Encoding": []string{"gzip", "deflate"},
},
},
false,
&input,
)
assert.Equal(t, nil, err, "Error must be nil")
assert.Equal(t, "fake-scan-id", input.ID, "ID must be parsed from path")
assert.Equal(t, int64(2), input.Page, "Page must be parsed from query")
assert.Equal(t, int64(30), input.PageSize, "PageSize must be parsed from query")
assert.Equal(t, "en-us", input.Language, "Language must be parsed from headers")
assert.Equal(t, mockConstTwo, input.Const, "Const must be parsed from query")
assert.True(t, input.Bool, "Bool must be true")
assert.NotNil(t, input.PBoolOne, "PBoolOne must not be nil")
assert.False(t, *input.PBoolOne, "PBoolOne must be *false")
assert.NotNil(t, input.Time, "Time must not be nil")
assert.Equal(t, input.Time.Format(time.RFC3339), "2021-11-01T11:11:11Z")
assert.Equal(t, input.Alias, stringAliasExample("hello"))
assert.NotNil(t, input.AliasPtr)
assert.Equal(t, *input.AliasPtr, aliasExample)
assert.DeepEqual(t, []Number{numberOne, numberTwo, numberThree}, input.CommaSplit, "CommaSplit must have 2 items")
assert.Equal(t, (*bool)(nil), input.PBoolTwo, "PBoolTwo must be nil")
assert.DeepEqual(t, []string{"one", "two"}, input.Terms, "Terms must be parsed from multiple query params")
assert.DeepEqual(t, []float64{1.2, 3.5, 666.666}, input.Numbers, "Numbers must be parsed from multiple query params")
assert.DeepEqual(t, []string{"gzip", "deflate"}, input.Encoding, "Encoding must be parsed from multiple header params")
})
t.Run("invalid path&query input", func(t *testing.T) {
var input mockListRequest
err := UnmarshalRequest(
events.APIGatewayProxyRequest{
PathParameters: map[string]string{
"id": "fake-scan-id",
},
QueryStringParameters: map[string]string{
"page": "abcd",
},
},
false,
&input,
)
assert.NotEqual(t, nil, err, "Error must not be nil")
var httpErr HTTPError
ok := errors.As(err, &httpErr)
assert.True(t, ok, "Error must be an HTTPError")
assert.Equal(t, http.StatusBadRequest, httpErr.Code, "Error code must be 400")
})
fakeDate := time.Date(2020, 3, 23, 11, 33, 0, 0, time.UTC)
t.Run("valid body input, not base64", func(t *testing.T) {
var input mockPostRequest
err := UnmarshalRequest(
events.APIGatewayProxyRequest{
IsBase64Encoded: false,
PathParameters: map[string]string{
"id": "bla",
},
Body: `{"name":"Fake Post","date":"2020-03-23T11:33:00Z"}`,
},
true,
&input,
)
assert.Equal(t, nil, err, "Error must be nil")
assert.Equal(t, "bla", input.ID, "ID must be parsed from path parameters")
assert.Equal(t, "Fake Post", input.Name, "Name must be parsed from body")
assert.Equal(t, fakeDate, input.Date, "Date must be parsed from body")
})
t.Run("invalid body input, not base64", func(t *testing.T) {
var input mockPostRequest
err := UnmarshalRequest(
events.APIGatewayProxyRequest{
IsBase64Encoded: false,
Body: `this is not JSON`,
},
true,
&input,
)
assert.NotEqual(t, nil, err, "Error must not be nil")
})
t.Run("valid body input, base64", func(t *testing.T) {
var input mockPostRequest
err := UnmarshalRequest(
events.APIGatewayProxyRequest{
IsBase64Encoded: true,
Body: "eyJuYW1lIjoiRmFrZSBQb3N0IiwiZGF0ZSI6IjIwMjAtMDMtMjNUMTE6MzM6MDBaIn0=",
},
true,
&input,
)
assert.Equal(t, nil, err, "Error must be nil")
assert.Equal(t, "Fake Post", input.Name, "Name must be parsed from body")
assert.Equal(t, fakeDate, input.Date, "Date must be parsed from body")
})
t.Run("invalid body input, base64", func(t *testing.T) {
var input mockPostRequest
err := UnmarshalRequest(
events.APIGatewayProxyRequest{
IsBase64Encoded: true,
Body: "dGhpcyBpcyBub3QgSlNPTg==",
},
true,
&input,
)
assert.NotEqual(t, nil, err, "Error must not be nil")
})
}