-
Notifications
You must be signed in to change notification settings - Fork 557
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
Setting rack.session to hash by default when using to_rack #985
Comments
perhaps |
I have a single file test case here: # frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'json'
gem 'rails', '~> 7.0'
gem 'rspec'
gem 'webmock'
end
ENV['RAILS_ENV'] ||= 'test'
require 'action_controller/railtie'
class Application < Rails::Application
config.logger = Logger.new($stdout)
Rails.logger = config.logger
config.action_dispatch.show_exceptions = :none
routes.draw do
get '/' => 'test#index'
end
end
class TestController < ActionController::Base
include Rails.application.routes.url_helpers
def index
render plain: 'Hello World'
end
end
require 'net/http'
require 'rspec/autorun'
require 'webmock/rspec'
WebMock.enable!
RSpec.configure do |config|
config.full_backtrace = true
end
RSpec.describe do
it do
stub_request(:any, /example\.org/)
.to_rack(Rails.application)
response = Net::HTTP.get('example.org', '/')
expect(response).to eq 'Hello World'
end
end Currently, this fails:
|
Does WebMock has to set a |
@jgraichen possibly not. I guess the intention was to have a complete Rack app with session, in case session is not set by the app like Rails does. |
@bblimke Thanks! I wasn't sure if that was intentional, e.g. to inspect the session later on. Would it be possible not to set That would help with another error I got on a Rails API-only app. It doesn't really have any sessions, but tests fail completely because any present I had to change the patch from above to completely strip module WebmockSessionPatch
def build_rack_env(request)
super.tap do |env|
env.delete('rack.session')
env.delete('rack.session.options')
end
end
WebMock::RackResponse.prepend(WebmockSessionPatch)
end |
In rails 7, if there is no session in the request, it now defaults to an object that responds to enabled?
However, because rack.session is set to {} in webmock (WebMock::RackResponse#session), when webmock us used in a way that mounts this rails app (using to_rack), this causes a failure in the following line in the rails app
ActionDispatch::Flash::RequestMethods#commit_flash
where 'session' is defined as
and fetch_header will return the session if already defined in env['rack.session'] therefore 'default_session' is not being called. The return value is therefore an empty hash which does not respond to enabled?
I am not sure if this is really a webmock issue or if rails are being a bit naughty setting rack.session to anything but a hash
The text was updated successfully, but these errors were encountered: