rack_doubles 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/README.md +12 -4
- data/lib/rack_doubles/client.rb +54 -3
- data/lib/rack_doubles/version.rb +1 -1
- data/spec/rack_doubles/client_spec.rb +138 -15
- metadata +5 -3
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rackdoubles
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-1.9.3-p392
|
data/README.md
CHANGED
@@ -75,15 +75,23 @@ and the response body is:
|
|
75
75
|
|
76
76
|
## Rack Doubles client
|
77
77
|
|
78
|
-
To use Rack Doubles from Ruby without invoking a HTTP PUT request, Rack Doubles includes a small client library.
|
79
|
-
|
78
|
+
To use Rack Doubles from Ruby without invoking a HTTP PUT request, Rack Doubles includes a small client library.
|
79
|
+
The Client takes as it's first argument an object that responds to `#url`.
|
80
|
+
Alternatively the `#create` method will attempt to construct a Client from a range of argument types including String, Hash, and Objects that respond to `#hostname`.
|
81
|
+
To set up the a stub on a GET request to '/some-service' instantiate a client and stub the path to the resource:
|
80
82
|
|
81
|
-
client = RackDoubles::Client.new("http://127.0.0.1:9292")
|
83
|
+
client = RackDoubles::Client.new(OpenStruct.new({ url: "http://127.0.0.1:9292" }))
|
82
84
|
client.stub("/some-service").to_return(200, { "Content-Type" => "text/plain" }, ["Hello World"])
|
85
|
+
|
86
|
+
or alternatively
|
87
|
+
|
88
|
+
client = RackDoubles::Client.create("http://127.0.0.1:9292")
|
89
|
+
client.stub("/some-service").to_return(200, { "Content-Type" => "text/plain" }, ["That's all folks"])
|
90
|
+
|
83
91
|
|
84
92
|
To delete individual stubs use:
|
85
93
|
|
86
|
-
client.delete some-service
|
94
|
+
client.delete '/some-service'
|
87
95
|
|
88
96
|
and to delete all stub responses use:
|
89
97
|
|
data/lib/rack_doubles/client.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
1
|
require 'rest-client'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
module RackDoubles
|
4
5
|
|
5
6
|
class Client
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
attr_reader :server
|
9
|
+
|
10
|
+
def self.create arg, http_client
|
11
|
+
new(Server.new(extract_args(arg)), http_client)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(server, http_client = RestClient)
|
15
|
+
@server = server
|
9
16
|
@http_client = http_client
|
10
17
|
end
|
11
18
|
|
@@ -22,9 +29,52 @@ module RackDoubles
|
|
22
29
|
end
|
23
30
|
|
24
31
|
def url_for path
|
25
|
-
|
32
|
+
"#{server.url}/#{path.gsub(/^\/+/, '')}"
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def self.extract_args arg
|
38
|
+
scheme, hostname, port = case
|
39
|
+
when arg.respond_to?(:url)
|
40
|
+
extract_from_string(arg.url)
|
41
|
+
when arg.respond_to?(:hostname)
|
42
|
+
extract_from_object(arg)
|
43
|
+
when arg.class == Hash && !arg[:hostname].nil?
|
44
|
+
extract_from_hash(arg)
|
45
|
+
when arg.class == String
|
46
|
+
extract_from_string(arg)
|
47
|
+
else
|
48
|
+
raise "InvalidArgument: #{arg}"
|
49
|
+
end
|
50
|
+
scheme ||= 'http'
|
51
|
+
port = port.to_i if port
|
52
|
+
{ scheme: scheme, hostname: hostname, port: port }
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.extract_from_string arg
|
56
|
+
scheme, hostname, port = arg.scan(/(http[s]?)?[:]?[\/]*([\w.\-_]+):?(\d{2,4})*/).flatten
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.extract_from_object obj
|
60
|
+
scheme, hostname, port = obj.scheme, obj.hostname, obj.port
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.extract_from_hash hash
|
64
|
+
scheme = hash[:scheme] || hash['scheme']
|
65
|
+
hostname = hash[:hostname] || hash['hostname']
|
66
|
+
port = hash[:port] || hash['port']
|
67
|
+
[scheme, hostname, port]
|
26
68
|
end
|
27
69
|
|
70
|
+
class Server < OpenStruct
|
71
|
+
|
72
|
+
def url
|
73
|
+
"#{scheme}://#{[hostname, port].compact.join(':')}"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
28
78
|
end
|
29
79
|
|
30
80
|
class Stub
|
@@ -40,4 +90,5 @@ module RackDoubles
|
|
40
90
|
|
41
91
|
end
|
42
92
|
|
93
|
+
|
43
94
|
end
|
data/lib/rack_doubles/version.rb
CHANGED
@@ -5,30 +5,153 @@ module RackDoubles
|
|
5
5
|
|
6
6
|
describe Client do
|
7
7
|
|
8
|
+
describe "you can get a reference to the server's details" do
|
9
|
+
|
10
|
+
before do
|
11
|
+
@client = Client.create(OpenStruct.new({ url: 'http://path.to-service:9999' }), 'stub')
|
12
|
+
end
|
13
|
+
|
14
|
+
it "including scheme" do
|
15
|
+
@client.server.scheme.should == "http"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "including hostname" do
|
19
|
+
@client.server.hostname.should == "path.to-service"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "including port" do
|
23
|
+
@client.server.port.should == 9999
|
24
|
+
end
|
25
|
+
|
26
|
+
it "including the url it will generate" do
|
27
|
+
@client.server.url.should == "http://path.to-service:9999"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
8
32
|
def http_client_should_stub(url, response)
|
9
33
|
@http_client.should_receive(:put).with(url, response, {'Content-Type' => 'application/json'})
|
10
34
|
end
|
35
|
+
|
36
|
+
describe "given a object that represents a url" do
|
37
|
+
|
38
|
+
before do
|
39
|
+
@http_client = mock("http_client")
|
40
|
+
@client = Client.new(OpenStruct.new(url: 'http://path.to-service:9999'), @http_client)
|
41
|
+
end
|
11
42
|
|
12
|
-
|
13
|
-
|
14
|
-
|
43
|
+
it "stubs url end points" do
|
44
|
+
http_client_should_stub("http://path.to-service:9999/resource", [200, {}, ["This should be the response body"]].to_json)
|
45
|
+
@client.stub("/resource").to_return(200, {}, "This should be the response body")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "deletes a stub" do
|
49
|
+
@http_client.should_receive(:delete).with("http://path.to-service:9999/some-resource")
|
50
|
+
@client.delete 'some-resource'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "deletes all stubs" do
|
54
|
+
@http_client.should_receive(:delete).with("http://path.to-service:9999/")
|
55
|
+
@client.delete_all
|
56
|
+
end
|
57
|
+
|
15
58
|
end
|
59
|
+
|
60
|
+
describe "given the url of a server" do
|
61
|
+
|
62
|
+
before do
|
63
|
+
@http_client = mock("http_client")
|
64
|
+
@client = Client.create("http://path-to-service", @http_client)
|
65
|
+
end
|
16
66
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
67
|
+
it "stubs url end points" do
|
68
|
+
http_client_should_stub("http://path-to-service/resource", [200, {}, ["This should be the response body"]].to_json)
|
69
|
+
@client.stub("/resource").to_return(200, {}, "This should be the response body")
|
70
|
+
end
|
21
71
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
72
|
+
it "deletes a stub" do
|
73
|
+
@http_client.should_receive(:delete).with("http://path-to-service/some-resource")
|
74
|
+
@client.delete 'some-resource'
|
75
|
+
end
|
26
76
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
77
|
+
it "deletes all stubs" do
|
78
|
+
@http_client.should_receive(:delete).with("http://path-to-service/")
|
79
|
+
@client.delete_all
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "given a object that represents a server" do
|
85
|
+
|
86
|
+
before do
|
87
|
+
@http_client = mock("http_client")
|
88
|
+
@client = Client.create(OpenStruct.new({ scheme: 'http', hostname: 'path.to_service', port: 9999 }), @http_client)
|
89
|
+
end
|
90
|
+
|
91
|
+
it "stubs url end points" do
|
92
|
+
http_client_should_stub("http://path.to_service:9999/resource", [200, {}, ["This should be the response body"]].to_json)
|
93
|
+
@client.stub("/resource").to_return(200, {}, "This should be the response body")
|
94
|
+
end
|
95
|
+
|
96
|
+
it "deletes a stub" do
|
97
|
+
@http_client.should_receive(:delete).with("http://path.to_service:9999/some-resource")
|
98
|
+
@client.delete 'some-resource'
|
99
|
+
end
|
100
|
+
|
101
|
+
it "deletes all stubs" do
|
102
|
+
@http_client.should_receive(:delete).with("http://path.to_service:9999/")
|
103
|
+
@client.delete_all
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "given a object with just a hostname" do
|
109
|
+
|
110
|
+
before do
|
111
|
+
@http_client = mock("http_client")
|
112
|
+
@client = Client.create(OpenStruct.new({ hostname: 'path.to.service' }), @http_client)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "stubs url end points" do
|
116
|
+
http_client_should_stub("http://path.to.service/resource", [200, {}, ["This should be the response body"]].to_json)
|
117
|
+
@client.stub("/resource").to_return(200, {}, "This should be the response body")
|
118
|
+
end
|
119
|
+
|
120
|
+
it "deletes a stub" do
|
121
|
+
@http_client.should_receive(:delete).with("http://path.to.service/some-resource")
|
122
|
+
@client.delete 'some-resource'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "deletes all stubs" do
|
126
|
+
@http_client.should_receive(:delete).with("http://path.to.service/")
|
127
|
+
@client.delete_all
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "given a hash" do
|
133
|
+
|
134
|
+
before do
|
135
|
+
@http_client = mock("http_client")
|
136
|
+
@client = Client.create({ hostname: 'path.toservice', port: 45 }, @http_client)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "stubs url end points" do
|
140
|
+
http_client_should_stub("http://path.toservice:45/resource", [200, {}, ["This should be the response body"]].to_json)
|
141
|
+
@client.stub("/resource").to_return(200, {}, "This should be the response body")
|
142
|
+
end
|
143
|
+
|
144
|
+
it "deletes a stub" do
|
145
|
+
@http_client.should_receive(:delete).with("http://path.toservice:45/some-resource")
|
146
|
+
@client.delete 'some-resource'
|
147
|
+
end
|
148
|
+
|
149
|
+
it "deletes all stubs" do
|
150
|
+
@http_client.should_receive(:delete).with("http://path.toservice:45/")
|
151
|
+
@client.delete_all
|
152
|
+
end
|
31
153
|
|
154
|
+
end
|
32
155
|
|
33
156
|
end
|
34
157
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack_doubles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-07-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -131,6 +131,8 @@ extensions: []
|
|
131
131
|
extra_rdoc_files: []
|
132
132
|
files:
|
133
133
|
- .gitignore
|
134
|
+
- .ruby-gemset
|
135
|
+
- .ruby-version
|
134
136
|
- Gemfile
|
135
137
|
- README.md
|
136
138
|
- Rakefile
|
@@ -166,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
166
168
|
version: '0'
|
167
169
|
requirements: []
|
168
170
|
rubyforge_project: rack_doubles
|
169
|
-
rubygems_version: 1.8.
|
171
|
+
rubygems_version: 1.8.25
|
170
172
|
signing_key:
|
171
173
|
specification_version: 3
|
172
174
|
summary: Rack middleware for stubbing responses from HTTP web services
|