-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathvhost_test.go
More file actions
117 lines (106 loc) · 3.05 KB
/
vhost_test.go
File metadata and controls
117 lines (106 loc) · 3.05 KB
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
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package echo
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
func TestVirtualHostHandler(t *testing.T) {
okHandler := func(c *Context) error { return c.String(http.StatusOK, http.StatusText(http.StatusOK)) }
teapotHandler := func(c *Context) error { return c.String(http.StatusTeapot, http.StatusText(http.StatusTeapot)) }
acceptHandler := func(c *Context) error { return c.String(http.StatusAccepted, http.StatusText(http.StatusAccepted)) }
teapotMiddleware := MiddlewareFunc(func(next HandlerFunc) HandlerFunc { return teapotHandler })
ok := New()
ok.GET("/", okHandler)
ok.GET("/foo", okHandler)
teapot := New()
teapot.GET("/", teapotHandler)
teapot.GET("/foo", teapotHandler)
middle := New()
middle.Use(teapotMiddleware)
middle.GET("/", okHandler)
middle.GET("/foo", okHandler)
virtualHosts := NewVirtualHostHandler(map[string]*Echo{
"ok.com": ok,
"teapot.com": teapot,
"middleware.com": middle,
})
virtualHosts.GET("/", acceptHandler)
virtualHosts.GET("/foo", acceptHandler)
var testCases = []struct {
name string
whenHost string
whenPath string
expectBody string
expectStatus int
}{
{
name: "No Host Root",
whenHost: "",
whenPath: "/",
expectBody: http.StatusText(http.StatusAccepted),
expectStatus: http.StatusAccepted,
},
{
name: "No Host Foo",
whenHost: "",
whenPath: "/foo",
expectBody: http.StatusText(http.StatusAccepted),
expectStatus: http.StatusAccepted,
},
{
name: "OK Host Root",
whenHost: "ok.com",
whenPath: "/",
expectBody: http.StatusText(http.StatusOK),
expectStatus: http.StatusOK,
},
{
name: "OK Host Foo",
whenHost: "ok.com",
whenPath: "/foo",
expectBody: http.StatusText(http.StatusOK),
expectStatus: http.StatusOK,
},
{
name: "Teapot Host Root",
whenHost: "teapot.com",
whenPath: "/",
expectBody: http.StatusText(http.StatusTeapot),
expectStatus: http.StatusTeapot,
},
{
name: "Teapot Host Foo",
whenHost: "teapot.com",
whenPath: "/foo",
expectBody: http.StatusText(http.StatusTeapot),
expectStatus: http.StatusTeapot,
},
{
name: "Middleware Host",
whenHost: "middleware.com",
whenPath: "/",
expectBody: http.StatusText(http.StatusTeapot),
expectStatus: http.StatusTeapot,
},
{
name: "Middleware Host Foo",
whenHost: "middleware.com",
whenPath: "/foo",
expectBody: http.StatusText(http.StatusTeapot),
expectStatus: http.StatusTeapot,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, tc.whenPath, nil)
req.Host = tc.whenHost
rec := httptest.NewRecorder()
virtualHosts.ServeHTTP(rec, req)
assert.Equal(t, tc.expectStatus, rec.Code)
assert.Equal(t, tc.expectBody, rec.Body.String())
})
}
}