Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 8edb40963c1386b5f368cd4b79d995733a77b881 1 parent dc8b0d2
@rtomayko rtomayko authored
Showing with 17 additions and 21 deletions.
  1. +15 −19 lib/sinatra/test.rb
  2. +2 −2 test/test_test.rb
View
34 lib/sinatra/test.rb
@@ -14,31 +14,27 @@ def self.deprecate(framework)
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 }
-
- 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
+ options = { :lint => true }.merge(options || {})
+
+ 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 @@ def respond_to?(symbol, include_private=false)
:accept => 'HTTP_ACCEPT',
:agent => 'HTTP_USER_AGENT',
:host => 'HTTP_HOST',
- :session => 'HTTP_COOKIE',
+ :session => 'rack.session',
:cookies => 'HTTP_COOKIE',
:content_type => 'CONTENT_TYPE'
}
View
4 test/test_test.rb
@@ -93,7 +93,7 @@ def request_params
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 @@ def request_params
browser = Sinatra::TestHarness.new(app)
browser.get '/'
- browser.post '/', :session => { 'foo' => 'bar' }
+ browser.post '/', {}, :session => { 'foo' => 'bar' }
assert_equal 'bar', browser.response.body
end
Please sign in to comment.
Something went wrong with that request. Please try again.