-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparser_both_test.go
215 lines (188 loc) · 4.58 KB
/
parser_both_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
package httparser
import (
"bytes"
"testing"
)
// 测试请求body
func Test_ParserResponse_RequestBody_BOTH(t *testing.T) {
p := New(BOTH)
data := []byte(
"POST /joyent/http-parser HTTP/1.1\r\n" +
"Host: github.com\r\n" +
"DNT: 1\r\n" +
"Accept-Encoding: gzip, deflate, sdch\r\n" +
"Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n" +
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/39.0.2171.65 Safari/537.36\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9," +
"image/webp,*/*;q=0.8\r\n" +
"Referer: https://github.com/joyent/http-parser\r\n" +
"Connection: keep-alive\r\n" +
"Transfer-Encoding: chunked\r\n" +
"Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n")
messageBegin := false
messageComplete := false
headersComplete := false
var body []byte
var url []byte
var field []byte
var value []byte
body2 := "hello world"
value2 := "github.com" +
"1" +
"gzip, deflate, sdch" +
"ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4" +
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/39.0.2171.65 Safari/537.36" +
"text/html,application/xhtml+xml,application/xml;q=0.9," +
"image/webp,*/*;q=0.8" +
"https://github.com/joyent/http-parser" +
"keep-alive" +
"chunked" +
"max-age=0"
field2 := []byte(
"Host" +
"DNT" +
"Accept-Encoding" +
"Accept-Language" +
"User-Agent" +
"Accept" +
"Referer" +
"Connection" +
"Transfer-Encoding" +
"Cache-Control")
setting := Setting{
MessageBegin: func(*Parser, int) {
messageBegin = true
},
URL: func(p *Parser, buf []byte, _ int) {
url = append(url, buf...)
},
Status: func(*Parser, []byte, int) {
// 响应包才需要用到
},
HeaderField: func(p *Parser, buf []byte, _ int) {
field = append(field, buf...)
},
HeaderValue: func(p *Parser, buf []byte, _ int) {
value = append(value, buf...)
},
HeadersComplete: func(*Parser, int) {
headersComplete = true
},
Body: func(p *Parser, buf []byte, _ int) {
body = append(body, buf...)
},
MessageComplete: func(p *Parser, _ int) {
messageComplete = true
},
}
i, err := p.Execute(&setting, data)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(url, []byte("/joyent/http-parser")) {
t.Fatal("url error")
}
if i != len(data) {
t.Fatal("data length error")
}
// 总数据长度
if i != len(data) {
t.Errorf("i(%d) != len(data)(%d)\n", i, len(data))
return
}
if !bytes.Equal(field, field2) {
t.Errorf("field error: %s\n", field)
return
}
if string(value) != value2 {
t.Errorf("value error: %s\n", value)
return
}
if string(body) != body2 {
t.Errorf("body error: %s\n", body)
return
}
if !messageBegin {
t.Error("message begin is false, expect true")
return
}
if !messageComplete {
t.Error("message complete is false, expect true")
return
}
if !headersComplete {
t.Error("headers complete is false, expect true")
return
}
if !p.EOF() {
t.Error("EOF is false, expect true")
return
}
// fmt.Printf("##:%s", stateTab[p.currState])
}
func Test_ParserResponse_Chunked_Both(t *testing.T) {
p := New(BOTH)
messageBegin := false
rcvBuf := []byte{}
setting := &Setting{
Status: func(p *Parser, buf []byte, _ int) {
if !bytes.Equal(buf, []byte("OK")) {
t.Error("status error")
}
}, MessageBegin: func(p *Parser, _ int) {
messageBegin = true
}, HeaderField: func(p *Parser, buf []byte, _ int) {
}, HeaderValue: func(p *Parser, buf []byte, _ int) {
}, Body: func(p *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.Errorf("err:%s", err)
return
}
parserTotal += rv
sentTotal += len(buf)
}
if !bytes.Equal(rcvBuf, []byte("MozillaDeveloperNetworknew year")) {
t.Errorf("rcvBuf:%s", rcvBuf)
return
}
if p.Major != 1 || p.Minor != 1 {
t.Errorf("major:%d, minor:%d", p.Major, p.Minor)
return
}
if !messageBegin {
t.Error("message begin is false, expect true")
return
}
if sentTotal != parserTotal {
t.Errorf("sendTotal:%d, parserTotal:%d", sentTotal, parserTotal)
return
}
if !p.EOF() {
t.Error("EOF is false, expect true")
return
}
}