Skip to content
Browse files

Add spec tests.

  • Loading branch information...
1 parent 1c69f63 commit fe297b2b4254fbfcaeaac28e1ea3a46d33ac371f @peter-murach committed
Showing with 101 additions and 0 deletions.
  1. +7 −0 Rakefile
  2. +49 −0 spec/cookie_limiter_spec.rb
  3. +45 −0 spec/spec_helper.rb
View
7 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
View
49 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
View
45 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.
Something went wrong with that request. Please try again.