From 8edb40963c1386b5f368cd4b79d995733a77b881 Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Thu, 26 Feb 2009 22:37:53 -0800 Subject: [PATCH] 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. --- lib/sinatra/test.rb | 32 ++++++++++++++------------------ test/test_test.rb | 4 ++-- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/lib/sinatra/test.rb b/lib/sinatra/test.rb index f4e5bf4e..65c49c51 100644 --- a/lib/sinatra/test.rb +++ b/lib/sinatra/test.rb @@ -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' } diff --git a/test/test_test.rb b/test/test_test.rb index 64ee15a1..8c13303d 100644 --- a/test/test_test.rb +++ b/test/test_test.rb @@ -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