-
Notifications
You must be signed in to change notification settings - Fork 21
/
location_test.go
188 lines (162 loc) · 3.34 KB
/
location_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
package location
import (
"crypto/tls"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func init() {
gin.SetMode(gin.TestMode)
}
var defaultHeaders = Headers {
Scheme: "X-Forwarded-Proto",
Host: "X-Forwarded-For",
}
var tests = []struct {
want string
conf Config
req *http.Request
}{
// defaults
{
want: "http://localhost:8080",
conf: DefaultConfig(),
req: &http.Request{
Header: http.Header{},
URL: &url.URL{},
},
},
// url scheme
{
want: "https://localhost:8080",
conf: DefaultConfig(),
req: &http.Request{
Header: http.Header{},
URL: &url.URL{
Scheme: "https",
},
},
},
// x-formward headers
{
want: "https://bar.com/bar",
conf: Config{"http", "foo.com", "/bar", defaultHeaders},
req: &http.Request{
Header: http.Header{
"X-Forwarded-Proto": {"https"},
"X-Forwarded-For": {"bar.com"},
},
URL: &url.URL{},
},
},
// X-Host headers
{
want: "http://bar.com/bar",
conf: Config{"http", "foo.com", "/bar", defaultHeaders},
req: &http.Request{
Header: http.Header{
"X-Host": {"bar.com"},
},
URL: &url.URL{},
},
},
// URL Host
{
want: "http://bar.com/bar",
conf: Config{"http", "foo.com", "/bar", defaultHeaders},
req: &http.Request{
Header: http.Header{},
URL: &url.URL{
Host: "bar.com",
},
},
},
// requests
{
want: "https://baz.com/bar",
conf: Config{"http", "foo.com", "/bar", defaultHeaders},
req: &http.Request{
Proto: "HTTPS://",
Host: "baz.com",
Header: http.Header{},
URL: &url.URL{},
},
},
// tls
{
want: "https://foo.com/bar",
conf: Config{"http", "foo.com", "/bar", defaultHeaders},
req: &http.Request{
TLS: &tls.ConnectionState{},
Header: http.Header{},
URL: &url.URL{},
},
},
// X-Forwarded-Host host header
{
want: "http://bar.com/bar",
conf: Config{"http", "foo.com", "/bar", Headers{
Scheme: "X-Forwarded-Proto",
Host: "X-Forwarded-Host",
}},
req: &http.Request{
Header: http.Header{
"X-Forwarded-Host": {"bar.com"},
},
URL: &url.URL{},
},
},
}
func TestLocation(t *testing.T) {
for _, test := range tests {
c := new(gin.Context)
c.Request = test.req
loc := newLocation(test.conf)
loc.applyToContext(c)
got := Get(c)
if got.String() != test.want {
t.Errorf("wanted location %s, got %s", got.String(), test.want)
}
}
}
func defaultRouter() *gin.Engine {
router := gin.New()
router.Use(Default())
router.GET("/", func(c *gin.Context) {
url := Get(c)
c.String(200, url.String())
})
return router
}
func performRequest(r http.Handler, method string) *httptest.ResponseRecorder {
req, _ := http.NewRequest(method, "/", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
func TestDefault(t *testing.T) {
router := defaultRouter()
w := performRequest(router, "GET")
assert.Equal(t, "http://localhost:8080", w.Body.String())
}
func customRouter(config Config) *gin.Engine {
router := gin.New()
router.Use(New(config))
router.GET("/", func(c *gin.Context) {
url := Get(c)
c.String(200, url.String())
})
return router
}
func TestCustom(t *testing.T) {
router := customRouter(Config{
Scheme: "https",
Host: "foo.com",
Base: "/base",
})
w := performRequest(router, "GET")
assert.Equal(t, "https://foo.com/base", w.Body.String())
}