-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolhttp_spec_test.go
136 lines (119 loc) · 3.69 KB
/
golhttp_spec_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
package golhttpclient
import (
"io/ioutil"
"testing"
)
var (
specPost = `POST /what/is/path?user=me HTTP/1.0
Host: 10.0.0.1:8080
Postman-Token: xxx-post
`
specGet = `GET /?statuses=read,liked& page=1 HTTP/1.1
Host: 1.1.2.0
cache-control: no-cache
`
specBody = `{
"start_date": "2029-04-19T00:00:00Z",
"end_date": "2039-04-19T00:00:00Z",
"name": "Alice",
"friends": [
"Bob",
"Eve"
]
}`
)
func TestUnmarshalPost(t *testing.T) {
var reqPost Request
Unmarshal([]byte(specPost), &reqPost)
if reqPost.Method != "POST" {
t.Errorf("FAILED to parse HTTP Method: %s", reqPost.Method)
}
if reqPost.Protocol != "HTTP/1.0" {
t.Errorf("FAILED to parse HTTP Protocol: %s", reqPost.Protocol)
}
if reqPost.Path != "/what/is/path" {
t.Errorf("FAILED to parse HTTP Request Path: %s", reqPost.Path)
}
if reqPost.Params["user"] != "me" {
t.Errorf("FAILED to parse HTTP URL Params: %v", reqPost.Params)
}
if reqPost.Headers["Host"] != "10.0.0.1:8080" || reqPost.Headers["Postman-Token"] != "xxx-post" {
t.Errorf("FAILED to parse HTTP Headers: %v", reqPost.Headers)
}
body, _ := ioutil.ReadAll(reqPost.Body)
if string(body) != "" {
t.Errorf("FAILED to parse HTTP Body: %v", body)
}
}
func TestUnmarshalPostWithBody(t *testing.T) {
var reqPost Request
Unmarshal([]byte(specPost+specBody), &reqPost)
if reqPost.Method != "POST" {
t.Errorf("FAILED to parse HTTP Method: %s", reqPost.Method)
}
if reqPost.Protocol != "HTTP/1.0" {
t.Errorf("FAILED to parse HTTP Protocol: %s", reqPost.Protocol)
}
if reqPost.Path != "/what/is/path" {
t.Errorf("FAILED to parse HTTP Request Path: %s", reqPost.Path)
}
if reqPost.Params["user"] != "me" {
t.Errorf("FAILED to parse HTTP URL Params: %v", reqPost.Params)
}
if reqPost.Headers["Host"] != "10.0.0.1:8080" ||
reqPost.Headers["Postman-Token"] != "xxx-post" {
t.Errorf("FAILED to parse HTTP Headers: %v", reqPost.Headers)
}
bodyBytes, _ := ioutil.ReadAll(reqPost.Body)
body := string(bodyBytes)
if body != specBody {
t.Errorf("FAILED to parse HTTP Body: %s", body)
}
}
func TestUnmarshalGet(t *testing.T) {
var reqGet Request
Unmarshal([]byte(specGet), &reqGet)
if reqGet.Method != "GET" {
t.Errorf("FAILED to parse HTTP Method: %s", reqGet.Method)
}
if reqGet.Protocol != "HTTP/1.1" {
t.Errorf("FAILED to parse HTTP Protocol: %s", reqGet.Protocol)
}
if reqGet.Path != "/" {
t.Errorf("FAILED to parse HTTP Request Path: %s", reqGet.Path)
}
if reqGet.Params["statuses"] != "read,liked" || reqGet.Params["amp;page"] != "1" {
t.Errorf("FAILED to parse HTTP URL Params: %v", reqGet.Params)
}
if reqGet.Headers["Host"] != "1.1.2.0" || reqGet.Headers["cache-control"] != "no-cache" {
t.Errorf("FAILED to parse HTTP Headers: %v", reqGet.Headers)
}
body, _ := ioutil.ReadAll(reqGet.Body)
if string(body) != "" {
t.Errorf("FAILED to parse HTTP Body: %v", body)
}
}
func TestUnmarshalGetWithBody(t *testing.T) {
var reqGet Request
Unmarshal([]byte(specGet+specBody), &reqGet)
if reqGet.Method != "GET" {
t.Errorf("FAILED to parse HTTP Method: %s", reqGet.Method)
}
if reqGet.Protocol != "HTTP/1.1" {
t.Errorf("FAILED to parse HTTP Protocol: %s", reqGet.Protocol)
}
if reqGet.Path != "/" {
t.Errorf("FAILED to parse HTTP Request Path: %s", reqGet.Path)
}
if reqGet.Params["statuses"] != "read,liked" || reqGet.Params["amp;page"] != "1" {
t.Errorf("FAILED to parse HTTP URL Params: %v", reqGet.Params)
}
if reqGet.Headers["Host"] != "1.1.2.0" || reqGet.Headers["cache-control"] != "no-cache" {
t.Errorf("FAILED to parse HTTP Headers: %v", reqGet.Headers)
}
bodyBytes, _ := ioutil.ReadAll(reqGet.Body)
body := string(bodyBytes)
if body != specBody {
t.Errorf("FAILED to parse HTTP Body: %s", body)
}
}