Skip to content

Instantly share code, notes, and snippets.

@pixeltrix
Created December 9, 2013 02:48
Show Gist options
  • Save pixeltrix/7866720 to your computer and use it in GitHub Desktop.
Save pixeltrix/7866720 to your computer and use it in GitHub Desktop.
ActionDispatch::IntegrationTest showing that sessions, cookies and flash all work
require 'action_controller/railtie'
require 'minitest/autorun'
class TestApp < Rails::Application
config.eager_load = false
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'session'
config.secret_key_base = 'secret'
end
TestApp.initialize!
TestApp.routes.draw do
resources :cookies
resources :flashes
resources :sessions
end
class SessionsController < ActionController::Base
def show
render text: session[key]
end
def update
session[key] = params[key]
head :ok
end
def destroy
session.delete(key)
head :ok
end
protected
def key
params[:id]
end
end
class CookiesController < ActionController::Base
def show
render text: cookies[key]
end
def update
cookies[key] = params[key]
head :ok
end
def destroy
cookies.delete(key)
head :ok
end
protected
def key
params[:id]
end
end
class FlashesController < ActionController::Base
def show
render text: flash[key]
end
def update
flash[key] = params[key]
head :ok
end
protected
def key
params[:id]
end
end
class SessionTest < ActionDispatch::IntegrationTest
def test_session_variables_are_persisted
put '/sessions/foo', foo: 'bar'
assert_equal 200, response.status
get '/sessions/foo'
assert_equal 200, response.status
assert_equal "bar", response.body
end
def test_session_variables_are_deleted
put '/sessions/foo', foo: 'bar'
assert_equal 200, response.status
get '/sessions/foo'
assert_equal 200, response.status
assert_equal "bar", response.body
delete '/sessions/foo'
assert_equal 200, response.status
get '/sessions/foo'
assert_equal 200, response.status
assert_equal " ", response.body
end
end
class CookiesTest < ActionDispatch::IntegrationTest
def test_cookies_variables_are_persisted
put '/cookies/foo', foo: 'bar'
assert_equal 200, response.status
get '/cookies/foo'
assert_equal 200, response.status
assert_equal "bar", response.body
end
def test_cookies_variables_are_deleted
put '/cookies/foo', foo: 'bar'
assert_equal 200, response.status
get '/cookies/foo'
assert_equal 200, response.status
assert_equal "bar", response.body
delete '/cookies/foo'
assert_equal 200, response.status
get '/cookies/foo'
assert_equal 200, response.status
assert_equal " ", response.body
end
end
class FlashTest < ActionDispatch::IntegrationTest
def test_flashes_are_persisted
put '/flashes/foo', foo: 'bar'
assert_equal 200, response.status
get '/flashes/foo'
assert_equal 200, response.status
assert_equal "bar", response.body
end
def test_flashes_are_swept
put '/flashes/foo', foo: 'bar'
assert_equal 200, response.status
get '/flashes/foo'
assert_equal 200, response.status
assert_equal "bar", response.body
get '/flashes/foo'
assert_equal 200, response.status
assert_equal " ", response.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment