-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathheader_test.go
114 lines (105 loc) · 2.79 KB
/
header_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
package postman
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHeaderListMarshalJSON(t *testing.T) {
cases := []struct {
scenario string
headerList HeaderList
expectedOutput string
}{
{
"Successfully marshalling a HeaderList",
HeaderList{
Headers: []*Header{
{
Key: "Content-Type",
Value: "application/json",
Description: "The content type",
},
{
Key: "Authorization",
Value: "Bearer a-bearer-token",
Description: "The Bearer token",
},
},
},
"[{\"key\":\"Content-Type\",\"value\":\"application/json\",\"description\":\"The content type\"},{\"key\":\"Authorization\",\"value\":\"Bearer a-bearer-token\",\"description\":\"The Bearer token\"}]",
},
}
for _, tc := range cases {
bytes, _ := tc.headerList.MarshalJSON()
assert.Equal(t, tc.expectedOutput, string(bytes), tc.scenario)
}
}
func TestHeaderListUnmarshalJSON(t *testing.T) {
cases := []struct {
scenario string
bytes []byte
expectedHeaderList HeaderList
expectedError error
}{
{
"Successfully unmarshalling a HeaderList from a string",
[]byte("\"Content-Type: application/json\nAuthorization: Bearer a-bearer-token\n\""),
HeaderList{
Headers: []*Header{
{
Key: "Content-Type",
Value: "application/json",
},
{
Key: "Authorization",
Value: "Bearer a-bearer-token",
},
},
},
nil,
},
{
"Successfully unmarshalling a HeaderList from an empty slice of bytes",
make([]byte, 0),
HeaderList{},
nil,
},
{
"Successfully unmarshalling a HeaderList from an array of objects",
[]byte("[{\"key\": \"Content-Type\",\"value\": \"application\\/json\",\"description\": \"The content type\"},{\"key\": \"Authorization\",\"value\": \"Bearer a-bearer-token\",\"description\": \"The Bearer token\"}]"),
HeaderList{
Headers: []*Header{
{
Key: "Content-Type",
Value: "application/json",
Description: "The content type",
},
{
Key: "Authorization",
Value: "Bearer a-bearer-token",
Description: "The Bearer token",
},
},
},
nil,
},
{
"Failed to unmarshal a HeaderList because of an invalid header",
[]byte("\"Content-Type\n\""),
HeaderList{},
errors.New("invalid header, missing key or value: Content-Type"),
},
{
"Failed to unmarshal a HeaderList because of an unsupported type",
[]byte("not-a-valid-header-list"),
HeaderList{},
errors.New("unsupported type for header list"),
},
}
for _, tc := range cases {
var hl HeaderList
err := hl.UnmarshalJSON(tc.bytes)
assert.Equal(t, tc.expectedHeaderList, hl, tc.scenario)
assert.Equal(t, tc.expectedError, err, tc.scenario)
}
}