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

Save session during follow_redirect! #218

Merged
merged 3 commits into from
Mar 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Metrics/AbcSize:
# Offense count: 2
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 166
Max: 172

# Offense count: 5
Metrics/CyclomaticComplexity:
Expand Down
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## master

* Minor enhancements
* Save `session` during `follow_redirect!`
(Alexander Popov #218)

## 0.8.2 / 2017-11-21

* Bug fixes:
Expand Down
16 changes: 11 additions & 5 deletions lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,17 @@ def follow_redirect!
unless last_response.redirect?
raise Error, 'Last response was not a redirect. Cannot follow_redirect!'
end
if last_response.status == 307
send(last_request.request_method.downcase.to_sym, last_response['Location'], last_request.params, 'HTTP_REFERER' => last_request.url)
else
get(last_response['Location'], {}, 'HTTP_REFERER' => last_request.url)
end
request_method, params =
if last_response.status == 307
[last_request.request_method.downcase.to_sym, last_request.params]
else
[:get, {}]
end
send(
request_method, last_response['Location'], params,
'HTTP_REFERER' => last_request.url,
'rack.session' => last_request.session
)
end

private
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class FakeApp < Sinatra::Base

%i[get put post delete].each do |meth|
send(meth, '/redirected') do
additional_info = meth == :get ? '' : " using #{meth} with #{params}"
additional_info = meth == :get ? ", session #{session}" : " using #{meth} with #{params}"
"You've been redirected" + additional_info
end
end
Expand Down
9 changes: 8 additions & 1 deletion spec/rack/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def close
follow_redirect!

expect(last_response).not_to be_redirect
expect(last_response.body).to eq("You've been redirected")
expect(last_response.body).to eq("You've been redirected, session {}")
expect(last_request.env['HTTP_REFERER']).to eql('http://example.org/redirect')
end

Expand All @@ -373,6 +373,13 @@ def close
expect(last_request.GET).to eq({})
end

it 'includes session when following the redirect' do
get '/redirect', {}, 'rack.session' => { 'foo' => 'bar' }
follow_redirect!

expect(last_response.body).to include('session {"foo"=>"bar"}')
end

it 'raises an error if the last_response is not set' do
expect do
follow_redirect!
Expand Down