From ab4ee5971f21ee5cf8a1970eb58a8a3a8733f3cb Mon Sep 17 00:00:00 2001 From: guonaihong Date: Mon, 30 Oct 2023 22:06:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=85=A2=E6=85=A2=E7=A7=BB=E9=99=A4=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- parser_both_test.go | 53 +++++++++++++++++++++++++++------------------ unhex_test.go | 21 ++++++++++++------ zsplit_test.go | 15 ++++++++----- 3 files changed, 56 insertions(+), 33 deletions(-) diff --git a/parser_both_test.go b/parser_both_test.go index 05322e1..1ed07e4 100644 --- a/parser_both_test.go +++ b/parser_both_test.go @@ -1,6 +1,7 @@ package httparser import ( + "bytes" "testing" "github.com/stretchr/testify/assert" @@ -10,7 +11,7 @@ import ( func Test_ParserResponse_RequestBody_BOTH(t *testing.T) { p := New(BOTH) - var data = []byte( + data := []byte( "POST /joyent/http-parser HTTP/1.1\r\n" + "Host: github.com\r\n" + "DNT: 1\r\n" + @@ -36,7 +37,7 @@ func Test_ParserResponse_RequestBody_BOTH(t *testing.T) { var value []byte body2 := "hello world" - var value2 = "github.com" + + value2 := "github.com" + "1" + "gzip, deflate, sdch" + "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4" + @@ -49,7 +50,7 @@ func Test_ParserResponse_RequestBody_BOTH(t *testing.T) { "keep-alive" + "chunked" + "max-age=0" - var field2 = []byte( + field2 := []byte( "Host" + "DNT" + "Accept-Encoding" + @@ -61,7 +62,7 @@ func Test_ParserResponse_RequestBody_BOTH(t *testing.T) { "Transfer-Encoding" + "Cache-Control") - var setting = Setting{ + setting := Setting{ MessageBegin: func(*Parser) { messageBegin = true }, @@ -89,18 +90,28 @@ func Test_ParserResponse_RequestBody_BOTH(t *testing.T) { } i, err := p.Execute(&setting, data) - assert.NoError(t, err) - assert.Equal(t, url, []byte("/joyent/http-parser")) //url - assert.Equal(t, i, len(data)) //总数据长度 - assert.Equal(t, field, field2) //header field - assert.Equal(t, string(value), value2) //header field - assert.Equal(t, string(body), body2) //chunked body + 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") + } + + assert.Equal(t, i, len(data)) // 总数据长度 + assert.Equal(t, field, field2) // header field + assert.Equal(t, string(value), value2) // header field + assert.Equal(t, string(body), body2) // chunked body assert.True(t, messageBegin) assert.True(t, messageComplete) assert.True(t, headersComplete) assert.True(t, p.EOF()) - //fmt.Printf("##:%s", stateTab[p.currState]) + // fmt.Printf("##:%s", stateTab[p.currState]) } func Test_ParserResponse_Chunked_Both(t *testing.T) { @@ -108,16 +119,16 @@ func Test_ParserResponse_Chunked_Both(t *testing.T) { messageBegin := false rcvBuf := []byte{} - setting := &Setting{Status: func(p *Parser, buf []byte) { - assert.Equal(t, buf, []byte("OK")) - }, MessageBegin: func(p *Parser) { - messageBegin = true - }, HeaderField: func(p *Parser, buf []byte) { - - }, HeaderValue: func(p *Parser, buf []byte) { - }, Body: func(p *Parser, buf []byte) { - rcvBuf = append(rcvBuf, buf...) - }, + setting := &Setting{ + Status: func(p *Parser, buf []byte) { + assert.Equal(t, buf, []byte("OK")) + }, MessageBegin: func(p *Parser) { + messageBegin = true + }, HeaderField: func(p *Parser, buf []byte) { + }, HeaderValue: func(p *Parser, buf []byte) { + }, Body: func(p *Parser, buf []byte) { + rcvBuf = append(rcvBuf, buf...) + }, } var rsp [3]string diff --git a/unhex_test.go b/unhex_test.go index eba78dd..d66db0a 100644 --- a/unhex_test.go +++ b/unhex_test.go @@ -1,10 +1,7 @@ package httparser import ( - "fmt" "testing" - - "github.com/stretchr/testify/assert" ) func Test_Unhex(t *testing.T) { @@ -12,13 +9,23 @@ func Test_Unhex(t *testing.T) { v := unhex[i] switch { case i >= '0' && i <= '9': - assert.Equal(t, v, int8(i-'0')) + if i-'0' != int(v) { + t.Fatalf("fail:%c", i) + } case i >= 'a' && i <= 'f': - assert.Equal(t, v, int8(i-'a'+10)) + if i-'a'+10 != int(v) { + t.Fatalf("fail:%c", i) + } + case i >= 'A' && i <= 'F': - assert.Equal(t, v, int8(i-'A'+10)) + if i-'A'+10 != int(v) { + t.Fatalf("fail:%c", i) + } default: - assert.Equal(t, v, int8(-1), fmt.Sprintf("fail:%c", i)) + if v != -1 { + t.Fatalf("fail:%c", i) + } + } } } diff --git a/zsplit_test.go b/zsplit_test.go index 926e42b..532b401 100644 --- a/zsplit_test.go +++ b/zsplit_test.go @@ -4,8 +4,6 @@ import ( "bytes" "errors" "testing" - - "github.com/stretchr/testify/assert" ) func Test_ZSplit(t *testing.T) { @@ -20,14 +18,21 @@ func Test_ZSplit(t *testing.T) { } return nil }) - assert.NoError(t, err) + if err != nil { + t.Error(err) + } - assert.Equal(t, need, got) + if got != need { + t.Errorf("got %d, need %d", got, need) + } } func Test_ZSplit_Error(t *testing.T) { err := Split([]byte("hello,world"), []byte(","), func(v []byte) error { return errors.New("fail") }) - assert.Error(t, err) + + if err == nil { + t.Error("err should not be nil") + } }