Skip to content

Commit

Permalink
Merge 19b6d20 into 7717fdb
Browse files Browse the repository at this point in the history
  • Loading branch information
BMorearty committed Feb 22, 2020
2 parents 7717fdb + 19b6d20 commit a30f6c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
11 changes: 6 additions & 5 deletions lib/async/http/relative_location.rb
Expand Up @@ -32,8 +32,9 @@ class TooManyRedirects < StandardError
# A client wrapper which transparently handles both relative and absolute redirects to a given maximum number of hops.
class RelativeLocation < ::Protocol::HTTP::Middleware
DEFAULT_METHOD = GET

def initialize(app, maximum_hops = 4)

# maximum_hops is the max number of redirects. Set to 0 to allow 1 request with no redirects.
def initialize(app, maximum_hops = 3)
super(app)

@maximum_hops = maximum_hops
Expand All @@ -48,11 +49,11 @@ def call(request)
# We need to cache the body as it might be submitted multiple times.
request.finish

while hops < @maximum_hops
while hops <= @maximum_hops
response = super(request)
hops += 1


if response.redirection?
hops += 1
response.finish

location = response.headers['location']
Expand Down
25 changes: 18 additions & 7 deletions spec/async/http/relative_location_spec.rb
Expand Up @@ -45,10 +45,10 @@
let(:server) do
Async::HTTP::Server.for(endpoint) do |request|
case request.path
when '/home'
Protocol::HTTP::Response[301, {'location' => '/'}, []]
when '/'
Protocol::HTTP::Response[301, {'location' => '/index.html'}, []]
when '/forever'
Protocol::HTTP::Response[301, {'location' => '/forever'}, []]
when '/index.html'
Protocol::HTTP::Response[200, {}, [request.method]]
end
Expand All @@ -61,11 +61,22 @@
expect(response).to be_success
expect(response.read).to be == "GET"
end

it 'should fail with maximum redirects' do
expect{
response = subject.get('/forever')
}.to raise_error(Async::HTTP::TooManyRedirects, /maximum/)

context 'limiting redirects' do
subject {described_class.new(client, 1)}

it 'should allow the maximum number of redirects' do
response = subject.get('/')

response.finish
expect(response).to be_success
end

it 'should fail with maximum redirects' do
expect{
response = subject.get('/home')
}.to raise_error(Async::HTTP::TooManyRedirects, /maximum/)
end
end
end

Expand Down

0 comments on commit a30f6c3

Please sign in to comment.