forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch_hosts_test.go
211 lines (193 loc) · 4.81 KB
/
switch_hosts_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package apps
import (
"fmt"
"net/url"
"testing"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/kataras/iris/v12/httptest"
)
type testRequests map[string]map[string]int // url -> path -> status code
func TestSwitchHosts(t *testing.T) {
var (
expected = func(app context.Application, host string) string {
return fmt.Sprintf("App Name: %s\nHost: %s", app, host)
}
index = func(ctx iris.Context) {
ctx.WriteString(expected(ctx.Application(), ctx.Host()))
}
)
testdomain1 := iris.New().SetName("test 1 domain")
testdomain1.Get("/", index) // should match host matching with "testdomain1.com".
testdomain2 := iris.New().SetName("test 2 domain")
testdomain2.Get("/", index) // should match host matching with "testdomain2.com".
mydomain := iris.New().SetName("my domain")
mydomain.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
ctx.WriteString(ctx.Host() + " custom not found")
})
mydomain.Get("/", index) // should match ALL hosts starting with "my".
tests := []struct {
Pattern string
Target *iris.Application
Requests testRequests
}{
{
"testdomain1.com",
testdomain1,
testRequests{
"http://testdomain1.com": {
"/": iris.StatusOK,
},
},
},
{
"testdomain2.com",
testdomain2,
testRequests{
"http://testdomain2.com": {
"/": iris.StatusOK,
},
},
},
{
"^my.*$",
mydomain,
testRequests{
"http://mydomain.com": {
"/": iris.StatusOK,
"/nf": iris.StatusNotFound,
},
"http://myotherdomain.com": {
"/": iris.StatusOK,
},
"http://mymy.com": {
"/": iris.StatusOK,
},
"http://nmy.com": {
"/": iris.StatusBadGateway, /* 404 hijacked by switch.OnErrorCode */
},
},
},
}
var hosts Hosts
for _, tt := range tests {
hosts = append(hosts, Host{tt.Pattern, tt.Target})
}
switcher := Switch(hosts)
switcher.OnErrorCode(iris.StatusNotFound, func(ctx iris.Context) {
// inject the 404 to 502.
// tests the ctx.Next inside the Hosts switch provider.
ctx.StatusCode(iris.StatusBadGateway)
ctx.WriteString("Switcher: Bad Gateway")
})
e := httptest.New(t, switcher)
for i, tt := range tests {
for URL, paths := range tt.Requests {
u, err := url.Parse(URL)
if err != nil {
t.Fatalf("[%d] %v", i, err)
}
targetHost := u.Host
for requestPath, statusCode := range paths {
// url := fmt.Sprintf("http://%s", requestHost)
body := expected(tt.Target, targetHost)
switch statusCode {
case 404:
body = targetHost + " custom not found"
case 502:
body = "Switcher: Bad Gateway"
}
e.GET(requestPath).WithURL(URL).Expect().Status(statusCode).Body().Equal(body)
}
}
}
}
func TestSwitchHostsRedirect(t *testing.T) {
var (
expected = func(appName, host, path string) string {
return fmt.Sprintf("App Name: %s\nHost: %s\nPath: %s", appName, host, path)
}
index = func(ctx iris.Context) {
ctx.WriteString(expected(ctx.Application().String(), ctx.Host(), ctx.Path()))
}
)
mydomain := iris.New().SetName("mydomain")
mydomain.OnAnyErrorCode(func(ctx iris.Context) {
ctx.WriteString("custom: " + iris.StatusText(ctx.GetStatusCode()))
})
mydomain.Get("/", index)
mydomain.Get("/f", index)
tests := []struct {
Pattern string
Target string
Requests testRequests
}{
{
"www.mydomain.com",
"mydomain",
testRequests{
"http://www.mydomain.com": {
"/": iris.StatusOK,
"/f": iris.StatusOK,
"/nf": iris.StatusNotFound,
},
},
},
{
"^test.*$",
"mydomain",
testRequests{
"http://testdomain.com": {
"/": iris.StatusOK,
"/f": iris.StatusOK,
"/nf": iris.StatusNotFound,
},
},
},
// Something like this will panic to protect users:
// {
// ...,
// "^my.*$",
// "mydomain.com",
// ...
//
{
"^www.*$",
"google.com",
testRequests{
"http://www.mydomain.com": {
"/": iris.StatusOK,
},
"http://www.golang.org": {
"/": iris.StatusNotFound, // should give not found because this is not a switcher's web app.
},
},
},
}
var hostsRedirect Hosts
for _, tt := range tests {
hostsRedirect = append(hostsRedirect, Host{tt.Pattern, tt.Target})
}
switcher := Switch(hostsRedirect)
e := httptest.New(t, switcher)
for i, tt := range tests {
for requestURL, paths := range tt.Requests {
u, err := url.Parse(requestURL)
if err != nil {
t.Fatalf("[%d] %v", i, err)
}
targetHost := u.Host
for requestPath, statusCode := range paths {
body := expected(mydomain.String(), targetHost, requestPath)
if statusCode != 200 {
if tt.Target != mydomain.String() { // it's external.
body = "Not Found"
} else {
body = "custom: " + iris.StatusText(statusCode)
}
}
e.GET(requestPath).WithURL(requestURL).Expect().Status(statusCode).Body().Equal(body)
}
}
}
}