Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from gotjosh/allow-cookies-configuration
Allow configuration of Cookies.
- Loading branch information
Showing
6 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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
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
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[:foo] = 'nomnomnom!' | ||
end | ||
end | ||
|
||
action 'SetWithOptions' do | ||
def call(params) | ||
self.body = 'with options!' | ||
expire_date = Time.parse params[:expires] | ||
|
||
cookies[:foo] = { | ||
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 |
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
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('foo=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("foo=nomnomnom%21; domain=lotusrocks.com; path=/controller; expires=#{next_week.gmtime.rfc2822}; 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 |