Skip to content

Instantly share code, notes, and snippets.

@gotwalt
Created June 9, 2013 13:58
Show Gist options
  • Save gotwalt/5743651 to your computer and use it in GitHub Desktop.
Save gotwalt/5743651 to your computer and use it in GitHub Desktop.
Trying to use Webmock's to_rack functionality and Rails 4 and getting huge errors with the session object? This monkeypatch provides the missing 'rack.session.options' key to the request environment.
require 'webmock/minitest'
module WebMock
class RackResponse < WebMock::Response
def build_rack_env(request)
uri = request.uri
headers = request.headers || {}
body = request.body || ''
env = {
# CGI variables specified by Rack
'REQUEST_METHOD' => request.method.to_s.upcase,
'CONTENT_TYPE' => headers.delete('Content-Type'),
'CONTENT_LENGTH' => body.size,
'PATH_INFO' => uri.path,
'QUERY_STRING' => uri.query || '',
'SERVER_NAME' => uri.host,
'SERVER_PORT' => uri.port,
'SCRIPT_NAME' => ""
}
env['HTTP_AUTHORIZATION'] = 'Basic ' + [uri.userinfo].pack('m').delete("\r\n") if uri.userinfo
# Rack-specific variables
env['rack.input'] = StringIO.new(body)
env['rack.errors'] = $stderr
env['rack.version'] = Rack::VERSION
env['rack.url_scheme'] = uri.scheme
env['rack.run_once'] = true
env['rack.session'] = session
env['rack.session.options'] = session_options # include session options to keep rails 4 from asploding
headers.each do |k, v|
env["HTTP_#{k.tr('-','_').upcase}"] = v
end
env
end
def session_options
@session_options ||= {}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment