Skip to content

Commit

Permalink
fix: Unexpected default rack.session
Browse files Browse the repository at this point in the history
Some rack application fail when the `rack.session` already is
initialized, but with an unexpected value. For example, in an Rails
API-only application, unexpected behavior is triggered when a
`rack.session` is present.

This commit changes webmock, not to set a `rack.session` at all, similar
to how webserver do not set a session. The applications or a middleware
sets them up if needed.

Fixes bblimke#985
  • Loading branch information
jgraichen committed Jan 22, 2025
1 parent 9ff63ac commit 5a1cb2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
10 changes: 0 additions & 10 deletions lib/webmock/rack_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,12 @@ def build_rack_env(request)
end
env['rack.url_scheme'] = uri.scheme
env['rack.run_once'] = true
env['rack.session'] = session
env['rack.session.options'] = session_options

headers.each do |k, v|
env["HTTP_#{k.tr('-','_').upcase}"] = v
end

env
end

def session
@session ||= {}
end

def session_options
@session_options ||= {}
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/rack_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@
end
end

it "shouldn't set a rack.session" do
request = WebMock::RequestSignature.new(:get, 'www.example.com/env')
response = @rack_response.evaluate(request)

expect(response.status.first).to eq(200)
body = JSON.parse(response.body)

expect(body).not_to include("rack.session")
expect(body).not_to include("rack.session.options")
end

describe 'rack error output' do
before :each do
@original_stderr = $stderr
Expand Down

0 comments on commit 5a1cb2b

Please sign in to comment.