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

View File

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