Skip to content

Commit

Permalink
Follow relative locations correctly. Fixes #228
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 6, 2018
1 parent 72fc72d commit 657ff89
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,13 @@ def follow_redirect!
else
[:get, {}]
end

# Compute the next location by appending the location header with the
# last request, as per https://tools.ietf.org/html/rfc7231#section-7.1.2
next_location = URI.parse(last_request.url) + URI.parse(last_response['Location'])

send(
request_method, last_response['Location'], params,
request_method, next_location.to_s, params,
'HTTP_REFERER' => last_request.url,
'rack.session' => last_request.session
)
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class FakeApp < Sinatra::Base
redirect '/redirected'
end

get '/nested/redirect' do
[301, {'location' => 'redirected'}, []]
end

get '/nested/redirected' do
"Hello World!"
end

post '/redirect' do
if params['status']
redirect to('/redirected'), Integer(params['status'])
Expand Down
10 changes: 10 additions & 0 deletions spec/rack/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ def close
expect(last_request.env['HTTP_REFERER']).to eql('http://example.org/redirect')
end

it 'follows nested redirects' do
get '/nested/redirect'

expect(last_response.headers['location']).to be == 'redirected'
follow_redirect!

expect(last_response).to be_ok
expect(last_request.env['PATH_INFO']).to eq('/nested/redirected')
end

it 'does not include params when following the redirect' do
get '/redirect', 'foo' => 'bar'
follow_redirect!
Expand Down

0 comments on commit 657ff89

Please sign in to comment.