1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

Don't allow :session option in body/params arg; misc refactoring

This changes the parent commit's :session option semantics just
slightly. The :session option must be passed as part of the options
Hash -- the body arg is assumed to be params or a POST/PUT body.
Also, the mapping from :session to HTTP_COOKIE has been switched to
'rack.session' - mapping it to HTTP_COOKIE doesn't make any sense.

While here, refactor the make_request method to make it more
obvious that we're really just building up the options Hash for
MockRequest.
This commit is contained in:
Ryan Tomayko 2009-02-26 22:37:53 -08:00
parent dc8b0d26e9
commit 8edb40963c
2 changed files with 16 additions and 20 deletions

View file

@ -14,31 +14,27 @@ for more information.
EOF EOF
end end
def make_request(verb, path, data={}, h=nil) def make_request(verb, path, body=nil, options={})
@app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application) @app = Sinatra::Application if @app.nil? && defined?(Sinatra::Application)
fail "@app not set - cannot make request" if @app.nil? fail "@app not set - cannot make request" if @app.nil?
@request = Rack::MockRequest.new(@app) @request = Rack::MockRequest.new(@app)
options = { :lint => true } options = { :lint => true }.merge(options || {})
session = data[:session] case
session = data[:env][:session] if data[:env] when body.respond_to?(:to_hash)
options['rack.session'] = session unless session.nil? options.merge! body.delete(:env) if body.key?(:env)
options[:input] = param_string(body)
case data when body.respond_to?(:to_str)
when Hash options[:input] = body
if env = data.delete(:env) when body.nil?
options = rack_options(env) options[:input] = ''
end else
options.merge!(h) if h.is_a?(Hash) raise ArgumentError, "body must be a Hash, String, or nil"
options[:input] = param_string(data)
when String
options = rack_options(h) if h.is_a?(Hash)
options[:input] = data
end end
yield @request if block_given? yield @request if block_given?
@response = @request.request(verb, path, options) @response = @request.request(verb, path, rack_options(options))
end end
def get(path, *args, &b) ; make_request('GET', path, *args, &b) ; end def get(path, *args, &b) ; make_request('GET', path, *args, &b) ; end
@ -74,7 +70,7 @@ for more information.
:accept => 'HTTP_ACCEPT', :accept => 'HTTP_ACCEPT',
:agent => 'HTTP_USER_AGENT', :agent => 'HTTP_USER_AGENT',
:host => 'HTTP_HOST', :host => 'HTTP_HOST',
:session => 'HTTP_COOKIE', :session => 'rack.session',
:cookies => 'HTTP_COOKIE', :cookies => 'HTTP_COOKIE',
:content_type => 'CONTENT_TYPE' :content_type => 'CONTENT_TYPE'
} }

View file

@ -93,7 +93,7 @@ describe 'Sinatra::Test' do
assert_equal '1.2.3.4', request['HTTP_HOST'] assert_equal '1.2.3.4', request['HTTP_HOST']
get '/', :env => { :session => 'foo' } get '/', :env => { :session => 'foo' }
assert_equal 'foo', request['HTTP_COOKIE'] assert_equal 'foo', request['rack.session']
get '/', :env => { :cookies => 'foo' } get '/', :env => { :cookies => 'foo' }
assert_equal 'foo', request['HTTP_COOKIE'] assert_equal 'foo', request['HTTP_COOKIE']
@ -117,7 +117,7 @@ describe 'Sinatra::Test' do
browser = Sinatra::TestHarness.new(app) browser = Sinatra::TestHarness.new(app)
browser.get '/' browser.get '/'
browser.post '/', :session => { 'foo' => 'bar' } browser.post '/', {}, :session => { 'foo' => 'bar' }
assert_equal 'bar', browser.response.body assert_equal 'bar', browser.response.body
end end