forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
e2e_chunked_test.go
99 lines (82 loc) · 2.42 KB
/
e2e_chunked_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package httpexpect
import (
"bufio"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
func createChunkedHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Proto != "HTTP/1.1" {
w.WriteHeader(http.StatusBadRequest)
} else if len(r.TransferEncoding) != 1 || r.TransferEncoding[0] != "chunked" {
w.WriteHeader(http.StatusBadRequest)
} else if r.PostFormValue("key") != "value" {
w.WriteHeader(http.StatusBadRequest)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`[1, `))
w.(http.Flusher).Flush()
_, _ = w.Write([]byte(`2]`))
}
})
return mux
}
func createChunkedFastHandler(t *testing.T) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
headers := map[string][]string{}
ctx.Request.Header.VisitAll(func(k, v []byte) {
headers[string(k)] = append(headers[string(k)], string(v))
})
assert.Equal(t, []string{"chunked"}, headers["Transfer-Encoding"])
assert.Equal(t, "value", string(ctx.FormValue("key")))
assert.Equal(t, "key=value", string(ctx.Request.Body()))
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.Response.SetBodyStreamWriter(func(w *bufio.Writer) {
_, _ = w.WriteString(`[1, `)
_ = w.Flush()
_, _ = w.WriteString(`2]`)
})
}
}
func testChunkedHandler(e *Expect) {
e.PUT("/").
WithHeader("Content-Type", "application/x-www-form-urlencoded").
WithChunked(strings.NewReader("key=value")).
Expect().
Status(http.StatusOK).
ContentType("application/json").
TransferEncoding("chunked").
JSON().Array().Elements(1, 2)
}
func TestE2EChunkedLive(t *testing.T) {
handler := createChunkedHandler()
server := httptest.NewServer(handler)
defer server.Close()
testChunkedHandler(New(t, server.URL))
}
func TestE2EChunkedBinderStandard(t *testing.T) {
handler := createChunkedHandler()
testChunkedHandler(WithConfig(Config{
BaseURL: "http://example.com",
Reporter: NewAssertReporter(t),
Client: &http.Client{
Transport: NewBinder(handler),
},
}))
}
func TestE2EChunkedBinderFast(t *testing.T) {
handler := createChunkedFastHandler(t)
testChunkedHandler(WithConfig(Config{
BaseURL: "http://example.com",
Reporter: NewAssertReporter(t),
Client: &http.Client{
Transport: NewFastBinder(handler),
},
}))
}