Created
June 17, 2009 17:35
-
-
Save alexch/131374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Failure: | |
test_logout(AppTest) [/Users/chaffee/dev/sessiontest/gist-131374/login_app_test.rb:33]: | |
<"logged out barack"> expected but was | |
<"logged out ">. | |
2) Failure: | |
test_main_page(AppTest) [/Users/chaffee/dev/sessiontest/gist-131374/login_app_test.rb:48]: | |
please log in: <form action='/login' method='get'><input name='username'><input type='submit'></form>. | |
<nil> is not true. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require "sinatra" | |
enable :sessions | |
#use Rack::Session::Cookie | |
#use Rack::Session::Pool | |
get '/' do | |
if session[:username] | |
"hi, #{session[:username]}" | |
else | |
"please log in: <form action='/login' method='get'><input name='username'><input type='submit'></form>" | |
end | |
end | |
get '/login' do | |
session[:username] = params[:username] | |
"logged in as #{session[:username]}" | |
end | |
get '/logout' do | |
old_user = session[:username] | |
session[:username] = nil | |
"logged out #{old_user}" | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RACK_ENV'] = 'test' | |
require 'rubygems' | |
require 'rack' | |
require 'test/unit' | |
require "rack/test" | |
require 'login_app' | |
class AppTest < Test::Unit::TestCase | |
include Rack::Test::Methods | |
def app | |
Sinatra::Application | |
end | |
def session | |
last_request.env['rack.session'] | |
end | |
def test_login | |
get '/login', :username => "barack" | |
assert_equal "logged in as barack", last_response.body | |
assert_equal "barack", session[:username] | |
end | |
def test_logout | |
get '/login', :username => "barack" | |
assert_equal "barack", session[:username] | |
get '/logout' | |
assert_nil session[:username] | |
assert_equal "logged out barack", last_response.body | |
end | |
def test_logout_with_session_passed_explicitly | |
get '/logout', {}, 'rack.session' => {:username => 'barry'} | |
assert_nil session[:username] | |
assert_equal "logged out barry", last_response.body | |
end | |
def test_main_page | |
get '/login', :username => "barack" | |
assert_equal "barack", session[:username] | |
assert_equal "logged in as barack", last_response.body | |
get '/' | |
assert last_response.body =~ /hi, barack/ | |
get '/logout' | |
assert_equal "logged out barack", last_response.body | |
assert_nil session[:username] | |
get '/' | |
assert last_response.body =~ /please log in/ | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment