-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrouting_test.go
134 lines (106 loc) · 3.21 KB
/
routing_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
package mango
import (
"net/http"
"testing"
)
func routingTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Hello World!")
}
func routingATestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Server A")
}
func routingBTestServer(env Env) (Status, Headers, Body) {
return 200, Headers{}, Body("Server B")
}
func routingCTestServer(env Env) (Status, Headers, Body) {
if env["Routing.matches"].([]string)[1] == "123" {
return 200, Headers{}, Body("Server C")
}
return 500, Headers{}, Body("Test Failed")
}
func TestRoutingSuccess(t *testing.T) {
// Compile the stack
routingStack := new(Stack)
routes := make(map[string]App)
routes["/a"] = routingATestServer
routes["/b"] = routingBTestServer
routes["/c/(.*)"] = routingCTestServer
routingStack.Middleware(Routing(routes))
routingApp := routingStack.Compile(routingTestServer)
// Request against A
request, err := http.NewRequest("GET", "http://localhost:3000/a", nil)
status, _, body := routingApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected := "Server A"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
// Request against B
request, err = http.NewRequest("GET", "http://localhost:3000/b", nil)
status, _, body = routingApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected = "Server B"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
// Request against C
request, err = http.NewRequest("GET", "http://localhost:3000/c/123", nil)
status, _, body = routingApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected = "Server C"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
}
func TestRoutingFailure(t *testing.T) {
// Compile the stack
routingStack := new(Stack)
routes := make(map[string]App)
routes["/a"] = routingATestServer
routes["/b"] = routingBTestServer
routingStack.Middleware(Routing(routes))
routingApp := routingStack.Compile(routingTestServer)
// Request against it
request, err := http.NewRequest("GET", "http://localhost:3000/c", nil)
status, _, body := routingApp(Env{"mango.request": &Request{request}})
if err != nil {
t.Error(err)
}
if status != 200 {
t.Error("Expected status to equal 200, got:", status)
}
expected := "Hello World!"
if string(body) != expected {
t.Error("Expected body:", string(body), "to equal:", expected)
}
}
func BenchmarkRouting(b *testing.B) {
b.StopTimer()
routingStack := new(Stack)
routes := make(map[string]App)
routes["/a"] = routingATestServer
routes["/b"] = routingBTestServer
routingStack.Middleware(Routing(routes))
routingApp := routingStack.Compile(routingTestServer)
request, _ := http.NewRequest("GET", "http://localhost:3000/a", nil)
b.StartTimer()
for i := 0; i < b.N; i++ {
routingApp(Env{"mango.request": &Request{request}})
}
b.StopTimer()
}