http_stub 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/http_stub.rb +7 -3
- data/lib/http_stub/configurer.rb +75 -0
- data/lib/http_stub/controllers/alias_controller.rb +29 -0
- data/lib/http_stub/controllers/stub_controller.rb +27 -0
- data/lib/http_stub/models/alias.rb +22 -0
- data/lib/http_stub/models/registry.rb +29 -0
- data/lib/http_stub/models/stub.rb +35 -0
- data/lib/http_stub/response.rb +14 -0
- data/lib/http_stub/server.rb +24 -9
- data/lib/http_stub/start_server_rake_task.rb +1 -1
- data/lib/http_stub/version.rb +1 -1
- data/spec/lib/http_stub/configurer_integration_spec.rb +119 -0
- data/spec/lib/http_stub/controllers/alias_controller_spec.rb +75 -0
- data/spec/lib/http_stub/controllers/stub_controller_spec.rb +74 -0
- data/spec/lib/http_stub/models/alias_spec.rb +58 -0
- data/spec/lib/http_stub/models/registry_spec.rb +101 -0
- data/spec/lib/http_stub/{stub_spec.rb → models/stub_spec.rb} +18 -19
- data/spec/lib/http_stub/response_spec.rb +73 -0
- data/spec/lib/http_stub/server_spec.rb +117 -44
- data/spec/lib/http_stub/start_server_rake_task_integration_spec.rb +1 -1
- metadata +23 -11
- data/lib/http_stub/client.rb +0 -61
- data/lib/http_stub/registry.rb +0 -26
- data/lib/http_stub/stub.rb +0 -35
- data/spec/lib/http_stub/client_integration_spec.rb +0 -81
- data/spec/lib/http_stub/registry_spec.rb +0 -99
@@ -4,7 +4,7 @@ describe HttpStub::StartServerRakeTask do
|
|
4
4
|
describe("when the generated task is invoked") do
|
5
5
|
|
6
6
|
it "should start a stub server that responds to stub requests" do
|
7
|
-
request = Net::HTTP::Post.new("/
|
7
|
+
request = Net::HTTP::Post.new("/stubs")
|
8
8
|
request.body = { "response" => { "status" => 302, "body" => "Some Body" } }.to_json
|
9
9
|
|
10
10
|
response = Net::HTTP.new("localhost", 8001).start { |http| http.request(request) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_stub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -147,19 +147,27 @@ executables: []
|
|
147
147
|
extensions: []
|
148
148
|
extra_rdoc_files: []
|
149
149
|
files:
|
150
|
-
- ./lib/http_stub/
|
151
|
-
- ./lib/http_stub/
|
150
|
+
- ./lib/http_stub/configurer.rb
|
151
|
+
- ./lib/http_stub/controllers/alias_controller.rb
|
152
|
+
- ./lib/http_stub/controllers/stub_controller.rb
|
153
|
+
- ./lib/http_stub/models/alias.rb
|
154
|
+
- ./lib/http_stub/models/registry.rb
|
155
|
+
- ./lib/http_stub/models/stub.rb
|
156
|
+
- ./lib/http_stub/response.rb
|
152
157
|
- ./lib/http_stub/server.rb
|
153
158
|
- ./lib/http_stub/start_server_rake_task.rb
|
154
|
-
- ./lib/http_stub/stub.rb
|
155
159
|
- ./lib/http_stub/version.rb
|
156
160
|
- ./lib/http_stub.rb
|
157
161
|
- ./spec/curl_samples.txt
|
158
|
-
- ./spec/lib/http_stub/
|
159
|
-
- ./spec/lib/http_stub/
|
162
|
+
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
163
|
+
- ./spec/lib/http_stub/controllers/alias_controller_spec.rb
|
164
|
+
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
165
|
+
- ./spec/lib/http_stub/models/alias_spec.rb
|
166
|
+
- ./spec/lib/http_stub/models/registry_spec.rb
|
167
|
+
- ./spec/lib/http_stub/models/stub_spec.rb
|
168
|
+
- ./spec/lib/http_stub/response_spec.rb
|
160
169
|
- ./spec/lib/http_stub/server_spec.rb
|
161
170
|
- ./spec/lib/http_stub/start_server_rake_task_integration_spec.rb
|
162
|
-
- ./spec/lib/http_stub/stub_spec.rb
|
163
171
|
- ./spec/spec_helper.rb
|
164
172
|
- ./spec/support/server_integration.rb
|
165
173
|
homepage: http://github.com/MYOB-Technology/http_stub
|
@@ -183,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
183
191
|
version: '0'
|
184
192
|
segments:
|
185
193
|
- 0
|
186
|
-
hash:
|
194
|
+
hash: 3377634729008532417
|
187
195
|
requirements: []
|
188
196
|
rubyforge_project: http_stub
|
189
197
|
rubygems_version: 1.8.25
|
@@ -192,10 +200,14 @@ specification_version: 3
|
|
192
200
|
summary: A HTTP Server replaying configured stub responses
|
193
201
|
test_files:
|
194
202
|
- ./spec/curl_samples.txt
|
195
|
-
- ./spec/lib/http_stub/
|
196
|
-
- ./spec/lib/http_stub/
|
203
|
+
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
204
|
+
- ./spec/lib/http_stub/controllers/alias_controller_spec.rb
|
205
|
+
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
206
|
+
- ./spec/lib/http_stub/models/alias_spec.rb
|
207
|
+
- ./spec/lib/http_stub/models/registry_spec.rb
|
208
|
+
- ./spec/lib/http_stub/models/stub_spec.rb
|
209
|
+
- ./spec/lib/http_stub/response_spec.rb
|
197
210
|
- ./spec/lib/http_stub/server_spec.rb
|
198
211
|
- ./spec/lib/http_stub/start_server_rake_task_integration_spec.rb
|
199
|
-
- ./spec/lib/http_stub/stub_spec.rb
|
200
212
|
- ./spec/spec_helper.rb
|
201
213
|
- ./spec/support/server_integration.rb
|
data/lib/http_stub/client.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
module HttpStub
|
2
|
-
|
3
|
-
module Client
|
4
|
-
|
5
|
-
def self.included(mod)
|
6
|
-
mod.extend(HttpStub::Client::ClassMethods)
|
7
|
-
mod.send(:include, HttpStub::Client::InstanceMethods)
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
|
12
|
-
attr_reader :host_value, :port_value
|
13
|
-
|
14
|
-
def host(host)
|
15
|
-
@host_value = host
|
16
|
-
end
|
17
|
-
|
18
|
-
def port(port)
|
19
|
-
@port_value = port
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
module InstanceMethods
|
25
|
-
|
26
|
-
def stub!(uri, options)
|
27
|
-
response_options = options[:response]
|
28
|
-
request = Net::HTTP::Post.new("/stub")
|
29
|
-
request.content_type = "application/json"
|
30
|
-
request.body = {
|
31
|
-
"uri" => uri,
|
32
|
-
"method" => options[:method],
|
33
|
-
"parameters" => options[:parameters] || {},
|
34
|
-
"response" => {
|
35
|
-
"status" => response_options[:status] || "200",
|
36
|
-
"body" => response_options[:body]
|
37
|
-
}
|
38
|
-
}.to_json
|
39
|
-
response = submit(request)
|
40
|
-
raise "Unable to stub request: #{response.message}" unless response.code == "200"
|
41
|
-
end
|
42
|
-
|
43
|
-
alias_method :stub_response!, :stub!
|
44
|
-
|
45
|
-
def clear!
|
46
|
-
request = Net::HTTP::Delete.new("/stubs")
|
47
|
-
response = submit(request)
|
48
|
-
raise "Unable to clear stubs: #{response.message}" unless response.code == "200"
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def submit(request)
|
54
|
-
Net::HTTP.new(self.class.host_value, self.class.port_value).start { |http| http.request(request) }
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
data/lib/http_stub/registry.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
module HttpStub
|
2
|
-
|
3
|
-
class Registry
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
@stubs = []
|
7
|
-
end
|
8
|
-
|
9
|
-
def add(stub, request)
|
10
|
-
@stubs.unshift(stub)
|
11
|
-
request.logger.info "Stub registered: #{stub}"
|
12
|
-
end
|
13
|
-
|
14
|
-
def find_for(request)
|
15
|
-
request.logger.info "Finding stub fulfilling: #{request.inspect}"
|
16
|
-
@stubs.find { |stub| stub.stubs?(request) }
|
17
|
-
end
|
18
|
-
|
19
|
-
def clear(request)
|
20
|
-
request.logger.info "Clearing stubs"
|
21
|
-
@stubs.clear
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
data/lib/http_stub/stub.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module HttpStub
|
2
|
-
|
3
|
-
class Stub
|
4
|
-
|
5
|
-
Response = ImmutableStruct.new(:status, :body)
|
6
|
-
|
7
|
-
attr_reader :response
|
8
|
-
|
9
|
-
def initialize(request)
|
10
|
-
@data = JSON.parse(request.body.read)
|
11
|
-
@response = Response.new(status: @data["response"]["status"], body: @data["response"]["body"])
|
12
|
-
end
|
13
|
-
|
14
|
-
def stubs?(request)
|
15
|
-
@data["uri"] == request.path_info &&
|
16
|
-
@data["method"].downcase == request.request_method.downcase &&
|
17
|
-
parameters_match?(request)
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_s
|
21
|
-
@data.to_s
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def parameters_match?(request)
|
27
|
-
parameters = @data["parameters"]
|
28
|
-
parameters.nil? || parameters.reduce(true) do |result, parameter|
|
29
|
-
result && (request.params[parameter[0]] == parameter[1])
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
describe HttpStub::Client do
|
2
|
-
include_context "server integration"
|
3
|
-
|
4
|
-
class TestClient
|
5
|
-
include HttpStub::Client
|
6
|
-
|
7
|
-
host "localhost"
|
8
|
-
port 8001
|
9
|
-
end
|
10
|
-
|
11
|
-
let(:client) { TestClient.new }
|
12
|
-
|
13
|
-
after(:each) { client.clear! }
|
14
|
-
|
15
|
-
describe "when a response for a request is stubbed" do
|
16
|
-
|
17
|
-
describe "that contains no parameters" do
|
18
|
-
|
19
|
-
before(:each) { client.stub_response!("/a_path", method: :get,
|
20
|
-
response: { status: 200, body: "Some body" }) }
|
21
|
-
|
22
|
-
describe "and that request is made" do
|
23
|
-
|
24
|
-
let(:response) { Net::HTTP.get_response("localhost", "/a_path", 8001) }
|
25
|
-
|
26
|
-
it "should replay the stubbed response" do
|
27
|
-
response.code.should eql("200")
|
28
|
-
response.body.should eql("Some body")
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "and the stub is cleared" do
|
34
|
-
|
35
|
-
before(:each) { client.clear! }
|
36
|
-
|
37
|
-
describe "and the original request is made" do
|
38
|
-
|
39
|
-
let(:response) { Net::HTTP.get_response("localhost", "/a_path", 8001) }
|
40
|
-
|
41
|
-
it "should respond with a 404 status code" do
|
42
|
-
response.code.should eql("404")
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
describe "that contains parameters" do
|
52
|
-
|
53
|
-
before(:each) { client.stub_response!("/a_path", method: :get, parameters: { key: "value" },
|
54
|
-
response: { status: 200, body: "Some body" }) }
|
55
|
-
|
56
|
-
describe "and that request is made" do
|
57
|
-
|
58
|
-
let(:response) { Net::HTTP.get_response("localhost", "/a_path?key=value", 8001) }
|
59
|
-
|
60
|
-
it "should replay the stubbed response" do
|
61
|
-
response.code.should eql("200")
|
62
|
-
response.body.should eql("Some body")
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
describe "and a request with different parameters is made" do
|
68
|
-
|
69
|
-
let(:response) { Net::HTTP.get_response("localhost", "/a_path?key=another_value", 8001) }
|
70
|
-
|
71
|
-
it "should respond with a 404 status code" do
|
72
|
-
response.code.should eql("404")
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
describe HttpStub::Registry do
|
2
|
-
|
3
|
-
let(:registry) { HttpStub::Registry.new }
|
4
|
-
|
5
|
-
let(:logger) { double("Logger").as_null_object }
|
6
|
-
let(:request) { double("HttpRequest", logger: logger, inspect: "Request inspect result") }
|
7
|
-
|
8
|
-
describe "#add" do
|
9
|
-
|
10
|
-
it "should log that the stub has been registered" do
|
11
|
-
stub = double(HttpStub::Stub, to_s: "Stub as String")
|
12
|
-
logger.should_receive(:info).with(/Stub as String/)
|
13
|
-
|
14
|
-
registry.add(stub, request)
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "#find_for" do
|
20
|
-
|
21
|
-
describe "when multiple stubs have been registered" do
|
22
|
-
|
23
|
-
let(:stubs) do
|
24
|
-
(1..3).map { |i| double("#{HttpStub::Stub}#{i}", :stubs? => false) }
|
25
|
-
end
|
26
|
-
|
27
|
-
before(:each) do
|
28
|
-
stubs.each { |stub| registry.add(stub, request) }
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "and one registered stub matches the request" do
|
32
|
-
|
33
|
-
before(:each) { stubs[1].stub!(:stubs?).and_return(true) }
|
34
|
-
|
35
|
-
it "should return the stub" do
|
36
|
-
registry.find_for(request).should eql(stubs[1])
|
37
|
-
end
|
38
|
-
|
39
|
-
describe "and the registry is subsequently cleared" do
|
40
|
-
|
41
|
-
before(:each) { registry.clear(request) }
|
42
|
-
|
43
|
-
it "should return nil" do
|
44
|
-
registry.find_for(request).should be_nil
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
describe "and multiple registered stubs match the request" do
|
52
|
-
|
53
|
-
before(:each) do
|
54
|
-
[0, 2].each { |i| stubs[i].stub!(:stubs?).and_return(true) }
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should support stub overrides by returning the last stub registered" do
|
58
|
-
registry.find_for(request).should eql(stubs[2])
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "and no registered stubs match the request" do
|
64
|
-
|
65
|
-
it "should return nil" do
|
66
|
-
registry.find_for(request).should be_nil
|
67
|
-
end
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
describe "when no stub has been registered" do
|
74
|
-
|
75
|
-
it "should return nil" do
|
76
|
-
registry.find_for(request).should be_nil
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
80
|
-
|
81
|
-
it "it should log stub discovery diagnostics that includes the complete details of the request" do
|
82
|
-
logger.should_receive(:info).with(/Request inspect result/)
|
83
|
-
|
84
|
-
registry.find_for(request)
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
describe "#clear" do
|
90
|
-
|
91
|
-
it "should log that the stubs are being cleared" do
|
92
|
-
logger.should_receive(:info).with(/clearing stubs/i)
|
93
|
-
|
94
|
-
registry.clear(request)
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
end
|