Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow relative locations correctly. Fixes #228 #230

Merged
merged 4 commits into from
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2017-07-12 09:10:37 +0300 using RuboCop version 0.49.1.
# on 2018-05-21 20:37:38 +0100 using RuboCop version 0.49.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -15,26 +15,26 @@ Layout/IndentHeredoc:
- 'lib/rack/test/utils.rb'
- 'rack-test.gemspec'

# Offense count: 2
# Offense count: 1
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/rack/test.rb'

# Offense count: 9
# Offense count: 8
Metrics/AbcSize:
Max: 58
Max: 47

# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 173
Max: 174

# Offense count: 5
Metrics/CyclomaticComplexity:
Max: 13
Max: 12

# Offense count: 75
# Offense count: 89
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Expand All @@ -48,11 +48,11 @@ Metrics/MethodLength:
# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 124
Max: 122

# Offense count: 3
Metrics/PerceivedComplexity:
Max: 16
Max: 15

# Offense count: 1
Security/Eval:
Expand Down
8 changes: 7 additions & 1 deletion lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ 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
# Adding two absolute locations returns the right-hand location
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,
'rack.session.options' => last_request.session_options
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ class FakeApp < Sinatra::Base
redirect '/redirected'
end

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

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

get '/absolute/redirect' do
[301, { 'location' => 'https://www.google.com' }, []]
end

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

it 'follows absolute redirects' do
get '/absolute/redirect'
expect(last_response.headers['location']).to be == 'https://www.google.com'
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