Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Trying to remove the pain out of session testing

  • Loading branch information...
commit dc8b0d26e9b89121df2a958399c8c5ff6c9a7cbc 1 parent d7eabb9
Simon Rozet sr authored rtomayko committed
Showing with 30 additions and 7 deletions.
  1. +11 −7 lib/sinatra/test.rb
  2. +19 −0 test/test_test.rb
18 lib/sinatra/test.rb
View
@@ -14,13 +14,17 @@ def self.deprecate(framework)
EOF
end
- def make_request(verb, path, data=nil, h=nil)
+ def make_request(verb, path, data={}, h=nil)
@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)
@@ -67,12 +71,12 @@ def respond_to?(symbol, include_private=false)
private
RACK_OPTIONS = {
- :accept => "HTTP_ACCEPT",
- :agent => "HTTP_USER_AGENT",
- :host => "HTTP_HOST",
- :session => "HTTP_COOKIE",
- :cookies => "HTTP_COOKIE",
- :content_type => "CONTENT_TYPE"
+ :accept => 'HTTP_ACCEPT',
+ :agent => 'HTTP_USER_AGENT',
+ :host => 'HTTP_HOST',
+ :session => 'HTTP_COOKIE',
+ :cookies => 'HTTP_COOKIE',
+ :content_type => 'CONTENT_TYPE'
}
def rack_options(opts)
19 test/test_test.rb
View
@@ -102,6 +102,25 @@ def request_params
assert_equal 'text/plain', request['CONTENT_TYPE']
end
+ it 'allow to test session easily' do
+ app = mock_app(Sinatra::Default) {
+ get '/' do
+ session['foo'] = 'bar'
+ 200
+ end
+
+ post '/' do
+ assert_equal 'bar', session['foo']
+ session['foo'] || "blah"
+ end
+ }
+
+ browser = Sinatra::TestHarness.new(app)
+ browser.get '/'
+ browser.post '/', :session => { 'foo' => 'bar' }
+ assert_equal 'bar', browser.response.body
+ end
+
it 'yields the request object to the block before invoking the application' do
called = false
get '/' do |req|
Please sign in to comment.
Something went wrong with that request. Please try again.