Skip to content

Commit

Permalink
Configuration#cookies integration tests and fixture.
Browse files Browse the repository at this point in the history
  • Loading branch information
gotjosh committed Jul 14, 2014
1 parent c6f7c3e commit 16eeaa8
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
59 changes: 59 additions & 0 deletions test/fixtures/cookies/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module CookiesApp
class Application < Lotus::Application
configure do
# Activate Cookies
cookies true

routes do
get '/get_cookies', to: 'cookies#get'
get '/set_cookies', to: 'cookies#set'
get '/set_cookies_with_options', to: 'cookies#set_with_options'
get '/del_cookies', to: 'cookies#del'
end
end

load!
end


module Controllers::Cookies
include CookiesApp::Controller

action 'Get' do
def call(params)
self.body = cookies[:foo]
end
end

action 'Set' do
def call(params)
self.body = 'yummy!'
cookies[:big_cookie] = 'nomnomnom!'
end
end

action 'SetWithOptions' do
def call(params)
self.body = 'with options!'
expire_date = Time.new params[:expires]

cookies[:with_options] = {
value: 'nomnomnom!',
domain: 'lotusrocks.com',
path: '/controller',
expires: expire_date,
secure: true,
httponly: true
}
end
end

action 'Del' do
def call(params)
self.body = 'deleted!'
cookies[:delete] = nil
end
end
end

end
64 changes: 64 additions & 0 deletions test/integration/cookies_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'test_helper'
require 'rack/test'
require 'fixtures/cookies/application'

describe 'Cookies' do
include Rack::Test::Methods

before do
@current_dir = Dir.pwd
Dir.chdir FIXTURES_ROOT.join('cookies')
@app = CookiesApp::Application.new
end

after do
Dir.chdir @current_dir
@current_dir = nil
end

def app
@app
end

def response
last_response
end

def request
last_request
end

it 'sucessfully gets cookies' do
get '/get_cookies', nil, {'HTTP_COOKIE' => 'foo=bar'}

request.cookies.must_equal({ 'foo' => 'bar' })

response.body.must_equal('bar')
response.headers['Set-Cookie'].must_equal('foo=bar')
end

it 'succesfully sets cookies' do
get '/set_cookies'

response.body.must_equal('yummy!')
response.headers['Set-Cookie'].must_equal('big_cookie=nomnomnom%21')
end

it 'sucessfully sets cookies with options' do
next_week = Time.now + 60 * 60 * 24 * 7
get '/set_cookies_with_options', { expires: next_week }

response.body.must_equal('with options!')
response.headers['Set-Cookie'].must_equal("with_options=nomnomnom%21; domain=lotusrocks.com; path=/controller; expires=Wed, 01 Jan 2014 04:00:00 -0000; secure; HttpOnly")
end

it 'sucessfully deletes cookies' do
get '/del_cookies', nil, {'HTTP_COOKIE' => 'foo=bar;delete=cookie'}

request.cookies.must_equal({ 'foo' => 'bar', 'delete' => 'cookie' })

response.body.must_equal('deleted!')
response.headers['Set-Cookie'].must_equal("foo=bar\ndelete=; max-age=0; expires=Thu, 01 Jan 1970 00:00:00 -0000")
end

end

0 comments on commit 16eeaa8

Please sign in to comment.