rack-stubs 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/features/stub_responses.feature +19 -20
- data/lib/rack_stubs/client.rb +5 -0
- data/lib/rack_stubs/middleware.rb +13 -13
- data/lib/rack_stubs.rb +1 -1
- data/rack-stubs.gemspec +4 -1
- data/spec/rack_stubs/client_spec.rb +5 -0
- data/spec/rack_stubs/middleware_spec.rb +14 -0
- metadata +40 -13
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
@@ -5,65 +5,64 @@ Feature: Stub Responses
|
|
5
5
|
|
6
6
|
Background:
|
7
7
|
Given GET /foo returns 404 with the body "Not Found"
|
8
|
-
And
|
9
|
-
And
|
10
|
-
And
|
8
|
+
And GET /bar returns 404 with the body "Not Found"
|
9
|
+
And POST /foo returns 404 with the body "Not Found"
|
10
|
+
And POST /bar returns 404 with the body "Not Found"
|
11
11
|
|
12
12
|
Scenario: Stub GET response
|
13
|
-
When
|
13
|
+
When I POST "application/json+rack-stub" to /foo with the body:
|
14
14
|
"""
|
15
15
|
{
|
16
16
|
"GET" : [200, { "header": "value" }, ["jazz"]]
|
17
17
|
}
|
18
18
|
"""
|
19
|
-
Then
|
20
|
-
And
|
19
|
+
Then GET /foo should return 200 with the body "jazz"
|
20
|
+
And POST /foo should return 404 with the body "Not Found"
|
21
21
|
|
22
22
|
Scenario: Stub POST response
|
23
|
-
When
|
23
|
+
When I POST "application/json+rack-stub" to /bar with the body:
|
24
24
|
"""
|
25
25
|
{
|
26
26
|
"POST" : [200, { "header": "value" }, ["funk"]]
|
27
27
|
}
|
28
28
|
"""
|
29
|
-
Then
|
30
|
-
And
|
29
|
+
Then POST /bar should return 200 with the body "funk"
|
30
|
+
And GET /foo should return 404 with the body "Not Found"
|
31
31
|
|
32
32
|
Scenario: Clear all stub responses
|
33
|
-
When
|
33
|
+
When I POST "application/json+rack-stub" to /foo with the body:
|
34
34
|
"""
|
35
35
|
{
|
36
36
|
"GET" : [200, { "header": "value" }, ["soul"]]
|
37
37
|
}
|
38
38
|
"""
|
39
|
-
And
|
39
|
+
And I POST "application/json+rack-stub" to /bar with the body:
|
40
40
|
"""
|
41
41
|
{
|
42
42
|
"POST" : [200, { "header": "value" }, ["brother"]]
|
43
43
|
}
|
44
44
|
"""
|
45
|
-
When
|
45
|
+
When I POST "application/json+rack-stub" to /rack_stubs/clear with the body:
|
46
46
|
"""
|
47
47
|
"""
|
48
|
-
Then
|
49
|
-
And
|
48
|
+
Then GET /foo should return 404 with the body "Not Found"
|
49
|
+
And POST /bar should return 404 with the body "Not Found"
|
50
50
|
|
51
51
|
Scenario: List all stub responses
|
52
|
-
Then
|
53
|
-
When
|
52
|
+
Then GET /rack_stubs/list should return 200 with the body "{}"
|
53
|
+
When I POST "application/json+rack-stub" to /foo with the body:
|
54
54
|
"""
|
55
55
|
{
|
56
56
|
"GET" : [200, { "header": "value" }, ["salsa"]]
|
57
57
|
}
|
58
58
|
"""
|
59
|
-
And
|
59
|
+
And I POST "application/json+rack-stub" to /bar with the body:
|
60
60
|
"""
|
61
61
|
{
|
62
62
|
"POST" : [200, { "header": "value" }, ["chicken"]]
|
63
63
|
}
|
64
64
|
"""
|
65
|
-
Then
|
65
|
+
Then GET /rack_stubs/list should return 200 with the body:
|
66
66
|
"""
|
67
67
|
{"/foo":{"GET":[200,{"header":"value"},["salsa"]]},"/bar":{"POST":[200,{"header":"value"},["chicken"]]}}
|
68
|
-
"""
|
69
|
-
|
68
|
+
"""
|
data/lib/rack_stubs/client.rb
CHANGED
@@ -19,6 +19,11 @@ module RackStubs
|
|
19
19
|
def post(path)
|
20
20
|
PathSpecification.new(service_url(path), 'POST', @rest_client)
|
21
21
|
end
|
22
|
+
|
23
|
+
def clear_all!
|
24
|
+
@rest_client.post(service_url("rack_stubs/clear"), "", {"Content-Type" => "application/json+rack-stub"})
|
25
|
+
end
|
26
|
+
|
22
27
|
end
|
23
28
|
|
24
29
|
class PathSpecification
|
@@ -11,12 +11,12 @@ module RackStubs
|
|
11
11
|
def call(env)
|
12
12
|
request = Rack::Request.new(env)
|
13
13
|
if request.path =~ /\/rack_stubs\/clear$/
|
14
|
-
|
14
|
+
clear_all!
|
15
15
|
ok
|
16
16
|
elsif request.path =~ /\/rack_stubs\/list$/
|
17
17
|
[200, {"Content-Type" => "text/plain"}, [@stubs.to_json]]
|
18
18
|
elsif request.content_type == "application/json+rack-stub"
|
19
|
-
|
19
|
+
@stubs[request.path] = JSON.parse(request.body.read.to_s)
|
20
20
|
ok
|
21
21
|
elsif @stubs.include?(request.path) &&
|
22
22
|
@stubs[request.path].include?(request.request_method)
|
@@ -25,30 +25,30 @@ module RackStubs
|
|
25
25
|
@app.call(env)
|
26
26
|
end
|
27
27
|
end
|
28
|
-
|
29
|
-
def setup_stub(path, http_verb_behaviours_hash)
|
30
|
-
@stubs[path] = http_verb_behaviours_hash
|
31
|
-
end
|
32
28
|
|
33
29
|
def get(path)
|
34
|
-
PathSpecification.new(path,
|
30
|
+
PathSpecification.new(@stubs, path, 'GET')
|
35
31
|
end
|
36
32
|
|
37
33
|
def post(path)
|
38
|
-
PathSpecification.new(path,
|
34
|
+
PathSpecification.new(@stubs, path, 'POST')
|
35
|
+
end
|
36
|
+
|
37
|
+
def clear_all!
|
38
|
+
@stubs = {}
|
39
39
|
end
|
40
|
-
|
41
|
-
private
|
42
40
|
|
43
41
|
class PathSpecification
|
44
|
-
def initialize(
|
45
|
-
@
|
42
|
+
def initialize(stubs, path, verb)
|
43
|
+
@stubs, @path, @verb = stubs, path, verb
|
46
44
|
end
|
47
45
|
|
48
46
|
def returns(status, headers, body)
|
49
|
-
|
47
|
+
(@stubs[@path] ||= {})[@verb] = [status, headers, body]
|
50
48
|
end
|
51
49
|
end
|
50
|
+
|
51
|
+
private
|
52
52
|
|
53
53
|
def ok
|
54
54
|
[200, {"Content-Type" => "text/plain"}, ["OK"]]
|
data/lib/rack_stubs.rb
CHANGED
data/rack-stubs.gemspec
CHANGED
@@ -15,9 +15,12 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.add_dependency 'rack', '~> 1.2.1'
|
16
16
|
s.add_dependency 'rest-client', '~> 1.4.2'
|
17
17
|
|
18
|
+
s.add_development_dependency 'rspec', '~> 2.2.0'
|
19
|
+
s.add_development_dependency 'cucumber', '~> 0.10.0'
|
20
|
+
|
18
21
|
s.rubygems_version = "1.3.7"
|
19
22
|
s.files = `git ls-files`.split("\n")
|
20
|
-
s.test_files = `git ls-files -- {features
|
23
|
+
s.test_files = `git ls-files -- {features}/*`.split("\n")
|
21
24
|
s.extra_rdoc_files = ["README.rdoc"]
|
22
25
|
s.require_path = "lib"
|
23
26
|
end
|
@@ -67,5 +67,10 @@ module RackStubs
|
|
67
67
|
Client.new("http://path/with/a/trailing/slash/", @rest_client).get("anything").returns(200, { }, "")
|
68
68
|
end
|
69
69
|
|
70
|
+
it "clears all stubs" do
|
71
|
+
@rest_client.should_receive(:post).with("http://path/to/service/rack_stubs/clear", '', {"Content-Type" => "application/json+rack-stub"})
|
72
|
+
@client.clear_all!
|
73
|
+
end
|
74
|
+
|
70
75
|
end
|
71
76
|
end
|
@@ -17,5 +17,19 @@ module RackStubs
|
|
17
17
|
middleware.post("/bar").returns(200, {}, "OK!")
|
18
18
|
middleware.call(env).should == [200, {}, "OK!"]
|
19
19
|
end
|
20
|
+
|
21
|
+
it "clears all stubs" do
|
22
|
+
app = mock("app")
|
23
|
+
middleware = Middleware.new(app)
|
24
|
+
middleware.get("/foo").returns(200, {}, "found!")
|
25
|
+
middleware.post("/bar").returns(200, {}, "OK!")
|
26
|
+
middleware.clear_all!
|
27
|
+
get_env = Rack::MockRequest.env_for("/foo")
|
28
|
+
post_env = Rack::MockRequest.env_for("/bar")
|
29
|
+
app.should_receive('call').with(get_env)
|
30
|
+
app.should_receive('call').with(post_env)
|
31
|
+
middleware.call(get_env)
|
32
|
+
middleware.call(post_env)
|
33
|
+
end
|
20
34
|
end
|
21
35
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-stubs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Chisholm
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-30 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -66,6 +66,38 @@ dependencies:
|
|
66
66
|
version: 1.4.2
|
67
67
|
type: :runtime
|
68
68
|
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 7
|
78
|
+
segments:
|
79
|
+
- 2
|
80
|
+
- 2
|
81
|
+
- 0
|
82
|
+
version: 2.2.0
|
83
|
+
type: :development
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: cucumber
|
87
|
+
prerelease: false
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 55
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
- 10
|
97
|
+
- 0
|
98
|
+
version: 0.10.0
|
99
|
+
type: :development
|
100
|
+
version_requirements: *id005
|
69
101
|
description: Makes rack apps deliver stub responses
|
70
102
|
email: joshuachisholm@gmail.com
|
71
103
|
executables: []
|
@@ -75,6 +107,7 @@ extensions: []
|
|
75
107
|
extra_rdoc_files:
|
76
108
|
- README.rdoc
|
77
109
|
files:
|
110
|
+
- .gitignore
|
78
111
|
- Gemfile
|
79
112
|
- README.rdoc
|
80
113
|
- features/step_definitions/steps.rb
|
@@ -122,12 +155,6 @@ rubyforge_project:
|
|
122
155
|
rubygems_version: 1.4.1
|
123
156
|
signing_key:
|
124
157
|
specification_version: 3
|
125
|
-
summary: rack-stubs-0.0.
|
126
|
-
test_files:
|
127
|
-
|
128
|
-
- features/stub_responses.feature
|
129
|
-
- features/support/env.rb
|
130
|
-
- features/support/web_server.rb
|
131
|
-
- spec/rack_stubs/client_spec.rb
|
132
|
-
- spec/rack_stubs/middleware_spec.rb
|
133
|
-
- spec/spec_helper.rb
|
158
|
+
summary: rack-stubs-0.0.2
|
159
|
+
test_files: []
|
160
|
+
|