aboutsummaryrefslogtreecommitdiff
path: root/streaming_test.go
diff options
context:
space:
mode:
authorGravatar ichx <czn16@qq.com> 2021-12-05 21:11:51 +0800
committerGravatar GitHub <noreply@github.com> 2021-12-05 14:11:51 +0100
commitda7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1 (patch)
treed361832c2ab7da262ce69a646377cf3301968cca /streaming_test.go
parentfix: reset request after reset user values on keep-alive connections (#1162) (diff)
downloadfasthttp-da7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1.tar.gz
fasthttp-da7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1.tar.bz2
fasthttp-da7ff7a2080953e370e8bd712f5ae8ef8b0cb4e1.zip
Add trailer support (#1165)
* Add trailer support * fix issue and add documentation * remove redundant code * add error return for add/set trailer method * fix lint error * fix bad trailer error return issue and update bad content-length error * update errNonNumericChars * update errNonNumericChars * fix issue about error and fix typo
Diffstat (limited to 'streaming_test.go')
-rw-r--r--streaming_test.go67
1 files changed, 66 insertions, 1 deletions
diff --git a/streaming_test.go b/streaming_test.go
index 6066c39..32f0003 100644
--- a/streaming_test.go
+++ b/streaming_test.go
@@ -3,6 +3,7 @@ package fasthttp
import (
"bufio"
"bytes"
+ "fmt"
"io/ioutil"
"sync"
"testing"
@@ -102,7 +103,7 @@ aaaaaaaaaa`
func getChunkedTestEnv(t testing.TB) (*fasthttputil.InmemoryListener, []byte) {
body := createFixedBody(128 * 1024)
- chunkedBody := createChunkedBody(body)
+ chunkedBody := createChunkedBody(body, nil, true)
testHandler := func(ctx *RequestCtx) {
bodyBytes, err := ioutil.ReadAll(ctx.RequestBodyStream())
@@ -142,6 +143,70 @@ func getChunkedTestEnv(t testing.TB) (*fasthttputil.InmemoryListener, []byte) {
return ln, formattedRequest
}
+func TestRequestStreamChunkedWithTrailer(t *testing.T) {
+ t.Parallel()
+
+ body := createFixedBody(10)
+ expectedTrailer := map[string]string{
+ "Foo": "footest",
+ "Bar": "bartest",
+ }
+ chunkedBody := createChunkedBody(body, expectedTrailer, true)
+ req := fmt.Sprintf(`POST / HTTP/1.1
+Host: example.com
+Transfer-Encoding: chunked
+Trailer: Foo, Bar
+
+%s
+`, chunkedBody)
+
+ ln := fasthttputil.NewInmemoryListener()
+ s := &Server{
+ StreamRequestBody: true,
+ Handler: func(ctx *RequestCtx) {
+ all, err := ioutil.ReadAll(ctx.RequestBodyStream())
+ if err != nil {
+ t.Errorf("unexpected error: %s", err)
+ }
+ if !bytes.Equal(all, body) {
+ t.Errorf("unexpected body %q. Expecting %q", all, body)
+ }
+
+ for k, v := range expectedTrailer {
+ r := ctx.Request.Header.Peek(k)
+ if string(r) != v {
+ t.Errorf("unexpected trailer %s. Expecting %s. Got %q", k, v, r)
+ }
+ }
+ },
+ }
+
+ ch := make(chan struct{})
+ go func() {
+ if err := s.Serve(ln); err != nil {
+ t.Errorf("unexpected error: %s", err)
+ }
+ close(ch)
+ }()
+
+ conn, err := ln.Dial()
+ if err != nil {
+ t.Fatalf("unexpected error: %s", err)
+ }
+ if _, err = conn.Write([]byte(req)); err != nil {
+ t.Fatalf("unexpected error: %s", err)
+ }
+ if err := ln.Close(); err != nil {
+ t.Fatalf("error when closing listener: %s", err)
+ }
+
+ select {
+ case <-ch:
+ case <-time.After(time.Second):
+ t.Fatal("timeout when waiting for the server to stop")
+ }
+}
+
func TestRequestStream(t *testing.T) {
t.Parallel()