-
-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathcontext_test.go
More file actions
239 lines (177 loc) · 6.84 KB
/
context_test.go
File metadata and controls
239 lines (177 loc) · 6.84 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package web_test
import (
"crypto/tls"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/getfider/fider/app/models/entity"
. "github.com/getfider/fider/app/pkg/assert"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/web"
)
func newGetContext(rawurl string, headers map[string]string) *web.Context {
u, _ := url.Parse(rawurl)
e := web.New()
res := httptest.NewRecorder()
req := httptest.NewRequest("GET", u.RequestURI(), nil)
req.Host = u.Host
if u.Scheme == "https" {
req.TLS = &tls.ConnectionState{}
}
for k, v := range headers {
req.Header.Set(k, v)
}
return web.NewContext(e, req, res, nil)
}
func newBodyContext(method string, params web.StringMap, body, contentType string) *web.Context {
e := web.New()
res := httptest.NewRecorder()
req := httptest.NewRequest(method, "/some/resource", strings.NewReader(body))
req.Host = "demo.test.fider.io:3000"
req.Header.Set("Content-Type", contentType)
return web.NewContext(e, req, res, params)
}
func TestContextID(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
Expect(ctx.ContextID()).IsNotEmpty()
Expect(ctx.ContextID()).HasLen(32)
}
func TestBaseURL(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "multi"
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
Expect(ctx.BaseURL()).Equals("http://demo.test.fider.io:3000")
}
func TestBaseURL_HTTPS(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "multi"
ctx := newGetContext("https://demo.test.fider.io:3000", nil)
Expect(ctx.BaseURL()).Equals("https://demo.test.fider.io:3000")
}
func TestBaseURL_HTTPS_Proxy(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "multi"
ctx := newGetContext("http://demo.test.fider.io:3000", map[string]string{
"X-Forwarded-Proto": "https",
})
Expect(ctx.BaseURL()).Equals("https://demo.test.fider.io:3000")
}
func TestBaseURL_SingleHostMode(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "single"
env.Config.BaseURL = "https://example.com"
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
Expect(ctx.BaseURL()).Equals("https://example.com")
}
func TestBaseURL_SingleHostMode_WithPath(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "single"
env.Config.BaseURL = "https://example.com/feedback"
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
Expect(ctx.BaseURL()).Equals("https://example.com/feedback")
}
func TestCurrentURL(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://demo.test.fider.io:3000/resource?id=23", nil)
Expect(ctx.Request.URL.String()).Equals("http://demo.test.fider.io:3000/resource?id=23")
}
func TestTenantURL(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://login.test.fider.io:3000", nil)
tenant := &entity.Tenant{
ID: 1,
Subdomain: "theavengers",
}
Expect(web.TenantBaseURL(ctx, tenant)).Equals("http://theavengers.test.fider.io:3000")
}
func TestTenantURL_WithCNAME(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
tenant := &entity.Tenant{
ID: 1,
Subdomain: "theavengers",
CNAME: "feedback.theavengers.com",
}
Expect(web.TenantBaseURL(ctx, tenant)).Equals("http://feedback.theavengers.com:3000")
}
func TestTenantURL_SingleHostMode(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "single"
ctx := newGetContext("http://demo.test.fider.io:3000", nil)
tenant := &entity.Tenant{
ID: 1,
Subdomain: "theavengers",
}
Expect(web.TenantBaseURL(ctx, tenant)).Equals("https://test.fider.io:3000")
}
func TestAssetsURL_SingleHostMode(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "single"
ctx := newGetContext("http://feedback.theavengers.com:3000", nil)
ctx.SetTenant(&entity.Tenant{
ID: 1,
Subdomain: "theavengers",
})
Expect(web.AssetsURL(ctx, "/assets/main.js")).Equals("https://test.fider.io:3000/assets/main.js")
Expect(web.AssetsURL(ctx, "/assets/main.css")).Equals("https://test.fider.io:3000/assets/main.css")
env.Config.CDN.Host = "fidercdn.com"
Expect(web.AssetsURL(ctx, "/assets/main.js")).Equals("http://fidercdn.com/assets/main.js")
Expect(web.AssetsURL(ctx, "/assets/main.css")).Equals("http://fidercdn.com/assets/main.css")
}
func TestAssetsURL_MultiHostMode(t *testing.T) {
RegisterT(t)
env.Config.HostMode = "multi"
ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
ctx.SetTenant(&entity.Tenant{
ID: 1,
Subdomain: "theavengers",
CNAME: "feedback.theavengers.com",
})
Expect(web.AssetsURL(ctx, "/assets/main.js")).Equals("http://theavengers.test.fider.io:3000/assets/main.js")
Expect(web.AssetsURL(ctx, "/assets/main.css")).Equals("http://theavengers.test.fider.io:3000/assets/main.css")
env.Config.CDN.Host = "fidercdn.com"
Expect(web.AssetsURL(ctx, "/assets/main.js")).Equals("http://theavengers.fidercdn.com/assets/main.js")
Expect(web.AssetsURL(ctx, "/assets/main.css")).Equals("http://theavengers.fidercdn.com/assets/main.css")
}
func TestCanonicalURL_SameDomain(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
ctx.SetCanonicalURL("")
Expect(ctx.Value("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000`)
ctx.SetCanonicalURL("/some-url")
Expect(ctx.Value("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000/some-url`)
ctx.SetCanonicalURL("/some-other-url")
Expect(ctx.Value("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000/some-other-url`)
ctx.SetCanonicalURL("page-b/abc.html")
Expect(ctx.Value("Canonical-URL")).Equals(`http://theavengers.test.fider.io:3000/page-b/abc.html`)
}
func TestCanonicalURL_DifferentDomain(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://theavengers.test.fider.io:3000", nil)
ctx.SetCanonicalURL("http://feedback.theavengers.com/some-url")
Expect(ctx.Value("Canonical-URL")).Equals(`http://feedback.theavengers.com/some-url`)
ctx.SetCanonicalURL("")
Expect(ctx.Value("Canonical-URL")).Equals(`http://feedback.theavengers.com`)
ctx.SetCanonicalURL("/some-other-url")
Expect(ctx.Value("Canonical-URL")).Equals(`http://feedback.theavengers.com/some-other-url`)
ctx.SetCanonicalURL("page-b/abc.html")
Expect(ctx.Value("Canonical-URL")).Equals(`http://feedback.theavengers.com/page-b/abc.html`)
}
func TestGetOAuthBaseURL(t *testing.T) {
RegisterT(t)
ctx := newGetContext("https://mydomain.com/hello-world", nil)
env.Config.HostMode = "multi"
Expect(web.OAuthBaseURL(ctx)).Equals("https://login.test.fider.io")
env.Config.HostMode = "single"
Expect(web.OAuthBaseURL(ctx)).Equals("https://test.fider.io:3000")
}
func TestGetOAuthBaseURL_WithPort(t *testing.T) {
RegisterT(t)
ctx := newGetContext("http://demo.test.fider.io:3000/hello-world", nil)
env.Config.HostMode = "multi"
Expect(web.OAuthBaseURL(ctx)).Equals("http://login.test.fider.io:3000")
env.Config.HostMode = "single"
Expect(web.OAuthBaseURL(ctx)).Equals("https://test.fider.io:3000")
}