Skip to content

Commit

Permalink
慢慢移除依赖库
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Oct 30, 2023
1 parent 3ec9dfe commit ab4ee59
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
53 changes: 32 additions & 21 deletions parser_both_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httparser

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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
},
Expand Down Expand Up @@ -89,35 +90,45 @@ 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) {
p := New(BOTH)

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
Expand Down
21 changes: 14 additions & 7 deletions unhex_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
package httparser

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_Unhex(t *testing.T) {
for i := 0; i < 256; i++ {
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)
}

}
}
}
15 changes: 10 additions & 5 deletions zsplit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"bytes"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_ZSplit(t *testing.T) {
Expand All @@ -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")
}
}

0 comments on commit ab4ee59

Please sign in to comment.