-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathrequest_test.go
115 lines (106 loc) · 2.44 KB
/
request_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
package postman
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRequestMarshalJSON(t *testing.T) {
cases := []struct {
scenario string
req Request
expectedOutput string
}{
{
"Successfully marshalling a Request as a string",
Request{
Method: Get,
URL: &URL{
Raw: "http://www.google.fr",
version: V200,
},
},
"\"http://www.google.fr\"",
},
{
"Successfully marshalling a Request as an object (v2.0.0)",
Request{
Method: Post,
URL: &URL{
Raw: "http://www.google.fr",
version: V200,
},
Body: &Body{
Mode: "raw",
Raw: "raw-content",
Options: &BodyOptions{BodyOptionsRaw{Language: "json"}},
},
},
"{\"url\":\"http://www.google.fr\",\"method\":\"POST\",\"body\":{\"mode\":\"raw\",\"raw\":\"raw-content\",\"options\":{\"raw\":{\"language\":\"json\"}}}}",
},
{
"Successfully marshalling a Request as an object (v2.1.0)",
Request{
Method: Post,
URL: &URL{
Raw: "http://www.google.fr",
version: V210,
},
Body: &Body{
Mode: "raw",
Raw: "raw-content",
},
},
"{\"url\":{\"raw\":\"http://www.google.fr\"},\"method\":\"POST\",\"body\":{\"mode\":\"raw\",\"raw\":\"raw-content\"}}",
},
}
for _, tc := range cases {
bytes, _ := tc.req.MarshalJSON()
assert.Equal(t, tc.expectedOutput, string(bytes), tc.scenario)
}
}
func TestRequestUnmarshalJSON(t *testing.T) {
cases := []struct {
scenario string
bytes []byte
expectedRequest Request
expectedError error
}{
{
"Successfully unmarshalling a Request as a string",
[]byte("\"http://www.google.fr\""),
Request{
Method: Get,
URL: &URL{
Raw: "http://www.google.fr",
},
},
nil,
},
{
"Successfully unmarshalling a Request URL with some content",
[]byte("{\"url\":\"http://www.google.fr\",\"body\":{\"mode\":\"raw\",\"raw\":\"awesome-body\"}}"),
Request{
URL: &URL{
Raw: "http://www.google.fr",
},
Body: &Body{
Mode: "raw",
Raw: "awesome-body",
},
},
nil,
},
{
"Failed to unmarshal a Request because of an unsupported type",
[]byte("not-a-valid-request"),
Request{},
errors.New("Unsupported type"),
},
}
for _, tc := range cases {
var r Request
err := r.UnmarshalJSON(tc.bytes)
assert.Equal(t, tc.expectedRequest, r, tc.scenario)
assert.Equal(t, tc.expectedError, err, tc.scenario)
}
}