sham_rack 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +11 -0
- data/lib/sham_rack.rb +1 -0
- data/lib/sham_rack/registry.rb +5 -0
- data/lib/sham_rack/stub_web_service.rb +51 -0
- data/lib/sham_rack/version.rb +1 -1
- data/sham_rack.gemspec +6 -3
- data/spec/sham_rack/stub_web_service_spec.rb +114 -0
- data/spec/sham_rack_spec.rb +18 -2
- metadata +5 -2
data/README.markdown
CHANGED
@@ -57,6 +57,17 @@ Using it
|
|
57
57
|
|
58
58
|
ShamRack.mount(my_google_stub, "google.com")
|
59
59
|
|
60
|
+
### General-purpose stubbing
|
61
|
+
|
62
|
+
@stub_app = ShamRack.at("stubbed.com").stub
|
63
|
+
|
64
|
+
open("http://stubbed.com/greeting").read #=> OpenURI::HTTPError: 404
|
65
|
+
|
66
|
+
@stub_app.register_resource("/greeting", "Hello, world!", "text/plain")
|
67
|
+
|
68
|
+
open("http://stubbed.com/greeting").read #=> "Hello, world!"
|
69
|
+
@stub_app.last_request.path #=> "/greeting"
|
70
|
+
|
60
71
|
What's the catch?
|
61
72
|
-----------------
|
62
73
|
|
data/lib/sham_rack.rb
CHANGED
data/lib/sham_rack/registry.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "rack"
|
2
|
+
|
3
|
+
module ShamRack
|
4
|
+
|
5
|
+
# A simple Rack app that stubs out a web service, for testing.
|
6
|
+
class StubWebService
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@handlers = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def last_request
|
13
|
+
@request
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(env)
|
17
|
+
@request = Rack::Request.new(env)
|
18
|
+
@handlers.each do |handler|
|
19
|
+
response = handler.call(@request)
|
20
|
+
return response if response
|
21
|
+
end
|
22
|
+
return default_response
|
23
|
+
end
|
24
|
+
|
25
|
+
def handle(&block)
|
26
|
+
@handlers.unshift(block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def register_resource(path, content, content_type = "application/xml", status = 200)
|
30
|
+
handle do |request|
|
31
|
+
request_path = request.path_info
|
32
|
+
unless request.query_string.to_s.empty?
|
33
|
+
request_path += "?" + request.query_string
|
34
|
+
end
|
35
|
+
[status, {"Content-Type" => content_type}, content] if request_path == path
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def reset
|
40
|
+
@handlers.clear
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def default_response
|
46
|
+
[404, {"Content-Type" => "text/plain"}, "Not found"]
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
data/lib/sham_rack/version.rb
CHANGED
data/sham_rack.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sham_rack}
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Mike Williams"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-03-11}
|
13
13
|
s.description = %q{ShamRack plumbs Net::HTTP directly into Rack, for quick and easy HTTP testing.}
|
14
14
|
s.email = %q{mdub@dogbiscuit.org}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -26,8 +26,10 @@ Gem::Specification.new do |s|
|
|
26
26
|
"lib/sham_rack/core_ext/net/http.rb",
|
27
27
|
"lib/sham_rack/http.rb",
|
28
28
|
"lib/sham_rack/registry.rb",
|
29
|
+
"lib/sham_rack/stub_web_service.rb",
|
29
30
|
"lib/sham_rack/version.rb",
|
30
31
|
"sham_rack.gemspec",
|
32
|
+
"spec/sham_rack/stub_web_service_spec.rb",
|
31
33
|
"spec/sham_rack_spec.rb",
|
32
34
|
"spec/spec_helper.rb"
|
33
35
|
]
|
@@ -38,7 +40,8 @@ Gem::Specification.new do |s|
|
|
38
40
|
s.rubygems_version = %q{1.3.5}
|
39
41
|
s.summary = %q{Net::HTTP-to-Rack plumbing}
|
40
42
|
s.test_files = [
|
41
|
-
"spec/
|
43
|
+
"spec/sham_rack/stub_web_service_spec.rb",
|
44
|
+
"spec/sham_rack_spec.rb",
|
42
45
|
"spec/spec_helper.rb"
|
43
46
|
]
|
44
47
|
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
require "sham_rack/stub_web_service"
|
4
|
+
require "rack/test"
|
5
|
+
|
6
|
+
describe ShamRack::StubWebService do
|
7
|
+
|
8
|
+
include Rack::Test::Methods
|
9
|
+
|
10
|
+
attr_reader :app
|
11
|
+
|
12
|
+
before(:each) do
|
13
|
+
@app = ShamRack::StubWebService.new
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#last_request" do
|
17
|
+
|
18
|
+
it "returns the last request" do
|
19
|
+
get '/foo/bar'
|
20
|
+
@app.last_request.path_info.should == "/foo/bar"
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "with no handlers registered" do
|
26
|
+
|
27
|
+
describe "any request" do
|
28
|
+
|
29
|
+
before do
|
30
|
+
get "/foo/456"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns a 404" do
|
34
|
+
last_response.status.should == 404
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "with two handlers registered" do
|
42
|
+
|
43
|
+
before(:each) do
|
44
|
+
|
45
|
+
@app.handle do |request|
|
46
|
+
[200, {}, "response from first handler"] if request.get?
|
47
|
+
end
|
48
|
+
|
49
|
+
@app.handle do |request|
|
50
|
+
[200, {}, "response from second handler"] if request.path_info == "/stuff"
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "a request matching the first handler" do
|
56
|
+
|
57
|
+
before do
|
58
|
+
get "/foo/456"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "receives a response from the first handler" do
|
62
|
+
last_response.body.should == "response from first handler"
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "a request matching the second handler" do
|
68
|
+
|
69
|
+
before do
|
70
|
+
post "/stuff"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "receives a response from the second handler" do
|
74
|
+
last_response.body.should == "response from second handler"
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "a request matching both handlers" do
|
80
|
+
|
81
|
+
before do
|
82
|
+
get "/stuff"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "receives a response from the second handler" do
|
86
|
+
last_response.body.should == "response from second handler"
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
describe ".register_resource" do
|
94
|
+
|
95
|
+
before do
|
96
|
+
@app.register_resource("/stuff?foo=bar", "STUFF", "text/plain", 202)
|
97
|
+
get "/stuff?foo=bar"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "sets body" do
|
101
|
+
last_response.body.should == "STUFF"
|
102
|
+
end
|
103
|
+
|
104
|
+
it "sets content-type" do
|
105
|
+
last_response.content_type.should == "text/plain"
|
106
|
+
end
|
107
|
+
|
108
|
+
it "sets status code" do
|
109
|
+
last_response.status.should == 202
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
data/spec/sham_rack_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
require "sham_rack"
|
4
4
|
require "open-uri"
|
@@ -125,7 +125,7 @@ describe ShamRack do
|
|
125
125
|
|
126
126
|
end
|
127
127
|
|
128
|
-
describe "
|
128
|
+
describe ".at" do
|
129
129
|
|
130
130
|
describe "with a block" do
|
131
131
|
|
@@ -180,6 +180,22 @@ describe ShamRack do
|
|
180
180
|
|
181
181
|
end
|
182
182
|
|
183
|
+
describe "#stub" do
|
184
|
+
|
185
|
+
before do
|
186
|
+
@return_value = ShamRack.at("stubbed.xyz").stub
|
187
|
+
end
|
188
|
+
|
189
|
+
it "mounts a StubWebService" do
|
190
|
+
ShamRack.application_for("stubbed.xyz").should be_kind_of(ShamRack::StubWebService)
|
191
|
+
end
|
192
|
+
|
193
|
+
it "returns the StubWebService" do
|
194
|
+
@return_value.should == ShamRack.application_for("stubbed.xyz")
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
183
199
|
end
|
184
200
|
|
185
201
|
describe "Rack environment" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sham_rack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Williams
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-03-11 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -32,8 +32,10 @@ files:
|
|
32
32
|
- lib/sham_rack/core_ext/net/http.rb
|
33
33
|
- lib/sham_rack/http.rb
|
34
34
|
- lib/sham_rack/registry.rb
|
35
|
+
- lib/sham_rack/stub_web_service.rb
|
35
36
|
- lib/sham_rack/version.rb
|
36
37
|
- sham_rack.gemspec
|
38
|
+
- spec/sham_rack/stub_web_service_spec.rb
|
37
39
|
- spec/sham_rack_spec.rb
|
38
40
|
- spec/spec_helper.rb
|
39
41
|
has_rdoc: true
|
@@ -65,5 +67,6 @@ signing_key:
|
|
65
67
|
specification_version: 3
|
66
68
|
summary: Net::HTTP-to-Rack plumbing
|
67
69
|
test_files:
|
70
|
+
- spec/sham_rack/stub_web_service_spec.rb
|
68
71
|
- spec/sham_rack_spec.rb
|
69
72
|
- spec/spec_helper.rb
|