-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d6bedc
commit ce71109
Showing
5 changed files
with
319 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package httparser | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Isuse1(t *testing.T) { | ||
var 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" + | ||
|
||
"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") | ||
|
||
body := []byte{} | ||
var setting = Setting{ | ||
MessageBegin: func() { | ||
fmt.Println("---- begin") | ||
}, | ||
URL: func(buf []byte) { | ||
}, | ||
Status: func([]byte) { | ||
// 响应包才需要用到 | ||
}, | ||
HeaderField: func(buf []byte) { | ||
}, | ||
HeaderValue: func(buf []byte) { | ||
}, | ||
HeadersComplete: func() { | ||
}, | ||
Body: func(buf []byte) { | ||
body = append(body, buf...) | ||
}, | ||
MessageComplete: func() { | ||
}, | ||
} | ||
|
||
p := New(REQUEST) | ||
fmt.Printf("req_len=%d\n", len(data)/2) | ||
// 一个POST 518,一共两个POST,第一次解析600字节,第二次解析剩余的 | ||
data1, data2 := data[:600], data[600:] | ||
_, err := p.Execute(&setting, data1) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
_, err = p.Execute(&setting, data2) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
assert.Equal(t, body, []byte("hello worldhello world")) | ||
p.Reset() | ||
|
||
} | ||
|
||
func Test_Issue2(t *testing.T) { | ||
|
||
var 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") | ||
|
||
var body []byte | ||
var setting = Setting{ | ||
MessageBegin: func() { | ||
}, | ||
URL: func(buf []byte) { | ||
}, | ||
Status: func([]byte) { | ||
}, | ||
HeaderField: func(buf []byte) { | ||
}, | ||
HeaderValue: func(buf []byte) { | ||
}, | ||
HeadersComplete: func() { | ||
}, | ||
Body: func(buf []byte) { | ||
body = append(body, buf...) | ||
}, | ||
MessageComplete: func() { | ||
fmt.Println("---- complete") | ||
}, | ||
} | ||
|
||
p := New(REQUEST) | ||
fmt.Printf("req_len=%d\n", len(data)) | ||
// 一个POST 518,一共两个POST,第一次解析600字节,第二次解析剩余的 | ||
data1, data2 := data[:300], data[300:] | ||
sucess, err := p.Execute(&setting, data1) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
sucess, err = p.Execute(&setting, append(data1[sucess:], data2...)) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
p.Reset() | ||
|
||
} |
Oops, something went wrong.