Skip to content

Commit

Permalink
Update to use new syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Nov 25, 2014
1 parent 3139e30 commit 045b9f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
49 changes: 24 additions & 25 deletions spec/cookie_limiter_spec.rb
@@ -1,12 +1,12 @@
# -*- encoding: utf-8 -*-
# coding: utf-8 -*-

require File.expand_path('../spec_helper.rb', __FILE__)

describe Rack::Policy::CookieLimiter do
RSpec.describe Rack::Policy::CookieLimiter do

it 'preserves normal requests' do
get('/').should be_ok
last_response.body.should == 'ok'
expect(get('/')).to be_ok
expect(last_response.body).to eq('ok')
end

it "does not meter where the middleware is inserted" do
Expand All @@ -16,8 +16,8 @@
run DummyApp
}
get '/'
last_response.should be_ok
last_response.headers['Set-Cookie'].should be_nil
expect(last_response).to be_ok
expect(last_response.headers['Set-Cookie']).to eq(nil)
end

context 'no consent' do
Expand All @@ -27,8 +27,8 @@
run DummyApp
}
request '/'
last_response.should be_ok
last_response.headers['Set-Cookie'].should be_nil
expect(last_response).to be_ok
expect(last_response.headers['Set-Cookie']).to eq(nil)
end

it 'clears all the cookies' do
Expand All @@ -38,7 +38,7 @@
}
set_cookie ["foo=1", "bar=2"]
request '/'
last_request.cookies.should == {}
expect(last_request.cookies).to eq({})
end

it 'revalidates caches' do
Expand All @@ -47,29 +47,29 @@
run DummyApp
}
request '/'
last_response.should be_ok
last_response.headers['Cache-Control'].should =~ /must-revalidate/
expect(last_response).to be_ok
expect(last_response.headers['Cache-Control']).to match(/must-revalidate/)
end
end

context 'with consent' do
it 'preserves cookie header' do
mock_app with_headers('Set-Cookie' => "cookie_limiter=true; path=/;")
get '/'
last_response.should be_ok
last_response.headers['Set-Cookie'].should_not be_nil
expect(last_response).to be_ok
expect(last_response.headers['Set-Cookie']).to_not eq(nil)
end

it 'sets consent cookie' do
mock_app with_headers('Set-Cookie' => "cookie_limiter=true; path=/;")
get '/'
last_response.headers['Set-Cookie'].should =~ /cookie_limiter/
expect(last_response.headers['Set-Cookie']).to match(/cookie_limiter/)
end

it 'preserves other session cookies' do
mock_app with_headers('Set-Cookie' => "cookie_limiter=true; path=/;\ngithub.com=bot")
get '/'
last_response.headers['Set-Cookie'].should =~ /github.com=bot/
expect(last_response.headers['Set-Cookie']).to match(/github.com=bot/)
end

context 'token' do
Expand All @@ -80,7 +80,7 @@
}
set_cookie ["foo=1", "bar=2", "consent=true"]
request '/'
last_request.cookies.should == {'foo'=>'1', 'bar'=>'2', 'consent'=>'true'}
expect(last_request.cookies).to eq({'foo'=>'1', 'bar'=>'2', 'consent'=>'true'})
end
end
end
Expand All @@ -92,7 +92,7 @@
run DummyApp
}
request '/'
last_request.env.should have_key('rack-policy.consent')
expect(last_request.env).to have_key('rack-policy.consent')
end

it "assigns value for the consent variable" do
Expand All @@ -102,7 +102,7 @@
}
set_cookie ["consent=true"]
request '/'
last_request.env['rack-policy.consent'].should == 'true'
expect(last_request.env['rack-policy.consent']).to eq('true')
end
end

Expand All @@ -113,23 +113,22 @@
run DummyApp
}
head '/'
last_response.should be_ok
expect(last_response).to be_ok
end

it "strips content headers for no content" do
mock_app with_status(204)
get '/'
last_response.headers['Content-Type'].should be_nil
last_response.headers['Content-Length'].should be_nil
last_response.body.should be_empty
expect(last_response.headers['Content-Type']).to eq(nil)
expect(last_response.headers['Content-Length']).to eq(nil)
expect(last_response.body).to be_empty
end

it "strips headers for information request" do
mock_app with_status(102)
get '/'
last_response.headers['Content-Length'].should be_nil
last_response.body.should be_empty
expect(last_response.headers['Content-Length']).to eq(nil)
expect(last_response.body).to be_empty
end
end

end # Rack::Policy::CookieLimiter
24 changes: 11 additions & 13 deletions spec/helpers_spec.rb
@@ -1,4 +1,4 @@
# -*- encoding: utf-8 -*-
# coding: utf-8

require File.expand_path('../spec_helper.rb', __FILE__)

Expand All @@ -16,34 +16,32 @@ def initialize; @env = {}; end
end
end

describe Rack::Policy::Helpers do

RSpec.describe Rack::Policy::Helpers do
let(:helper_test) { HelperTest.new }

before do
helper_test.request.env.stub(:has_key?).and_return true
allow(helper_test.request.env).to receive(:has_key?).and_return(true)
end

it "guards against missing key" do
helper_test.request.env.stub(:has_key?).and_return false
helper_test.cookies_accepted?.should be_false
allow(helper_test.request.env).to receive(:has_key?).and_return(false)
expect(helper_test.cookies_accepted?).to eq(false)
end

it "doesn't accept cookies" do
helper_test.request.env.stub(:[]).with('rack-policy.consent') { nil }
helper_test.cookies_accepted?.should be_false
allow(helper_test.request.env).to receive(:[]).with('rack-policy.consent') { nil }
expect(helper_test.cookies_accepted?).to eq(false)
end

it "accepts cookies" do
helper_test.request.env.stub(:[]).with('rack-policy.consent') { 'true' }
helper_test.cookies_accepted?.should be_true
allow(helper_test.request.env).to receive(:[]).with('rack-policy.consent') { 'true' }
expect(helper_test.cookies_accepted?).to eq(true)
end

it "yields to the block" do
helper_test.request.env.stub(:[]).with('rack-policy.consent') { 'true' }
allow(helper_test.request.env).to receive(:[]).with('rack-policy.consent') { 'true' }
block = Proc.new { 'Accepted'}
helper_test.should_receive(:cookies_accepted?).and_yield(&block)
expect(helper_test).to receive(:cookies_accepted?).and_yield(&block)
helper_test.cookies_accepted?(&block)
end

end # Rack::Policy::Helpers

0 comments on commit 045b9f5

Please sign in to comment.