Skip to content

Commit

Permalink
Trying to remove the pain out of session testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sr authored and rtomayko committed Feb 27, 2009
1 parent d7eabb9 commit dc8b0d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/sinatra/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions test/test_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit dc8b0d2

Please sign in to comment.