forked from gavv/httpexpect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2e_redirect_test.go
74 lines (58 loc) · 1.48 KB
/
e2e_redirect_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
package httpexpect
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/valyala/fasthttp"
)
func createRedirectHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/foo", func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`hello`))
})
mux.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/foo", http.StatusFound)
})
return mux
}
func createRedirectFastHandler() fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/foo":
ctx.SetBody([]byte(`hello`))
case "/bar":
ctx.Redirect("/foo", http.StatusFound)
}
}
}
func testRedirectHandler(e *Expect) {
e.POST("/bar").
Expect().
Status(http.StatusOK).Body().Equal(`hello`)
}
func TestE2ERedirectLive(t *testing.T) {
handler := createRedirectHandler()
server := httptest.NewServer(handler)
defer server.Close()
testRedirectHandler(New(t, server.URL))
}
func TestE2ERedirectBinderStandard(t *testing.T) {
handler := createRedirectHandler()
testRedirectHandler(WithConfig(Config{
BaseURL: "http://example.com",
Reporter: NewAssertReporter(t),
Client: &http.Client{
Transport: NewBinder(handler),
},
}))
}
func TestE2ERedirectBinderFast(t *testing.T) {
handler := createRedirectFastHandler()
testRedirectHandler(WithConfig(Config{
BaseURL: "http://example.com",
Reporter: NewAssertReporter(t),
Client: &http.Client{
Transport: NewFastBinder(handler),
},
}))
}