Skip to content

Commit

Permalink
Add spec tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jun 23, 2012
1 parent 1c69f63 commit fe297b2
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Rakefile
@@ -1,2 +1,9 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = FileList['spec/**/*_spec.rb']
end

task :default => :spec
49 changes: 49 additions & 0 deletions spec/cookie_limiter_spec.rb
@@ -0,0 +1,49 @@
require File.expand_path('../spec_helper.rb', __FILE__)

describe Rack::Policy::CookieLimiter do

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

it "does not meter where the middleware is inserted"

context 'no consent' do
it 'removes cookie session header' do
mock_app with_headers('Set-Cookie' => "google=bot")
get '/'
last_response.should be_ok
last_response.headers['Set-Cookie'].should be_nil
end

it 'revalidates caches' do
mock_app with_headers('Set-Cookie' => "google=bot")
get '/'
last_response.should be_ok
last_response.headers['Cache-control'].should_not be_nil
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
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/
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/
end
end

end # Rack::Policy::CookieLimiter
45 changes: 45 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,45 @@
require 'rubygems'

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'rspec'
require 'rack/test'
require 'rack/policy'

module DummyApp
def self.call(env)
Thread.current[:last_env] = env
[200, {'Content-Type' => 'text/plain'}, ['ok']]
end
end

module TestHelpers
def app
@app || mock_app(DummyApp)
end

def mock_app(app=nil, &block)
app = block if app.nil? and block.arity == 1
if app
klass = described_class
mock_app do
use klass
run app
end
else
@app = Rack::Lint.new Rack::Builder.new(&block).to_app
end
end

def with_headers(headers)
proc { [200, {'Content-Type' => 'text/plain' }.merge(headers), ['ok']] }
end
end

RSpec.configure do |config|
config.order = :rand
config.expect_with :rspec, :stdlib
config.include Rack::Test::Methods
config.include TestHelpers
end

0 comments on commit fe297b2

Please sign in to comment.