Skip to content

Commit

Permalink
test: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
welkeyever committed Aug 10, 2023
1 parent d32681d commit 98e69ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/protocol/http1/ext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
errs "github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/network"
"github.com/cloudwego/hertz/pkg/protocol"
Expand Down Expand Up @@ -117,9 +118,16 @@ func WriteBodyChunked(w network.Writer, r io.Reader) error {
if err == nil {
panic("BUG: io.Reader returned 0, nil")
}

if !errors.Is(err, io.EOF) {
hlog.SystemLogger().Warnf("writing chunked response body encountered an error from the reader, "+
"this may cause the short of the content in response body, error: %s", err.Error())
}

if err = WriteChunk(w, buf[:0], true); err != nil {
break
}

err = nil
break
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/protocol/http1/ext/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package ext
import (
"bytes"
"errors"
"github.com/cloudwego/hertz/pkg/common/hlog"

Check failure on line 22 in pkg/protocol/http1/ext/common_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 22 in pkg/protocol/http1/ext/common_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 22 in pkg/protocol/http1/ext/common_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

File is not `gofumpt`-ed (gofumpt)
"io"
"strings"
"testing"

Check failure on line 25 in pkg/protocol/http1/ext/common_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.18)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 25 in pkg/protocol/http1/ext/common_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.19)

File is not `gofumpt`-ed (gofumpt)

Check failure on line 25 in pkg/protocol/http1/ext/common_test.go

View workflow job for this annotation

GitHub Actions / lint-and-ut (1.20)

File is not `gofumpt`-ed (gofumpt)
Expand Down Expand Up @@ -123,6 +124,9 @@ func TestReadRawHeaders(t *testing.T) {
}

func TestBodyChunked(t *testing.T) {
var log bytes.Buffer
hlog.SetOutput(&log)

body := "foobar baz aaa bbb ccc"
chunk := "16\r\nfoobar baz aaa bbb ccc\r\n0\r\n"
b := bytes.NewBufferString(body)
Expand All @@ -137,6 +141,22 @@ func TestBodyChunked(t *testing.T) {
rb, err := ReadBody(zr, -1, 0, nil)
assert.Nil(t, err)
assert.DeepEqual(t, body, string(rb))

assert.DeepEqual(t, 0, log.Len())
}

func TestBrokenBodyChunked(t *testing.T) {
brokenReader := mock.NewBrokenConn("")
var log bytes.Buffer
hlog.SetOutput(&log)

var w bytes.Buffer
zw := netpoll.NewWriter(&w)
err := WriteBodyChunked(zw, brokenReader)
assert.Nil(t, err)

assert.DeepEqual(t, []byte("0\r\n"), w.Bytes())
assert.True(t, bytes.Contains(log.Bytes(), []byte("writing chunked response body encountered an error from the reader")))
}

func TestBodyFixedSize(t *testing.T) {
Expand Down

0 comments on commit 98e69ab

Please sign in to comment.