From 84a7c393f7cdd80618bf8668f3d89d3a14aaddb8 Mon Sep 17 00:00:00 2001 From: Jan Graichen Date: Wed, 22 Jan 2025 12:28:53 +0100 Subject: [PATCH] fix: Unexpected default rack.session Some rack applications fail when the `rack.session` is already initialized, but with an unexpected value. For example, in a 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 web servers do not set a session. The application or a middleware sets them up if needed. Fixes #985 --- lib/webmock/rack_response.rb | 10 ---------- spec/unit/rack_response_spec.rb | 11 +++++++++++ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/webmock/rack_response.rb b/lib/webmock/rack_response.rb index 16882836..5f4aadba 100644 --- a/lib/webmock/rack_response.rb +++ b/lib/webmock/rack_response.rb @@ -52,8 +52,6 @@ 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 @@ -61,13 +59,5 @@ def build_rack_env(request) env end - - def session - @session ||= {} - end - - def session_options - @session_options ||= {} - end end end diff --git a/spec/unit/rack_response_spec.rb b/spec/unit/rack_response_spec.rb index 3a505ff1..a5f69581 100644 --- a/spec/unit/rack_response_spec.rb +++ b/spec/unit/rack_response_spec.rb @@ -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