forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_test.rb
284 lines (237 loc) · 8.3 KB
/
app_test.rb
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require 'test_helper'
require 'rack/test'
require 'app'
class AppTest < MiniTest::Spec
include Rack::Test::Methods
MODERN_BROWSER = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
def app
App
end
before do
current_session.env('HTTPS', 'on')
end
it 'redirects to HTTPS' do
get 'http://example.com/test?q=1', {}, 'HTTPS' => 'off'
assert last_response.redirect?
assert_equal 'https://example.com/test?q=1', last_response['Location']
end
it 'returns HSTS header' do
get 'https://example.com/test'
assert_equal 'max-age=31536000; includeSubDomains', last_response['Strict-Transport-Security']
end
describe "/" do
it "works" do
get '/'
assert last_response.ok?
end
it "redirects to /#q= when there is a 'q' query param" do
get '/search', q: 'foo'
assert last_response.redirect?
assert_equal 'https://example.org/#q=foo', last_response['Location']
end
it "redirects without the query string" do
get '/', foo: 'bar'
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
end
end
describe "/[static-page]" do
it "redirects to /#/[static-page] by default" do
%w(offline about news help).each do |page|
get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal "https://example.org/#/#{page}", last_response['Location']
end
end
it "redirects via JS cookie when a cookie exists" do
%w(offline about news help).each do |page|
set_cookie('foo=bar')
get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
assert last_response['Set-Cookie'].start_with?("initial_path=%2F#{page}; path=/; expires=")
end
end
end
describe "/search" do
it "redirects to /#q=" do
get '/search'
assert last_response.redirect?
assert_equal 'https://example.org/#q=', last_response['Location']
get '/search', q: 'foo'
assert last_response.redirect?
assert_equal 'https://example.org/#q=foo', last_response['Location']
end
end
describe "/[doc]" do
it "renders when the doc exists and isn't enabled" do
set_cookie('docs=html~5')
get '/html~4/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.ok?
end
it "renders when the doc exists, is a default doc, and all docs are enabled" do
set_cookie('docs=')
get '/css/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.ok?
end
it "redirects via JS cookie when the doc exists and is enabled" do
set_cookie('docs=html~5')
get '/html~5/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%7E5%2F; path=/; expires=")
end
it "renders when the doc exists, has no version in the path, and isn't enabled" do
get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.ok?
end
it "redirects via JS cookie when the doc exists, has no version in the path, and a version is enabled" do
set_cookie('docs=html~5')
get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%2F; path=/; expires=")
end
it "renders when the doc exists and is enabled, and the request is from Googlebot" do
set_cookie('docs=html')
get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +https://www.google.com/bot.html)'
assert last_response.ok?
end
it "returns 404 when the doc doesn't exist" do
get '/html~6/'
assert last_response.not_found?
end
it "decodes '~' properly" do
get '/html%7E5/'
assert last_response.ok?
get '/html%7E42/'
assert last_response.not_found?
end
it "redirects with trailing slash" do
get '/html'
assert last_response.redirect?
assert_equal 'https://example.org/html/', last_response['Location']
get '/html', bar: 'baz'
assert last_response.redirect?
assert_equal 'https://example.org/html/?bar=baz', last_response['Location']
end
it "redirects old docs" do
get '/iojs/'
assert last_response.redirect?
assert_equal 'https://example.org/node/', last_response['Location']
end
end
describe "/[doc]-[type]" do
it "works when the doc exists" do
get '/html~4-foo-bar_42/'
assert last_response.ok?
assert_includes last_response.body, 'data-doc="{"name":"HTML","slug":"html~4"'
end
it "works when the doc has no version in the path and a version exists" do
get '/html-foo-bar_42/'
assert last_response.ok?
assert_includes last_response.body, 'data-doc="{"name":"HTML","slug":"html~5"'
end
it "returns 404 when the type is blank" do
get '/css-/'
assert last_response.not_found?
end
it "returns 404 when the type is not alpha-numeric" do
get '/css-foo:bar/'
assert last_response.not_found?
end
it "returns 404 when the doc doesn't exist" do
get '/html~6-bar/'
assert last_response.not_found?
end
it "redirects with trailing slash" do
get '/css-foo'
assert last_response.redirect?
assert_equal 'https://example.org/css-foo/', last_response['Location']
get '/css-foo', bar: 'baz'
assert last_response.redirect?
assert_equal 'https://example.org/css-foo/?bar=baz', last_response['Location']
end
it "redirects old docs" do
get '/yii1-foo/'
assert last_response.redirect?
assert_equal 'https://example.org/yii~1.1-foo/', last_response['Location']
end
end
describe "/[doc+type]/[path]" do
it "works when the doc exists" do
get '/css/foo'
assert last_response.ok?
get '/css-bar/foo'
assert last_response.ok?
end
it "returns 404 when the doc doesn't exist" do
get '/foo/bar'
assert last_response.not_found?
end
it "redirects without trailing slash" do
get '/css/foo/'
assert last_response.redirect?
assert_equal 'https://example.org/css/foo', last_response['Location']
get '/css/foo/', bar: 'baz'
assert last_response.redirect?
assert_equal 'https://example.org/css/foo?bar=baz', last_response['Location']
end
it "redirects old docs" do
get '/python2/foo'
assert last_response.redirect?
assert_equal 'https://example.org/python~2.7/foo', last_response['Location']
end
end
describe "/docs.json" do
it "returns to the asset path" do
get '/docs.json'
assert last_response.redirect?
assert_equal 'https://example.org/assets/docs.json', last_response['Location']
end
end
describe "/application.js" do
it "returns to the asset path" do
get '/application.js'
assert last_response.redirect?
assert_equal 'https://example.org/assets/application.js', last_response['Location']
end
end
describe "/application.css" do
it "returns to the asset path" do
get '/application.css'
assert last_response.redirect?
assert_equal 'https://example.org/assets/application.css', last_response['Location']
end
end
describe "/feed" do
it "returns an atom feed" do
get '/feed'
assert last_response.ok?
assert_equal 'application/atom+xml', last_response['Content-Type']
get '/feed.atom'
assert last_response.ok?
assert_equal 'application/atom+xml', last_response['Content-Type']
end
end
describe "/s/[link]" do
it "redirects" do
%w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
get "/s/#{link}"
assert last_response.redirect?
end
end
end
describe "/ping" do
it "works" do
get '/ping'
assert last_response.ok?
end
end
describe "404" do
it "works" do
get '/foo'
assert last_response.not_found?
end
end
end