-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
130 lines (104 loc) · 2.76 KB
/
helpers_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
package golden
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_helpers_headers2Lines(t *testing.T) {
// --- Given ---
hs := http.Header{}
hs.Add("Authorization", "Bearer token")
hs.Add("Custom-Header", "val0")
hs.Add("Custom-Header", "val1")
// --- When ---
lns := headers2Lines(t, hs)
// --- Then ---
assert.Len(t, lns, 3)
assert.Exactly(t, "Authorization: Bearer token", lns[0])
assert.Exactly(t, "Custom-Header: val0", lns[1])
assert.Exactly(t, "Custom-Header: val1", lns[2])
}
func Test_helpers_headers2Lines_emptyHeaders(t *testing.T) {
// --- Given ---
hs := http.Header{}
// --- When ---
lns := headers2Lines(t, hs)
// --- Then ---
assert.Len(t, lns, 0)
}
func Test_helpers_lines2Headers(t *testing.T) {
// --- Given ---
lns := []string{
"Authorization: Bearer token",
"Custom-Header: val0",
"Custom-Header: val1",
}
// --- When ---
hs := lines2Headers(t, lns...)
// --- Then ---
assert.Len(t, hs, 2)
assert.Contains(t, hs, "Authorization")
assert.Contains(t, hs, "Custom-Header")
assert.Len(t, hs.Values("Authorization"), 1)
assert.Len(t, hs.Values("Custom-Header"), 2)
assert.Exactly(t, "Bearer token", hs.Get("Authorization"))
assert.Exactly(t, "val0", hs.Values("Custom-Header")[0])
assert.Exactly(t, "val1", hs.Values("Custom-Header")[1])
}
func Test_helpers_readBody(t *testing.T) {
// --- Given ---
body := "{\n \"key2\": \"val2\"\n}\n"
req := httptest.NewRequest(
http.MethodPost,
"/some/path",
strings.NewReader(body),
)
// --- When ---
got, rc := readBody(t, req.Body)
// --- Then ---
assert.Exactly(t, body, string(got))
// Make sure you can still read form the request body.
reqBody, _ := ioutil.ReadAll(rc)
assert.Exactly(t, body, string(reqBody))
}
func Test_helpers_Map(t *testing.T) {
// --- When ---
data := make(Map).Add("key1", "val1").Add("key2", 2)
// --- Then ---
exp := map[string]interface{}{
"key1": "val1",
"key2": 2,
}
assert.Exactly(t, exp, map[string]interface{}(data))
}
func Test_helpers_unmarshalBody_JSON(t *testing.T) {
// --- Given ---
data := make(map[string]interface{})
// --- When ---
unmarshalBody(t, TypeJSON, `{"key1":"val1","key2":2}`, &data)
// --- Then ---
exp := map[string]interface{}{
"key1": "val1",
"key2": 2.0,
}
assert.Exactly(t, exp, data)
}
func Test_helpers_unmarshalBody_Text(t *testing.T) {
// --- Given ---
var m string
// --- When ---
unmarshalBody(t, TypeText, "Line 1\nLine 2", &m)
// --- Then ---
assert.Exactly(t, "Line 1\nLine 2", m)
}
func Test_helpers_unmarshalBody_Bytes(t *testing.T) {
// --- Given ---
m := make([]byte, 13)
// --- When ---
unmarshalBody(t, TypeText, "Line 1\nLine 2", &m)
// --- Then ---
assert.Exactly(t, []byte("Line 1\nLine 2"), m)
}