-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostroute_test.go
185 lines (146 loc) · 5.11 KB
/
hostroute_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
package hostroute
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func defineHost1Routes(g *gin.RouterGroup) {
g.GET("", func(c *gin.Context) {
c.String(http.StatusOK, "Hello from host1")
})
g.GET("/hi", func(c *gin.Context) {
c.String(http.StatusOK, "Hi from host1")
})
}
func defineHost2Routes(g *gin.RouterGroup) {
g.GET("", func(c *gin.Context) {
c.String(http.StatusOK, "Hello from host2")
})
g.GET("/hi", func(c *gin.Context) {
c.String(http.StatusOK, "Hi from host2")
})
}
func noRouteSpecifier(_ string, r *gin.Engine) error {
r.NoRoute(noRouteHandler)
return nil
}
func noRouteHandler(c *gin.Context) {
c.String(http.StatusNotFound, "No known route")
}
func TestHostBasedRouting(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.Default()
r.NoRoute(noRouteHandler)
hostConfigs := []HostConfig{
{Host: "host1.com", Prefix: "1", RouterFactory: defineHost1Routes},
{Host: "host2.com", Prefix: "2", RouterFactory: defineHost2Routes},
}
genericHosts := []string{"host3.com", "host4.com"}
err := SetupHostBasedRoutes(r, hostConfigs, genericHosts, true, noRouteSpecifier)
require.NoError(t, err)
server := httptest.NewServer(r)
defer server.Close()
tests := []struct {
host, path string
expected string
statusCode int
}{
{"host1.com", "/", "Hello from host1", http.StatusOK},
{"host1.com", "/hi", "Hi from host1", http.StatusOK},
{"host1.com", "/unknown", "No known route", http.StatusNotFound},
{"host2.com", "/", "Hello from host2", http.StatusOK},
{"host2.com", "/hi", "Hi from host2", http.StatusOK},
{"host2.com", "/unknown", "No known route", http.StatusNotFound},
{"host3.com", "/1", "Hello from host1", http.StatusOK},
{"host3.com", "/1/hi", "Hi from host1", http.StatusOK},
{"host3.com", "/2", "Hello from host2", http.StatusOK},
{"host3.com", "/2/hi", "Hi from host2", http.StatusOK},
{"host3.com", "/unknown", "No known route", http.StatusNotFound},
{"host4.com", "/1", "Hello from host1", http.StatusOK},
{"host4.com", "/1/hi", "Hi from host1", http.StatusOK},
{"host4.com", "/2", "Hello from host2", http.StatusOK},
{"host4.com", "/2/hi", "Hi from host2", http.StatusOK},
{"host4.com", "/unknown", "No known route", http.StatusNotFound},
{"unknown.com", "/", "Unknown host", http.StatusNotFound},
}
client := &http.Client{}
for _, tt := range tests {
t.Run(fmt.Sprintf("Host: %s, Path: %s", tt.host, tt.path), func(t *testing.T) {
req, _ := http.NewRequest("GET", server.URL+tt.path, nil)
req.Host = tt.host
var resp *http.Response
resp, err = client.Do(req)
require.NoError(t, err)
defer func() {
err = resp.Body.Close()
require.NoError(t, err)
}()
var body []byte
body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, tt.statusCode, resp.StatusCode)
assert.Equal(t, tt.expected, string(body))
})
}
}
func TestHostBasedRoutingWithoutSecure(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.Default()
r.NoRoute(noRouteHandler)
hostConfigs := []HostConfig{
{Host: "host1.com", Prefix: "1", RouterFactory: defineHost1Routes},
{Host: "host2.com", Prefix: "2", RouterFactory: defineHost2Routes},
}
genericHosts := []string{"host3.com", "host4.com"}
err := SetupHostBasedRoutes(r, hostConfigs, genericHosts, false, noRouteSpecifier)
require.NoError(t, err)
server := httptest.NewServer(r)
defer server.Close()
tests := []struct {
host, path string
expected string
statusCode int
}{
{"host1.com", "/", "Hello from host1", http.StatusOK},
{"host1.com", "/hi", "Hi from host1", http.StatusOK},
{"host1.com", "/unknown", "No known route", http.StatusNotFound},
{"host2.com", "/", "Hello from host2", http.StatusOK},
{"host2.com", "/hi", "Hi from host2", http.StatusOK},
{"host2.com", "/unknown", "No known route", http.StatusNotFound},
{"host3.com", "/1", "Hello from host1", http.StatusOK},
{"host3.com", "/1/hi", "Hi from host1", http.StatusOK},
{"host3.com", "/2", "Hello from host2", http.StatusOK},
{"host3.com", "/2/hi", "Hi from host2", http.StatusOK},
{"host3.com", "/unknown", "No known route", http.StatusNotFound},
{"host4.com", "/1", "Hello from host1", http.StatusOK},
{"host4.com", "/1/hi", "Hi from host1", http.StatusOK},
{"host4.com", "/2", "Hello from host2", http.StatusOK},
{"host4.com", "/2/hi", "Hi from host2", http.StatusOK},
{"host4.com", "/unknown", "No known route", http.StatusNotFound},
{"unknown.com", "/", "No known route", http.StatusNotFound},
}
client := &http.Client{}
for _, tt := range tests {
t.Run(fmt.Sprintf("Host: %s, Path: %s", tt.host, tt.path), func(t *testing.T) {
req, _ := http.NewRequest("GET", server.URL+tt.path, nil)
req.Host = tt.host
var resp *http.Response
resp, err = client.Do(req)
require.NoError(t, err)
defer func() {
err = resp.Body.Close()
require.NoError(t, err)
}()
var body []byte
body, err = io.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, tt.statusCode, resp.StatusCode)
assert.Equal(t, tt.expected, string(body))
})
}
}