racktest_cookie_disabler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Herman verschooten
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # RackTestCookieDisabler
2
+
3
+ When you are testing a web app it is sometiimes necessary to be able to disable cookies to emulate what happens when
4
+ someone whose cookies are disabled uses your app. This gem allows you to disable cookies when using capybara to test your web app.
5
+ It only works for RackTest or RackTest-based drivers such as Mechanize.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'racktest_cookie_disabler'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install racktest_cookie_disabler
20
+
21
+ ## Usage
22
+
23
+ Add RackTestCookieDisabler middleware to rails middleware stack.
24
+ Add the following in`config/environments/test.rb`:
25
+ ```ruby
26
+ [MyRailsApp]::Application.configure do
27
+ ...
28
+ # Add cookie disabler middleware
29
+ config.middleware.insert_after Rack::Lock, RackTestCookieDisabler::Middleware
30
+ ...
31
+ end
32
+ ```
33
+ *Note* Ensure you include racktest_cookie_disabler middleware only for *test* environment
34
+ otherwise you may have issues.
35
+
36
+ In your spec:
37
+
38
+ ```rspec
39
+ scenario "with cookies disabled" do
40
+ page.disable_cookies(true)
41
+ page.visit '/'
42
+ ...
43
+ end
44
+ ```
45
+
46
+ ## How does it work
47
+
48
+ When you call disable_cookies(true) it adds an extra header to your request.
49
+ The middleware detects this header and removes the 'HTTP_COOKIE' from the request.
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,36 @@
1
+ require 'action_controller/railtie'
2
+
3
+ module TestRailsApp
4
+ class Application < Rails::Application
5
+ config.secret_token = '572c86f5ede338bd8aba8dae0fd3a326aabababc98d1e6ce34b9f5'
6
+ if Rails::VERSION::MAJOR > 3
7
+ config.secret_key_base = '6dfb795086781f017c63cadcd2653fac40967ac60f621e6299a0d6d811417156d81efcdf1d234c'
8
+ end
9
+
10
+ routes.draw do
11
+ get '/' => 'test_rails_app/sessions#new'
12
+ get '/test_cookie' => 'test_rails_app/sessions#test_cookie'
13
+ end
14
+ end
15
+
16
+ class SessionsController < ActionController::Base
17
+ def new
18
+ if cookies['cookie_test'].blank?
19
+ cookies['cookie_test'] = Time.now
20
+ redirect_to('/test_cookie')
21
+ else
22
+ render :text => "Cookie found"
23
+ end
24
+ end
25
+
26
+ def test_cookie
27
+ if cookies['cookie_test'].blank?
28
+ render :text => "Cookie not found"
29
+ else
30
+ render :text => "Cookie found"
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ Rails.logger = Logger.new('/dev/null')
@@ -0,0 +1,9 @@
1
+ require "racktest_cookie_disabler/version"
2
+
3
+ module RackTestCookieDisabler
4
+ autoload :Middleware, 'racktest_cookie_disabler/middleware'
5
+ class <<self
6
+ attr_accessor :cookies_disabled
7
+
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module RackTestCookieDisabler
2
+ module Capybara
3
+ def disable_cookies(disable)
4
+ add_header('CAPYBARA_DISABLE_COOKIES',disable)
5
+ end
6
+
7
+ def add_header(key,value)
8
+ current_headers = driver.browser.options[:headers] || {}
9
+ current_headers[key] =value
10
+ driver.browser.options[:headers]=current_headers
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'capybara/session'
16
+ Capybara::Session.send :include, RackTestCookieDisabler::Capybara
17
+
@@ -0,0 +1,14 @@
1
+ module RackTestCookieDisabler
2
+ class Middleware
3
+ def initialize(app, options = {})
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ if env['CAPYBARA_DISABLE_COOKIES']
9
+ env.delete('HTTP_COOKIE')
10
+ end
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module RackTestCookieDisabler
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'racktest_cookie_disabler/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "racktest_cookie_disabler"
8
+ gem.version = RackTestCookieDisabler::VERSION
9
+ gem.authors = ["Herman verschooten"]
10
+ gem.email = ["Herman@verschooten.net"]
11
+ gem.description = %q{Disable cookies for RackTest}
12
+ gem.summary = %q{Helper for capybara that allows you to test without cookies enabled.}
13
+ gem.homepage = "https://github.com/Hermanverschooten/racktest_cookie_disabler"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_development_dependency 'rspec', '2.12.0'
20
+ gem.add_development_dependency 'capybara', '1.1.4'
21
+ gem.add_development_dependency 'capybara-mechanize'
22
+ gem .add_development_dependency'rails', '3.2.12'
23
+
24
+ end
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ shared_examples "common scenarios" do
4
+ context "rails application" do
5
+ background { Capybara.app = TestRailsApp::Application}
6
+ scenario "with cookies enabled" do
7
+ page.visit '/'
8
+ page.should have_content "Cookie found"
9
+ end
10
+ scenario "with cookies disabled" do
11
+ page.disable_cookies(true)
12
+ page.visit '/'
13
+ page.should have_content "Cookie not found"
14
+ end
15
+ end
16
+ end
17
+
18
+ feature "manage rack session", %q(
19
+ As a developer
20
+ I want to have ability to test my app with or without cookies
21
+ ) do
22
+ context ":rack_test driver", :driver => :rack_test do
23
+ include_examples "common scenarios"
24
+ end
25
+ context ":mechanize driver", :driver => :mechanize do
26
+ include_examples "common scenarios"
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ describe RackTestCookieDisabler do
4
+ subject {described_class}
5
+
6
+ describe ".disable_cookies" do
7
+
8
+ it "has a cookies_disabled attribute" do
9
+ subject.respond_to?(:cookies_disabled).should be true
10
+ subject.respond_to?(:cookies_disabled=).should be true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require 'capybara/rspec'
2
+ require "racktest_cookie_disabler"
3
+ require "racktest_cookie_disabler/capybara"
4
+ require "capybara/mechanize"
5
+
6
+ Dir[File.expand_path('../../apps/*.rb', __FILE__)].each do |f|
7
+ require f
8
+ end
9
+
10
+ TestRailsApp::Application.configure do |app|
11
+ app.middleware.insert_after Rack::Lock, RackTestCookieDisabler::Middleware
12
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: racktest_cookie_disabler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Herman verschooten
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.12.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.12.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: capybara
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.4
46
+ - !ruby/object:Gem::Dependency
47
+ name: capybara-mechanize
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 3.2.12
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '='
76
+ - !ruby/object:Gem::Version
77
+ version: 3.2.12
78
+ description: Disable cookies for RackTest
79
+ email:
80
+ - Herman@verschooten.net
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - apps/test_rails_app.rb
91
+ - lib/racktest_cookie_disabler.rb
92
+ - lib/racktest_cookie_disabler/capybara.rb
93
+ - lib/racktest_cookie_disabler/middleware.rb
94
+ - lib/racktest_cookie_disabler/version.rb
95
+ - racktest_cookie_disabler.gemspec
96
+ - spec/middleware_spec.rb
97
+ - spec/racktest_cookie_disabler_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: https://github.com/Hermanverschooten/racktest_cookie_disabler
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
112
+ - 0
113
+ hash: -1047791174942023164
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: -1047791174942023164
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 1.8.23
126
+ signing_key:
127
+ specification_version: 3
128
+ summary: Helper for capybara that allows you to test without cookies enabled.
129
+ test_files:
130
+ - spec/middleware_spec.rb
131
+ - spec/racktest_cookie_disabler_spec.rb
132
+ - spec/spec_helper.rb