-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparser_res_test.go
290 lines (247 loc) · 6.39 KB
/
parser_res_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package httparser
import (
"bytes"
"reflect"
"testing"
)
// 测试解析状态行
// TODO status为空的测试数据
func Test_ParserResponse_StatusLine(t *testing.T) {
p := New(RESPONSE)
messageBegin := false
rsp := []byte("HTTP/1.1 200 OK\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(*Parser, int) {
messageBegin = true
},
}, rsp)
if err != nil {
t.Error(err)
}
if p.Major != 1 {
t.Errorf("major is %d, expect 1", p.Major)
}
if p.Minor != 1 {
t.Errorf("minor is %d, expect 1", p.Minor)
}
if !messageBegin {
t.Error("message begin is false, expect true")
}
}
// 测试解析http header
func Test_ParserResponse_HeaderField(t *testing.T) {
p := New(RESPONSE)
messageBegin := false
rsp := []byte("HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(*Parser, int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("Connection")) {
t.Error("header field error")
}
},
}, rsp)
if err != nil {
t.Error(err)
}
if p.Major != 1 {
t.Errorf("major is %d, expect 1", p.Major)
}
if p.Minor != 1 {
t.Errorf("minor is %d, expect 1", p.Minor)
}
if !messageBegin {
t.Error("message begin is false, expect true")
}
}
// 测试解析http header 和 http value
func Test_ParserResponse_HeaderValue(t *testing.T) {
p := New(RESPONSE)
messageBegin := false
rsp := []byte("HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(*Parser, int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("Connection")) {
t.Error("header field error")
}
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("close")) {
t.Error("header value error")
}
},
}, rsp)
if err != nil {
t.Error(err)
}
if p.Major != 1 {
t.Errorf("major is %d, expect 1", p.Major)
}
if p.Minor != 1 {
t.Errorf("minor is %d, expect 1", p.Minor)
}
if !messageBegin {
t.Error("message begin is false, expect true")
}
}
// 测试解析多个http header 和 http value。
func Test_ParserResponse_Multiple_HeaderValue(t *testing.T) {
p := New(RESPONSE)
messageBegin := false
field := []string{}
fieldValue := []string{}
rsp := []byte("HTTP/1.1 200 OK\r\n" +
"Content-Length: 10\r\n" +
"Connection: close\r\n\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(*Parser, int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
field = append(field, string(buf))
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
fieldValue = append(fieldValue, string(buf))
},
}, rsp)
if err != nil {
t.Error(err)
}
if p.Major != 1 {
t.Errorf("major is %d, expect 1", p.Major)
}
if p.Minor != 1 {
t.Errorf("minor is %d, expect 1", p.Minor)
}
if !messageBegin {
t.Error("message begin is false, expect true")
}
if !reflect.DeepEqual(field, []string{"Content-Length", "Connection"}) {
t.Errorf("field is %v, expect [Content-Length, Connection]", field)
}
if !reflect.DeepEqual(fieldValue, []string{"10", "close"}) {
t.Errorf("fieldvalue is %v, expect [10, close]", fieldValue)
}
}
// 测试解析Content-Length body数据
func Test_ParserResponse_Content_Length_Body(t *testing.T) {
p := New(RESPONSE)
messageBegin := false
rcvBuf := []byte{}
setting := &Setting{
Status: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(_ *Parser, _ int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
}, Body: func(_ *Parser, buf []byte, _ int) {
rcvBuf = append(rcvBuf, buf...)
},
}
var rsp [3][]byte
rsp[0] = []byte("HTTP/1.1 200 OK\r\n" +
"Content-Length: 20\r\n" +
"Connection: close\r\n\r\n")
rsp[1] = []byte("123456789a")
rsp[2] = []byte("abcdefghij")
for _, buf := range rsp {
_, err := p.Execute(setting, buf)
if err != nil {
t.Error(err)
return
}
}
if !bytes.Equal(rcvBuf, append(rsp[1], rsp[2]...)) {
t.Errorf("rcvBuf is %v, expect %v", rcvBuf, append(rsp[1], rsp[2]...))
}
if p.Major != 1 {
t.Errorf("major is %d, expect 1", p.Major)
}
if p.Minor != 1 {
t.Errorf("minor is %d, expect 1", p.Minor)
}
if !messageBegin {
t.Error("message begin is false, expect true")
}
}
func Test_ParserResponse_Chunked(t *testing.T) {
p := New(RESPONSE)
messageBegin := false
rcvBuf := []byte{}
setting := &Setting{
Status: func(_ *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(_ *Parser, _ int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
}, Body: func(_ *Parser, buf []byte, _ int) {
rcvBuf = append(rcvBuf, buf...)
},
}
var rsp [3]string
rsp[0] = "HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Transfer-Encoding: chunked\r\n\r\n" +
"7\r\n" +
"Mozilla\r\n" +
"9\r\n" +
"Developer\r\n" +
"7\r\n" +
"Network\r\n"
rsp[1] = "8\r\n" +
"new year\r\n"
rsp[2] = "0\r\n\r\n"
sentTotal := 0
parserTotal := 0
for _, buf := range rsp {
rv, err := p.Execute(setting, []byte(buf))
if err != nil {
t.Error(err)
return
}
parserTotal += rv
sentTotal += len(buf)
}
if !bytes.Equal(rcvBuf, []byte("MozillaDeveloperNetworknew year")) {
t.Errorf("rcvBuf is %v, expect %v", rcvBuf, []byte("MozillaDeveloperNetworknew year"))
}
if p.Major != 1 {
t.Errorf("major is %d, expect 1", p.Major)
}
if p.Minor != 1 {
t.Errorf("minor is %d, expect 1", p.Minor)
}
if !messageBegin {
t.Error("message begin is false, expect true")
}
if sentTotal != parserTotal {
t.Errorf("sentTotal is %d, parserTotal is %d", sentTotal, parserTotal)
}
if !p.EOF() {
t.Error("EOF is true, expect false")
}
}