This repository has been archived by the owner on Nov 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
application_test.go
140 lines (107 loc) · 3.48 KB
/
application_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"testing"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
func TestAddRouteWorks(t *testing.T) {
a := NewApp(NewRdm(), true)
a.AddRoute("/user/:name", "GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "CONNECT", "TRACE", "PATCH")
}
func TestAddRouteBadMethod(t *testing.T) {
defer shouldPanic()
a := NewApp(NewRdm(), true)
a.AddRoute("/user/:name", "BABY")
}
func TestAddRouteNoMethod(t *testing.T) {
defer shouldPanic()
a := NewApp(NewRdm(), true)
a.AddRoute("/user/:name")
}
func TestApplicationNilTree(t *testing.T) {
defer shouldPanic()
a := NewApp(NewRdm(), true)
a.root = nil
ctx := &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/")
a.ServeHTTP(ctx)
}
func TestApplicationNilBalancer(t *testing.T) {
defer shouldPanic()
a := NewApp(nil, true)
ctx := &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/")
a.ServeHTTP(ctx)
}
func TestApplicationRedirect(t *testing.T) {
ln := fasthttputil.NewInmemoryListener()
defer ln.Close()
go fasthttp.Serve(ln, fakeHandler)
setFakeBackend(ln.Addr().String(), 1)
fb := fakeBalancer{}
a := NewApp(fb, true)
a.AddRoute("/user/jhon", "POST")
a.AddRoute("/user/jhon/card/", "POST")
//redirect
ctx := &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/user/jhon/")
ctx.Request.Header.SetMethod("POST")
a.ServeHTTP(ctx)
if code := ctx.Response.StatusCode(); code != fasthttp.StatusTemporaryRedirect {
t.Errorf("response code should be %d but got: %d", fasthttp.StatusTemporaryRedirect, code)
}
// redirect
ctx = &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/user/jhon/card")
ctx.Request.Header.SetMethod("POST")
a.ServeHTTP(ctx)
if code := ctx.Response.StatusCode(); code != fasthttp.StatusTemporaryRedirect {
t.Errorf("response code should be %d but got: %d", fasthttp.StatusTemporaryRedirect, code)
}
// not found
ctx = &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/user/what/card")
ctx.Request.Header.SetMethod("POST")
a.ServeHTTP(ctx)
if code := ctx.Response.StatusCode(); code != fasthttp.StatusNotFound {
t.Errorf("response code should be %d but got: %d", fasthttp.StatusNotFound, code)
}
// method not allowed
ctx = &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/user/jhon")
ctx.Request.Header.SetMethod("GET")
a.ServeHTTP(ctx)
if code := ctx.Response.StatusCode(); code != fasthttp.StatusMethodNotAllowed {
t.Errorf("response code should be %d but got: %d", fasthttp.StatusMethodNotAllowed, code)
}
}
func TestApplicationCircuit(t *testing.T) {
ln := fasthttputil.NewInmemoryListener()
defer ln.Close()
go fasthttp.Serve(ln, fakeHandler)
setFakeBackend(ln.Addr().String(), 1)
fb := fakeBalancer{}
a := NewApp(fb, true)
a.AddRoute("/user/jhon", "POST")
//redirect
ctx := &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/user/jhon/")
ctx.Request.Header.SetMethod("POST")
a.ServeHTTP(ctx)
if code := ctx.Response.StatusCode(); code != fasthttp.StatusTemporaryRedirect {
t.Errorf("response code should be %d but got: %d", fasthttp.StatusTemporaryRedirect, code)
}
// circuit is not on
ctx = &fasthttp.RequestCtx{}
ctx.Request.SetRequestURI("/user/jhon")
ctx.Request.Header.SetMethod("POST")
a.ServeHTTP(ctx) // do not check what `Proxy` returns
// circuit is on
for i := 0; i < 100; i++ {
a.root.incr(fasthttp.StatusBadGateway)
}
a.ServeHTTP(ctx)
if code := ctx.Response.StatusCode(); code != fasthttp.StatusTooManyRequests {
t.Errorf("response code should be %d but got: %d", fasthttp.StatusTooManyRequests, code)
}
}