caulfield 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +0 -0
- data/LICENSE +20 -0
- data/README.md +35 -0
- data/ROADMAP.md +0 -0
- data/lib/caulfield/middleware.rb +72 -0
- data/lib/caulfield/version.rb +3 -0
- data/lib/caulfield.rb +12 -0
- metadata +72 -0
data/CHANGELOG.md
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Chris Hanks
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Caulfield
|
2
|
+
|
3
|
+
Caulfield is a small Rack middleware to be used in integration testing for Rails 3 applications. It offers a simple interface to inspect the session and cookie jar of the most recent request, and to set values and cookies for the next request. I'm using it with Capybara and RSpec, but I don't see why it wouldn't work with any other integration testing setup. It's a simple extraction from one of my projects, where I use it extensively, but it itself doesn't have tests, so beware.
|
4
|
+
|
5
|
+
Caulfield requires Rails 3.0.0.beta4 or higher.
|
6
|
+
|
7
|
+
### How to Use
|
8
|
+
|
9
|
+
- Run Caulfield.reset before each test. You can do this globally - for example, in your spec_helper file in RSpec:
|
10
|
+
|
11
|
+
Rspec.configure do |config|
|
12
|
+
config.before :each do
|
13
|
+
Caulfield.reset
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
- After you've processed a request, you can access its session and cookie jar using Caulfield.session and Caulfield.cookies, respectively.
|
18
|
+
- You can set session values and cookies for the next request using Caulfield.set_session and Caulfield.set_cookies, each of which take a hash.
|
19
|
+
- To remove a value from the session or cookie jar, set the value to nil.
|
20
|
+
|
21
|
+
You can wrap whatever DSL you like around these methods. For example:
|
22
|
+
|
23
|
+
def login_as(user)
|
24
|
+
Caulfield.set_session :user_id => user.id
|
25
|
+
end
|
26
|
+
|
27
|
+
def logged_in?
|
28
|
+
!!Caulfield.session[:user_id]
|
29
|
+
end
|
30
|
+
|
31
|
+
def logout
|
32
|
+
Caulfield.set_session :user_id => nil
|
33
|
+
end
|
34
|
+
|
35
|
+
Happy testing!
|
data/ROADMAP.md
ADDED
File without changes
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Caulfield
|
2
|
+
class Middleware
|
3
|
+
@@instance = new
|
4
|
+
attr_accessor :app, :session, :cookies
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def new(app)
|
8
|
+
@@instance.app = app
|
9
|
+
@@instance.reset
|
10
|
+
@@instance
|
11
|
+
end
|
12
|
+
|
13
|
+
def instance
|
14
|
+
@@instance
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
@request = ActionDispatch::Request.new(env)
|
20
|
+
|
21
|
+
merge_cookies unless @cookies_to_merge.empty?
|
22
|
+
merge_session unless @session_to_merge.empty?
|
23
|
+
|
24
|
+
response, headers, body = @app.call(env)
|
25
|
+
|
26
|
+
@cookies = @request.cookie_jar
|
27
|
+
@session = @request.session
|
28
|
+
|
29
|
+
[response, headers, body]
|
30
|
+
end
|
31
|
+
|
32
|
+
def reset
|
33
|
+
@cookies = nil
|
34
|
+
@session = nil
|
35
|
+
|
36
|
+
@cookies_to_merge = {}
|
37
|
+
@session_to_merge = {}
|
38
|
+
end
|
39
|
+
|
40
|
+
def set_cookies(hash)
|
41
|
+
@cookies_to_merge.merge!(hash)
|
42
|
+
end
|
43
|
+
|
44
|
+
def set_session(hash)
|
45
|
+
@session_to_merge.merge!(hash)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def merge_cookies
|
51
|
+
@cookies_to_merge.each do |key, value|
|
52
|
+
if value.nil?
|
53
|
+
@request.cookie_jar.delete key
|
54
|
+
else
|
55
|
+
@request.cookie_jar.signed[key] = value
|
56
|
+
end
|
57
|
+
end
|
58
|
+
@cookies_to_merge = {}
|
59
|
+
end
|
60
|
+
|
61
|
+
def merge_session
|
62
|
+
@session_to_merge.each do |key, value|
|
63
|
+
if value.nil?
|
64
|
+
@request.session.delete key
|
65
|
+
else
|
66
|
+
@request.session[key] = value
|
67
|
+
end
|
68
|
+
end
|
69
|
+
@session_to_merge = {}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/caulfield.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'caulfield/middleware'
|
2
|
+
|
3
|
+
module Caulfield
|
4
|
+
class << self
|
5
|
+
delegate :reset, :session, :cookies, :set_session, :set_cookies,
|
6
|
+
:to => 'Caulfield::Middleware.instance'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load(:before_initialize) do
|
11
|
+
Rails.configuration.middleware.use Caulfield::Middleware
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: caulfield
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Carl Lerche
|
13
|
+
- Yehuda Katz
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-12 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A Rack middleware to aid in integration testing for Rails 3 and up.
|
23
|
+
email:
|
24
|
+
- carlhuda@engineyard.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/caulfield.rb
|
33
|
+
- lib/caulfield/version.rb
|
34
|
+
- lib/caulfield/middleware.rb
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- ROADMAP.md
|
38
|
+
- CHANGELOG.md
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/chanks/caulfield
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 1
|
61
|
+
- 3
|
62
|
+
- 6
|
63
|
+
version: 1.3.6
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: caulfield
|
67
|
+
rubygems_version: 1.3.6
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A Rack middleware for Rails testing.
|
71
|
+
test_files: []
|
72
|
+
|