-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers_test.go
More file actions
130 lines (121 loc) · 3.06 KB
/
helpers_test.go
File metadata and controls
130 lines (121 loc) · 3.06 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
package hapi
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func newRequestWithURL(t *testing.T, url string) *http.Request {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Error("failed to create new request", err)
t.FailNow()
}
return request
}
func TestGetQueryParam(t *testing.T) {
testCases := []struct {
desc string
request *http.Request
key string
expectedResult string
expectedFound bool
}{
{
desc: "standard query param",
request: newRequestWithURL(t, "http://test.com/foo?bar=fubar"),
key: "bar",
expectedResult: "fubar",
expectedFound: true,
},
{
desc: "no key found",
request: newRequestWithURL(t, "http://test.com/foo?bar=fubar"),
key: "nokey",
expectedFound: false,
},
{
desc: "find first key",
request: newRequestWithURL(t, "http://test.com/foo?bar=first&bar=second"),
key: "bar",
expectedResult: "first",
expectedFound: true,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result, found := GetQueryParam(tc.request, tc.key)
assert.Equal(t, tc.expectedResult, result)
assert.Equal(t, tc.expectedFound, found)
})
}
}
type testStruct struct {
Text string
}
func TestUnmarshalBody(t *testing.T) {
testCases := []struct {
desc string
request *http.Request
opts []UnmarshalOption
expectedStruct testStruct
shouldError bool
expectedError string
}{
{
desc: "normal unmarshal",
request: &http.Request{
Body: ioutil.NopCloser(bytes.NewReader(json.RawMessage(`{"text":"hello world"}`))),
},
expectedStruct: testStruct{
Text: "hello world",
},
},
{
desc: "failed unmarshal",
request: &http.Request{
Body: ioutil.NopCloser(bytes.NewReader([]byte("bad json"))),
},
shouldError: true,
expectedError: "request body is not proper json: invalid character 'b' looking for beginning of value",
},
{
desc: "max bytes option success",
request: &http.Request{
Body: ioutil.NopCloser(bytes.NewReader(json.RawMessage(`{"text":"hello world"}`))),
},
opts: []UnmarshalOption{
WithMaxSize(nil, 100),
},
expectedStruct: testStruct{
Text: "hello world",
},
},
{
desc: "max bytes option failure",
request: &http.Request{
Body: ioutil.NopCloser(bytes.NewReader(json.RawMessage(`{"text":"hello world"}`))),
},
opts: []UnmarshalOption{
WithMaxSize(nil, 1),
},
shouldError: true,
expectedError: "request body is too large: http: request body too large",
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
var actualStruct testStruct
err := UnmarshalBody(tc.request, &actualStruct, tc.opts...)
if tc.shouldError && err == nil {
assert.Fail(t, "should have errored but didn't")
}
if err != nil {
assert.EqualError(t, err, tc.expectedError)
}
assert.Equal(t, tc.expectedStruct, actualStruct)
})
}
}