-
Notifications
You must be signed in to change notification settings - Fork 319
/
redirector_spec.rb
387 lines (308 loc) · 13.2 KB
/
redirector_spec.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# frozen_string_literal: true
RSpec.describe HTTP::Redirector do
def simple_response(status, body = "", headers = {})
HTTP::Response.new(
:status => status,
:version => "1.1",
:headers => headers,
:body => body,
:request => HTTP::Request.new(:verb => :get, :uri => "http://example.com")
)
end
def redirect_response(status, location)
simple_response status, "", "Location" => location
end
describe "#strict" do
subject { redirector.strict }
context "by default" do
let(:redirector) { described_class.new }
it { is_expected.to be true }
end
end
describe "#max_hops" do
subject { redirector.max_hops }
context "by default" do
let(:redirector) { described_class.new }
it { is_expected.to eq 5 }
end
end
describe "#perform" do
let(:options) { {} }
let(:redirector) { described_class.new options }
it "fails with TooManyRedirectsError if max hops reached" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = proc { |prev_req| redirect_response(301, "#{prev_req.uri}/1") }
expect { redirector.perform(req, res.call(req), &res) }.
to raise_error HTTP::Redirector::TooManyRedirectsError
end
it "fails with EndlessRedirectError if endless loop detected" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response(301, req.uri)
expect { redirector.perform(req, res) { res } }.
to raise_error HTTP::Redirector::EndlessRedirectError
end
it "fails with StateError if there were no Location header" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = simple_response(301)
expect { |b| redirector.perform(req, res, &b) }.
to raise_error HTTP::StateError
end
it "returns first non-redirect response" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
hops = [
redirect_response(301, "http://example.com/1"),
redirect_response(301, "http://example.com/2"),
redirect_response(301, "http://example.com/3"),
simple_response(200, "foo"),
redirect_response(301, "http://example.com/4"),
simple_response(200, "bar")
]
res = redirector.perform(req, hops.shift) { hops.shift }
expect(res.to_s).to eq "foo"
end
context "following 300 redirect" do
context "with strict mode" do
let(:options) { {:strict => true} }
it "it follows with original verb if it's safe" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "raises StateError if original request was PUT" do
req = HTTP::Request.new :verb => :put, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
it "raises StateError if original request was POST" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
it "raises StateError if original request was DELETE" do
req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
end
context "with non-strict mode" do
let(:options) { {:strict => false} }
it "it follows with original verb if it's safe" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "it follows with GET if original request was PUT" do
req = HTTP::Request.new :verb => :put, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "it follows with GET if original request was POST" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "it follows with GET if original request was DELETE" do
req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"
res = redirect_response 300, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
end
end
context "following 301 redirect" do
context "with strict mode" do
let(:options) { {:strict => true} }
it "it follows with original verb if it's safe" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "raises StateError if original request was PUT" do
req = HTTP::Request.new :verb => :put, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
it "raises StateError if original request was POST" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
it "raises StateError if original request was DELETE" do
req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
end
context "with non-strict mode" do
let(:options) { {:strict => false} }
it "it follows with original verb if it's safe" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "it follows with GET if original request was PUT" do
req = HTTP::Request.new :verb => :put, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "it follows with GET if original request was POST" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "it follows with GET if original request was DELETE" do
req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"
res = redirect_response 301, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
end
end
context "following 302 redirect" do
context "with strict mode" do
let(:options) { {:strict => true} }
it "it follows with original verb if it's safe" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "raises StateError if original request was PUT" do
req = HTTP::Request.new :verb => :put, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
it "raises StateError if original request was POST" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
it "raises StateError if original request was DELETE" do
req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
expect { redirector.perform(req, res) { simple_response 200 } }.
to raise_error HTTP::StateError
end
end
context "with non-strict mode" do
let(:options) { {:strict => false} }
it "it follows with original verb if it's safe" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "it follows with GET if original request was PUT" do
req = HTTP::Request.new :verb => :put, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "it follows with GET if original request was POST" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "it follows with GET if original request was DELETE" do
req = HTTP::Request.new :verb => :delete, :uri => "http://example.com"
res = redirect_response 302, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
end
end
context "following 303 redirect" do
it "follows with HEAD if original request was HEAD" do
req = HTTP::Request.new :verb => :head, :uri => "http://example.com"
res = redirect_response 303, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :head
simple_response 200
end
end
it "follows with GET if original request was GET" do
req = HTTP::Request.new :verb => :get, :uri => "http://example.com"
res = redirect_response 303, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
it "follows with GET if original request was neither GET nor HEAD" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 303, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :get
simple_response 200
end
end
end
context "following 307 redirect" do
it "follows with original request's verb" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 307, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :post
simple_response 200
end
end
end
context "following 308 redirect" do
it "follows with original request's verb" do
req = HTTP::Request.new :verb => :post, :uri => "http://example.com"
res = redirect_response 308, "http://example.com/1"
redirector.perform(req, res) do |prev_req, _|
expect(prev_req.verb).to be :post
simple_response 200
end
end
end
end
end