http_stub 0.5.4 → 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/http_stub/configurer/buffered_command_processor.rb +24 -0
- data/lib/http_stub/configurer/command.rb +21 -0
- data/lib/http_stub/configurer/immediate_command_processor.rb +17 -0
- data/lib/http_stub/configurer/stub_activator_request.rb +16 -0
- data/lib/http_stub/configurer/stub_request.rb +24 -0
- data/lib/http_stub/configurer.rb +8 -38
- data/lib/http_stub/version.rb +1 -1
- data/lib/http_stub.rb +5 -1
- data/spec/lib/http_stub/configurer/buffered_command_processor_spec.rb +47 -0
- data/spec/lib/http_stub/configurer/command_integration_spec.rb +30 -0
- data/spec/lib/http_stub/configurer/immediate_command_processor_spec.rb +34 -0
- data/spec/lib/http_stub/configurer/stub_activator_request_spec.rb +50 -0
- data/spec/lib/http_stub/configurer/stub_request_spec.rb +115 -0
- data/spec/lib/http_stub/configurer_integration_spec.rb +86 -61
- data/spec/spec_helper.rb +1 -1
- metadata +18 -6
- data/lib/http_stub/configurer_request.rb +0 -18
- data/spec/lib/http_stub/configurer_request_integration_spec.rb +0 -30
@@ -0,0 +1,24 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
|
4
|
+
class BufferedCommandProcessor
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@buffer = []
|
8
|
+
end
|
9
|
+
|
10
|
+
def process(command)
|
11
|
+
@buffer << command
|
12
|
+
end
|
13
|
+
|
14
|
+
def flush
|
15
|
+
until @buffer.empty?
|
16
|
+
command = @buffer.shift
|
17
|
+
command.execute()
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
|
4
|
+
class Command
|
5
|
+
|
6
|
+
def initialize(host, port, request, description)
|
7
|
+
@host = host
|
8
|
+
@port = port
|
9
|
+
@request = request
|
10
|
+
@description = description
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
response = Net::HTTP.new(@host, @port).start { |http| http.request(@request) }
|
15
|
+
raise "Error occurred #{@description}: #{response.message}" unless response.code == "200"
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
|
4
|
+
class StubActivatorRequest < Net::HTTP::Post
|
5
|
+
|
6
|
+
def initialize(activation_uri, stub_uri, options)
|
7
|
+
super("/stubs/activators")
|
8
|
+
stub_request = HttpStub::Configurer::StubRequest.new(stub_uri, options)
|
9
|
+
self.content_type = stub_request.content_type
|
10
|
+
self.body = JSON.parse(stub_request.body).merge({ "activation_uri" => activation_uri }).to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module HttpStub
|
2
|
+
module Configurer
|
3
|
+
|
4
|
+
class StubRequest < Net::HTTP::Post
|
5
|
+
|
6
|
+
def initialize(uri, options)
|
7
|
+
super("/stubs")
|
8
|
+
self.content_type = "application/json"
|
9
|
+
self.body = {
|
10
|
+
"uri" => uri,
|
11
|
+
"method" => options[:method],
|
12
|
+
"headers" => options[:headers] || {},
|
13
|
+
"parameters" => options[:parameters] || {},
|
14
|
+
"response" => {
|
15
|
+
"status" => options[:response][:status] || 200,
|
16
|
+
"body" => options[:response][:body]
|
17
|
+
}
|
18
|
+
}.to_json
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
data/lib/http_stub/configurer.rb
CHANGED
@@ -18,37 +18,12 @@ module HttpStub
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def stub_activator(activation_uri, stub_uri, options)
|
21
|
-
|
22
|
-
request = Net::HTTP::Post.new("/stubs/activators")
|
23
|
-
request.content_type = "application/json"
|
24
|
-
request.body = {
|
25
|
-
"activation_uri" => activation_uri,
|
26
|
-
"uri" => stub_uri,
|
27
|
-
"method" => options[:method],
|
28
|
-
"headers" => options[:headers] || {},
|
29
|
-
"parameters" => options[:parameters] || {},
|
30
|
-
"response" => {
|
31
|
-
"status" => response_options[:status] || "200",
|
32
|
-
"body" => response_options[:body]
|
33
|
-
}
|
34
|
-
}.to_json
|
21
|
+
request = HttpStub::Configurer::StubActivatorRequest.new(activation_uri, stub_uri, options)
|
35
22
|
handle(request, "registering activator '#{activation_uri}'")
|
36
23
|
end
|
37
24
|
|
38
25
|
def stub!(uri, options)
|
39
|
-
|
40
|
-
request = Net::HTTP::Post.new("/stubs")
|
41
|
-
request.content_type = "application/json"
|
42
|
-
request.body = {
|
43
|
-
"uri" => uri,
|
44
|
-
"method" => options[:method],
|
45
|
-
"headers" => options[:headers] || {},
|
46
|
-
"parameters" => options[:parameters] || {},
|
47
|
-
"response" => {
|
48
|
-
"status" => response_options[:status] || "200",
|
49
|
-
"body" => response_options[:body]
|
50
|
-
}
|
51
|
-
}.to_json
|
26
|
+
request = HttpStub::Configurer::StubRequest.new(uri, options)
|
52
27
|
handle(request, "stubbing '#{uri}'")
|
53
28
|
end
|
54
29
|
|
@@ -61,8 +36,8 @@ module HttpStub
|
|
61
36
|
alias_method :activate_stub!, :activate!
|
62
37
|
|
63
38
|
def initialize!
|
64
|
-
|
65
|
-
@
|
39
|
+
processor.flush
|
40
|
+
@processor = HttpStub::Configurer::ImmediateCommandProcessor.new
|
66
41
|
end
|
67
42
|
|
68
43
|
def clear_activators!
|
@@ -77,17 +52,12 @@ module HttpStub
|
|
77
52
|
|
78
53
|
private
|
79
54
|
|
80
|
-
def handle(
|
81
|
-
|
82
|
-
initialized? ? request.submit() : pending_requests << request
|
55
|
+
def handle(request, description)
|
56
|
+
processor.process(HttpStub::Configurer::Command.new(@host, @port, request, description))
|
83
57
|
end
|
84
58
|
|
85
|
-
def
|
86
|
-
@
|
87
|
-
end
|
88
|
-
|
89
|
-
def initialized?
|
90
|
-
@initialized ||= false
|
59
|
+
def processor
|
60
|
+
@processor ||= HttpStub::Configurer::BufferedCommandProcessor.new
|
91
61
|
end
|
92
62
|
|
93
63
|
end
|
data/lib/http_stub/version.rb
CHANGED
data/lib/http_stub.rb
CHANGED
@@ -20,5 +20,9 @@ require File.expand_path('../http_stub/models/registry', __FILE__)
|
|
20
20
|
require File.expand_path('../http_stub/controllers/stub_controller', __FILE__)
|
21
21
|
require File.expand_path('../http_stub/controllers/stub_activator_controller', __FILE__)
|
22
22
|
require File.expand_path('../http_stub/server', __FILE__)
|
23
|
-
require File.expand_path('../http_stub/
|
23
|
+
require File.expand_path('../http_stub/configurer/stub_request', __FILE__)
|
24
|
+
require File.expand_path('../http_stub/configurer/stub_activator_request', __FILE__)
|
25
|
+
require File.expand_path('../http_stub/configurer/command', __FILE__)
|
26
|
+
require File.expand_path('../http_stub/configurer/buffered_command_processor', __FILE__)
|
27
|
+
require File.expand_path('../http_stub/configurer/immediate_command_processor', __FILE__)
|
24
28
|
require File.expand_path('../http_stub/configurer', __FILE__)
|
@@ -0,0 +1,47 @@
|
|
1
|
+
describe HttpStub::Configurer::BufferedCommandProcessor do
|
2
|
+
|
3
|
+
let(:processor) { HttpStub::Configurer::BufferedCommandProcessor.new }
|
4
|
+
|
5
|
+
describe "#flush" do
|
6
|
+
|
7
|
+
describe "when a number of commands have been processed" do
|
8
|
+
|
9
|
+
let(:commands) do
|
10
|
+
(1..3).map { |i| double("Command#{i}").as_null_object }
|
11
|
+
end
|
12
|
+
|
13
|
+
before(:each) do
|
14
|
+
commands.each { |command| processor.process(command) }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should execute the buffered commands" do
|
18
|
+
commands.each { |command| command.should_receive(:execute) }
|
19
|
+
|
20
|
+
processor.flush()
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "and those commands have already been flushed" do
|
24
|
+
|
25
|
+
before(:each) { processor.flush() }
|
26
|
+
|
27
|
+
it "should not re-execute those commands" do
|
28
|
+
commands.each { |command| command.should_not_receive(:execute) }
|
29
|
+
|
30
|
+
processor.flush()
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "when no commands have been processed" do
|
38
|
+
|
39
|
+
it "should execute without error" do
|
40
|
+
lambda { processor.flush() }.should_not raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
describe HttpStub::Configurer::Command, "when the server is running" do
|
2
|
+
include_context "server integration"
|
3
|
+
|
4
|
+
let(:command) { HttpStub::Configurer::Command.new("localhost", 8001, request, "performing an operation") }
|
5
|
+
|
6
|
+
describe "#execute" do
|
7
|
+
|
8
|
+
describe "when the server responds with a 200 response" do
|
9
|
+
|
10
|
+
let(:request) { Net::HTTP::Get.new("/stubs") }
|
11
|
+
|
12
|
+
it "should execute without error" do
|
13
|
+
lambda { command.execute() }.should_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "when the server responds with a non-200 response" do
|
19
|
+
|
20
|
+
let(:request) { Net::HTTP::Get.new("/causes_error") }
|
21
|
+
|
22
|
+
it "should raise an exception that includes the commands description" do
|
23
|
+
lambda { command.execute() }.should raise_error(/performing an operation/)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
describe HttpStub::Configurer::ImmediateCommandProcessor do
|
2
|
+
|
3
|
+
let(:command) { double("Command").as_null_object }
|
4
|
+
|
5
|
+
let(:processor) { HttpStub::Configurer::ImmediateCommandProcessor.new }
|
6
|
+
|
7
|
+
describe "#process" do
|
8
|
+
|
9
|
+
it "should execute the provided command" do
|
10
|
+
command.should_receive(:execute)
|
11
|
+
|
12
|
+
processor.process(command)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#flush" do
|
18
|
+
|
19
|
+
describe "when a command has been processed" do
|
20
|
+
|
21
|
+
before(:each) { processor.process(command) }
|
22
|
+
|
23
|
+
it "should not re-execute the command" do
|
24
|
+
command.should_not_receive(:execute)
|
25
|
+
|
26
|
+
processor.flush
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
describe HttpStub::Configurer::StubActivatorRequest do
|
2
|
+
|
3
|
+
describe "#initialize" do
|
4
|
+
|
5
|
+
let(:stub_request_body) { { key: "value" }.to_json }
|
6
|
+
let(:stub_request_content_type) { "Some content type" }
|
7
|
+
let(:stub_request) { double("StubRequest", :content_type => stub_request_content_type, :body => stub_request_body) }
|
8
|
+
|
9
|
+
before(:each) { HttpStub::Configurer::StubRequest.stub!(:new).and_return(stub_request) }
|
10
|
+
|
11
|
+
describe "when provided an activation uri, stub uri and stub options" do
|
12
|
+
|
13
|
+
let(:activation_uri) { "Some activation URI" }
|
14
|
+
let(:stub_uri) { "Some stub URI" }
|
15
|
+
let(:stub_options) { "Some options" }
|
16
|
+
|
17
|
+
let(:request) { HttpStub::Configurer::StubActivatorRequest.new(activation_uri, stub_uri, stub_options) }
|
18
|
+
let(:request_body) { JSON.parse(request.body) }
|
19
|
+
|
20
|
+
it "should create a HTTP POST request" do
|
21
|
+
request.method.should eql("POST")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should submit the request to '/stubs/activators'" do
|
25
|
+
request.path.should eql("/stubs/activators")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should set the content type to the same as a stub request" do
|
29
|
+
request.content_type.should eql(stub_request_content_type)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "generates a JSON body which" do
|
33
|
+
|
34
|
+
it "should contain entries for a stub request" do
|
35
|
+
HttpStub::Configurer::StubRequest.should_receive(:new).with(stub_uri, stub_options).and_return(stub_request)
|
36
|
+
|
37
|
+
request_body.should include(JSON.parse(stub_request_body))
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have an entry for the provided activation URI" do
|
41
|
+
request_body.should include({ "activation_uri" => activation_uri })
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
describe HttpStub::Configurer::StubRequest do
|
2
|
+
|
3
|
+
describe "#initialize" do
|
4
|
+
|
5
|
+
describe "when provided a uri and stub options" do
|
6
|
+
|
7
|
+
let(:uri) { "Some URI" }
|
8
|
+
let(:stub_method) { "Some Method" }
|
9
|
+
let(:headers) { { "header_name" => "value" } }
|
10
|
+
let(:parameters) { { "parameter_name" => "value" } }
|
11
|
+
let(:response_status) { 500 }
|
12
|
+
let(:response_body) { "Some body" }
|
13
|
+
|
14
|
+
let(:stub_options) do
|
15
|
+
{
|
16
|
+
method: stub_method,
|
17
|
+
headers: headers,
|
18
|
+
parameters: parameters,
|
19
|
+
response: {
|
20
|
+
status: response_status,
|
21
|
+
body: response_body
|
22
|
+
}
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
let(:request) { HttpStub::Configurer::StubRequest.new(uri, stub_options) }
|
27
|
+
let(:request_body) { JSON.parse(request.body) }
|
28
|
+
|
29
|
+
it "should create a HTTP POST request" do
|
30
|
+
request.method.should eql("POST")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should submit the request to '/stubs'" do
|
34
|
+
request.path.should eql("/stubs")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should set the content type to json" do
|
38
|
+
request.content_type.should eql("application/json")
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "generates a JSON body which" do
|
42
|
+
|
43
|
+
it "should have an entry for the provided URI" do
|
44
|
+
request_body.should include({ "uri" => uri })
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should have an entry for the method option" do
|
48
|
+
request_body.should include({ "method" => stub_method })
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "when a header option is provided" do
|
52
|
+
|
53
|
+
it "should have an entry for the option" do
|
54
|
+
request_body.should include({ "headers" => headers })
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "when no header is provided" do
|
60
|
+
|
61
|
+
let(:headers) { nil }
|
62
|
+
|
63
|
+
it "should have an empty header entry" do
|
64
|
+
request_body.should include({ "headers" => {} })
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "when a parameter option is provided" do
|
70
|
+
|
71
|
+
it "should have an entry for the option" do
|
72
|
+
request_body.should include({ "parameters" => parameters })
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "when no parameter option is provided" do
|
78
|
+
|
79
|
+
let(:parameters) { nil }
|
80
|
+
|
81
|
+
it "should have an empty header entry" do
|
82
|
+
request_body.should include({ "parameters" => {} })
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "when a status response option is provided" do
|
88
|
+
|
89
|
+
it "should have a response entry for the option" do
|
90
|
+
request_body["response"].should include({ "status" => response_status })
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "when no status response option is provided" do
|
96
|
+
|
97
|
+
let(:response_status) { nil }
|
98
|
+
|
99
|
+
it "should have a response entry with status code of 200" do
|
100
|
+
request_body["response"].should include({ "status" => 200 })
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should have an entry for the response body option" do
|
106
|
+
request_body["response"].should include({ "body" => response_body })
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
@@ -18,7 +18,7 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
18
18
|
|
19
19
|
describe "and the stub request is made" do
|
20
20
|
|
21
|
-
let(:response) { Net::HTTP.get_response("localhost", "/
|
21
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_path", 8001) }
|
22
22
|
|
23
23
|
it "should replay the stubbed response" do
|
24
24
|
response.code.should eql("200")
|
@@ -33,7 +33,7 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
33
33
|
|
34
34
|
describe "and the stub request is made" do
|
35
35
|
|
36
|
-
let(:response) { Net::HTTP.get_response("localhost", "/
|
36
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_path", 8001) }
|
37
37
|
|
38
38
|
it "should respond with a 404 status code" do
|
39
39
|
response.code.should eql("404")
|
@@ -56,115 +56,124 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
56
56
|
|
57
57
|
end
|
58
58
|
|
59
|
-
|
59
|
+
describe "and a response for a request is stubbed" do
|
60
60
|
|
61
|
-
|
61
|
+
describe "that contains no headers or parameters" do
|
62
62
|
|
63
|
-
|
63
|
+
describe "and contains a response status" do
|
64
64
|
|
65
|
-
|
65
|
+
before(:each) do
|
66
|
+
configurer.stub_response!("/stub_with_status", method: :get, response: { status: 201, body: "Stub body" })
|
67
|
+
end
|
66
68
|
|
67
|
-
|
68
|
-
activation_lambda = lambda { configurer.activate!("/an_activator") }
|
69
|
+
describe "and that request is made" do
|
69
70
|
|
70
|
-
|
71
|
-
end
|
71
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_with_status", 8001) }
|
72
72
|
|
73
|
-
|
73
|
+
it "should respond with the stubbed status" do
|
74
|
+
response.code.should eql("201")
|
75
|
+
end
|
74
76
|
|
75
|
-
|
77
|
+
it "should replay the stubbed body" do
|
78
|
+
response.body.should eql("Stub body")
|
79
|
+
end
|
76
80
|
|
77
|
-
|
81
|
+
end
|
78
82
|
|
79
|
-
|
83
|
+
describe "and the stub is cleared" do
|
80
84
|
|
81
|
-
|
82
|
-
configurer.stub_response!("/path2", method: :get, response: { status: 201, body: "Stub body" })
|
83
|
-
end
|
85
|
+
before(:each) { configurer.clear! }
|
84
86
|
|
85
|
-
|
87
|
+
describe "and the original request is made" do
|
86
88
|
|
87
|
-
|
89
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_with_status", 8001) }
|
90
|
+
|
91
|
+
it "should respond with a 404 status code" do
|
92
|
+
response.code.should eql("404")
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
88
98
|
|
89
|
-
it "should replay the stubbed response" do
|
90
|
-
response.code.should eql("201")
|
91
|
-
response.body.should eql("Stub body")
|
92
99
|
end
|
93
100
|
|
94
|
-
|
101
|
+
describe "and does not contain a response status" do
|
95
102
|
|
96
|
-
|
103
|
+
before(:each) do
|
104
|
+
configurer.stub_response!("/stub_without_status", method: :get, response: { body: "Stub body" })
|
105
|
+
end
|
97
106
|
|
98
|
-
|
107
|
+
describe "and that request is made" do
|
99
108
|
|
100
|
-
|
109
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_without_status", 8001) }
|
101
110
|
|
102
|
-
|
111
|
+
it "should respond with the stubbed body" do
|
112
|
+
response.body.should eql("Stub body")
|
113
|
+
end
|
103
114
|
|
104
|
-
it "should respond with a 404 status code" do
|
105
|
-
response.code.should eql("404")
|
106
115
|
end
|
107
116
|
|
108
117
|
end
|
109
118
|
|
110
119
|
end
|
111
120
|
|
112
|
-
|
121
|
+
describe "that contains headers" do
|
113
122
|
|
114
|
-
|
123
|
+
before(:each) do
|
124
|
+
configurer.stub_response!("/stub_with_headers", method: :get, headers: { key: "value" },
|
125
|
+
response: { status: 202, body: "Another stub body" })
|
126
|
+
end
|
115
127
|
|
116
|
-
|
117
|
-
configurer.stub_response!("/path3", method: :get, headers: { key: "value" },
|
118
|
-
response: { status: 202, body: "Another stub body" })
|
119
|
-
end
|
128
|
+
describe "and that request is made" do
|
120
129
|
|
121
|
-
|
130
|
+
let(:response) { HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "value" }) }
|
122
131
|
|
123
|
-
|
132
|
+
it "should replay the stubbed response" do
|
133
|
+
response.code.should eql(202)
|
134
|
+
response.body.should eql("Another stub body")
|
135
|
+
end
|
124
136
|
|
125
|
-
it "should replay the stubbed response" do
|
126
|
-
response.code.should eql(202)
|
127
|
-
response.body.should eql("Another stub body")
|
128
137
|
end
|
129
138
|
|
130
|
-
|
139
|
+
describe "and a request with different headers is made" do
|
131
140
|
|
132
|
-
|
141
|
+
let(:response) { HTTParty.get("http://localhost:8001/stub_with_headers", headers: { "key" => "other_value" }) }
|
133
142
|
|
134
|
-
|
143
|
+
it "should respond with a 404 status code" do
|
144
|
+
response.code.should eql(404)
|
145
|
+
end
|
135
146
|
|
136
|
-
it "should respond with a 404 status code" do
|
137
|
-
response.code.should eql(404)
|
138
147
|
end
|
139
148
|
|
140
149
|
end
|
141
150
|
|
142
|
-
|
151
|
+
describe "that contains parameters" do
|
143
152
|
|
144
|
-
|
153
|
+
before(:each) do
|
154
|
+
configurer.stub_response!("/stub_with_parameters", method: :get, parameters: { key: "value" },
|
155
|
+
response: { status: 202, body: "Another stub body" })
|
156
|
+
end
|
145
157
|
|
146
|
-
|
147
|
-
configurer.stub_response!("/path4", method: :get, parameters: { key: "value" },
|
148
|
-
response: { status: 202, body: "Another stub body" })
|
149
|
-
end
|
158
|
+
describe "and that request is made" do
|
150
159
|
|
151
|
-
|
160
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=value", 8001) }
|
152
161
|
|
153
|
-
|
162
|
+
it "should replay the stubbed response" do
|
163
|
+
response.code.should eql("202")
|
164
|
+
response.body.should eql("Another stub body")
|
165
|
+
end
|
154
166
|
|
155
|
-
it "should replay the stubbed response" do
|
156
|
-
response.code.should eql("202")
|
157
|
-
response.body.should eql("Another stub body")
|
158
167
|
end
|
159
168
|
|
160
|
-
|
169
|
+
describe "and a request with different parameters is made" do
|
161
170
|
|
162
|
-
|
171
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_with_parameters?key=another_value", 8001) }
|
163
172
|
|
164
|
-
|
173
|
+
it "should respond with a 404 status code" do
|
174
|
+
response.code.should eql("404")
|
175
|
+
end
|
165
176
|
|
166
|
-
it "should respond with a 404 status code" do
|
167
|
-
response.code.should eql("404")
|
168
177
|
end
|
169
178
|
|
170
179
|
end
|
@@ -173,4 +182,20 @@ describe HttpStub::Configurer, "when the server is running" do
|
|
173
182
|
|
174
183
|
end
|
175
184
|
|
185
|
+
describe "and the configurer is uninitialized" do
|
186
|
+
|
187
|
+
describe "and an attempt is made to activate a stub" do
|
188
|
+
|
189
|
+
let(:response) { Net::HTTP.get_response("localhost", "/stub_path", 8001) }
|
190
|
+
|
191
|
+
it "should raise an exception indicating an error occurred during activation" do
|
192
|
+
activation_lambda = lambda { configurer.activate!("/an_activator") }
|
193
|
+
|
194
|
+
activation_lambda.should raise_error(/error occurred activating '\/an_activator'/i)
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
176
201
|
end
|
data/spec/spec_helper.rb
CHANGED
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.5.
|
4
|
+
version: 0.5.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sinatra
|
@@ -194,8 +194,12 @@ executables: []
|
|
194
194
|
extensions: []
|
195
195
|
extra_rdoc_files: []
|
196
196
|
files:
|
197
|
+
- ./lib/http_stub/configurer/buffered_command_processor.rb
|
198
|
+
- ./lib/http_stub/configurer/command.rb
|
199
|
+
- ./lib/http_stub/configurer/immediate_command_processor.rb
|
200
|
+
- ./lib/http_stub/configurer/stub_activator_request.rb
|
201
|
+
- ./lib/http_stub/configurer/stub_request.rb
|
197
202
|
- ./lib/http_stub/configurer.rb
|
198
|
-
- ./lib/http_stub/configurer_request.rb
|
199
203
|
- ./lib/http_stub/controllers/stub_activator_controller.rb
|
200
204
|
- ./lib/http_stub/controllers/stub_controller.rb
|
201
205
|
- ./lib/http_stub/hash_extensions.rb
|
@@ -216,8 +220,12 @@ files:
|
|
216
220
|
- ./lib/http_stub/views/stubs.haml
|
217
221
|
- ./lib/http_stub.rb
|
218
222
|
- ./spec/curl_samples.txt
|
223
|
+
- ./spec/lib/http_stub/configurer/buffered_command_processor_spec.rb
|
224
|
+
- ./spec/lib/http_stub/configurer/command_integration_spec.rb
|
225
|
+
- ./spec/lib/http_stub/configurer/immediate_command_processor_spec.rb
|
226
|
+
- ./spec/lib/http_stub/configurer/stub_activator_request_spec.rb
|
227
|
+
- ./spec/lib/http_stub/configurer/stub_request_spec.rb
|
219
228
|
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
220
|
-
- ./spec/lib/http_stub/configurer_request_integration_spec.rb
|
221
229
|
- ./spec/lib/http_stub/controllers/stub_activator_controller_spec.rb
|
222
230
|
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
223
231
|
- ./spec/lib/http_stub/hash_extensions_spec.rb
|
@@ -254,7 +262,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
254
262
|
version: '0'
|
255
263
|
segments:
|
256
264
|
- 0
|
257
|
-
hash:
|
265
|
+
hash: 914771674812276370
|
258
266
|
requirements: []
|
259
267
|
rubyforge_project: http_stub
|
260
268
|
rubygems_version: 1.8.25
|
@@ -263,8 +271,12 @@ specification_version: 3
|
|
263
271
|
summary: A HTTP Server replaying configured stub responses.
|
264
272
|
test_files:
|
265
273
|
- ./spec/curl_samples.txt
|
274
|
+
- ./spec/lib/http_stub/configurer/buffered_command_processor_spec.rb
|
275
|
+
- ./spec/lib/http_stub/configurer/command_integration_spec.rb
|
276
|
+
- ./spec/lib/http_stub/configurer/immediate_command_processor_spec.rb
|
277
|
+
- ./spec/lib/http_stub/configurer/stub_activator_request_spec.rb
|
278
|
+
- ./spec/lib/http_stub/configurer/stub_request_spec.rb
|
266
279
|
- ./spec/lib/http_stub/configurer_integration_spec.rb
|
267
|
-
- ./spec/lib/http_stub/configurer_request_integration_spec.rb
|
268
280
|
- ./spec/lib/http_stub/controllers/stub_activator_controller_spec.rb
|
269
281
|
- ./spec/lib/http_stub/controllers/stub_controller_spec.rb
|
270
282
|
- ./spec/lib/http_stub/hash_extensions_spec.rb
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module HttpStub
|
2
|
-
|
3
|
-
class ConfigurerRequest
|
4
|
-
|
5
|
-
def initialize(host, port, request, description)
|
6
|
-
@host = host
|
7
|
-
@port = port
|
8
|
-
@request = request
|
9
|
-
@description = description
|
10
|
-
end
|
11
|
-
|
12
|
-
def submit
|
13
|
-
response = Net::HTTP.new(@host, @port).start { |http| http.request(@request) }
|
14
|
-
raise "Error occurred #{@description}: #{response.message}" unless response.code == "200"
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
describe HttpStub::ConfigurerRequest, "when the server is running" do
|
2
|
-
include_context "server integration"
|
3
|
-
|
4
|
-
let(:configurer_request) { HttpStub::ConfigurerRequest.new("localhost", 8001, request, "performing some operation") }
|
5
|
-
|
6
|
-
describe "#submit" do
|
7
|
-
|
8
|
-
describe "when the server responds with a 200 response" do
|
9
|
-
|
10
|
-
let(:request) { Net::HTTP::Get.new("/stubs") }
|
11
|
-
|
12
|
-
it "should execute without error" do
|
13
|
-
lambda { configurer_request.submit() }.should_not raise_error
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
describe "when the server responds with a non-200 response" do
|
19
|
-
|
20
|
-
let(:request) { Net::HTTP::Get.new("/causes_error") }
|
21
|
-
|
22
|
-
it "should raise an exception that includes the description" do
|
23
|
-
lambda { configurer_request.submit() }.should raise_error(/performing some operation/)
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|