http_stub 0.2.0 → 0.2.1
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/lib/http_stub/configurer.rb +27 -1
- data/lib/http_stub/controllers/alias_controller.rb +4 -0
- data/lib/http_stub/server.rb +7 -2
- data/lib/http_stub/version.rb +1 -1
- data/spec/lib/http_stub/configurer_integration_spec.rb +33 -21
- data/spec/lib/http_stub/controllers/alias_controller_spec.rb +10 -0
- data/spec/lib/http_stub/server_spec.rb +17 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/support/server_integration.rb +2 -2
- metadata +3 -3
data/lib/http_stub/configurer.rb
CHANGED
@@ -31,14 +31,32 @@ module HttpStub
|
|
31
31
|
"body" => response_options[:body]
|
32
32
|
}
|
33
33
|
}.to_json
|
34
|
+
alias_requests << request
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize!
|
38
|
+
alias_requests.each do |request|
|
39
|
+
response = submit(request)
|
40
|
+
raise "Unable to initialize stub alias: #{response.message}" unless response.code == "200"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def clear_aliases!
|
45
|
+
request = Net::HTTP::Delete.new("/stubs/aliases")
|
34
46
|
response = submit(request)
|
35
|
-
raise "Unable to
|
47
|
+
raise "Unable to clear stub aliases: #{response.message}" unless response.code == "200"
|
36
48
|
end
|
37
49
|
|
38
50
|
def submit(request)
|
39
51
|
Net::HTTP.new(@host, @port).start { |http| http.request(request) }
|
40
52
|
end
|
41
53
|
|
54
|
+
private
|
55
|
+
|
56
|
+
def alias_requests
|
57
|
+
@alias_requests ||= []
|
58
|
+
end
|
59
|
+
|
42
60
|
end
|
43
61
|
|
44
62
|
module InstanceMethods
|
@@ -62,6 +80,14 @@ module HttpStub
|
|
62
80
|
|
63
81
|
alias_method :stub_response!, :stub!
|
64
82
|
|
83
|
+
def activate!(uri)
|
84
|
+
request = Net::HTTP::Get.new(uri)
|
85
|
+
response = self.class.submit(request)
|
86
|
+
raise "Alias #{uri} not configured: #{response.message}" unless response.code == "200"
|
87
|
+
end
|
88
|
+
|
89
|
+
alias_method :activate_stub!, :activate!
|
90
|
+
|
65
91
|
def clear!
|
66
92
|
request = Net::HTTP::Delete.new("/stubs")
|
67
93
|
response = self.class.submit(request)
|
data/lib/http_stub/server.rb
CHANGED
@@ -40,6 +40,11 @@ module HttpStub
|
|
40
40
|
halt(response.status, response.body)
|
41
41
|
end
|
42
42
|
|
43
|
+
delete "/stubs" do
|
44
|
+
@stub_controller.clear(request)
|
45
|
+
halt 200
|
46
|
+
end
|
47
|
+
|
43
48
|
# Sample request body:
|
44
49
|
# {
|
45
50
|
# "alias_uri": "/some/path",
|
@@ -50,8 +55,8 @@ module HttpStub
|
|
50
55
|
halt(response.status, response.body)
|
51
56
|
end
|
52
57
|
|
53
|
-
delete "/stubs" do
|
54
|
-
@
|
58
|
+
delete "/stubs/aliases" do
|
59
|
+
@alias_controller.clear(request)
|
55
60
|
halt 200
|
56
61
|
end
|
57
62
|
|
data/lib/http_stub/version.rb
CHANGED
@@ -1,46 +1,58 @@
|
|
1
1
|
describe HttpStub::Configurer do
|
2
2
|
include_context "server integration"
|
3
3
|
|
4
|
-
|
5
|
-
class TestConfigurer
|
6
|
-
include HttpStub::Configurer
|
4
|
+
let(:configurer) { HttpStub::Examples::ConfigurerWithAlias.new }
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
stub_alias "/an_alias", "/path1", method: :get, response: { status: 200, body: "Stub alias body" }
|
12
|
-
end
|
6
|
+
after(:each) do
|
7
|
+
configurer.clear!
|
8
|
+
configurer.class.clear_aliases!
|
13
9
|
end
|
14
10
|
|
15
|
-
|
11
|
+
describe "when initialized" do
|
16
12
|
|
17
|
-
|
13
|
+
before(:each) { configurer.class.initialize! }
|
18
14
|
|
19
|
-
|
15
|
+
describe "and a stub alias is activated" do
|
20
16
|
|
21
|
-
|
17
|
+
before(:each) { configurer.activate!("/an_alias") }
|
22
18
|
|
23
|
-
|
19
|
+
describe "and the stub request is made" do
|
24
20
|
|
25
|
-
|
21
|
+
let(:response) { Net::HTTP.get_response("localhost", "/path1", 8001) }
|
22
|
+
|
23
|
+
it "should replay the stubbed response" do
|
24
|
+
response.code.should eql("200")
|
25
|
+
response.body.should eql("Stub alias body")
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "and a stub alias is not activated" do
|
33
|
+
|
34
|
+
describe "and the stub request is made" do
|
35
|
+
|
36
|
+
let(:response) { Net::HTTP.get_response("localhost", "/path1", 8001) }
|
37
|
+
|
38
|
+
it "should respond with a 404 status code" do
|
39
|
+
response.code.should eql("404")
|
40
|
+
end
|
26
41
|
|
27
|
-
it "should replay the stubbed response" do
|
28
|
-
response.code.should eql("200")
|
29
|
-
response.body.should eql("Stub alias body")
|
30
42
|
end
|
31
43
|
|
32
44
|
end
|
33
45
|
|
34
46
|
end
|
35
47
|
|
36
|
-
describe "when
|
48
|
+
describe "when uninitialized" do
|
37
49
|
|
38
|
-
describe "and
|
50
|
+
describe "and an attempt is made to activate a stub alias" do
|
39
51
|
|
40
52
|
let(:response) { Net::HTTP.get_response("localhost", "/path1", 8001) }
|
41
53
|
|
42
|
-
it "should respond
|
43
|
-
|
54
|
+
it "should respond raise an exception indicating the alias is not configured" do
|
55
|
+
lambda { configurer.activate!("/an_alias") }.should raise_error(/alias \/an_alias not configured/i)
|
44
56
|
end
|
45
57
|
|
46
58
|
end
|
@@ -78,7 +78,7 @@ describe HttpStub::Server do
|
|
78
78
|
|
79
79
|
end
|
80
80
|
|
81
|
-
describe "when a request to clear the
|
81
|
+
describe "when a request to clear the stubs has been received" do
|
82
82
|
|
83
83
|
it "should delegate clearing to the stub controller" do
|
84
84
|
stub_controller.should_receive(:clear)
|
@@ -94,6 +94,22 @@ describe HttpStub::Server do
|
|
94
94
|
|
95
95
|
end
|
96
96
|
|
97
|
+
describe "when a request to clear the stub aliases has been received" do
|
98
|
+
|
99
|
+
it "should delegate clearing to the stub controller" do
|
100
|
+
alias_controller.should_receive(:clear)
|
101
|
+
|
102
|
+
delete "/stubs/aliases"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should respond with a 200 status code" do
|
106
|
+
delete "/stubs/aliases"
|
107
|
+
|
108
|
+
response.status.should eql(200)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
97
113
|
describe "when another type of request is received" do
|
98
114
|
|
99
115
|
describe "and the stub controller replays a response" do
|
data/spec/spec_helper.rb
CHANGED
@@ -3,5 +3,6 @@ require 'rack/test'
|
|
3
3
|
|
4
4
|
require File.expand_path('../../lib/http_stub/start_server_rake_task', __FILE__)
|
5
5
|
require File.expand_path('../../lib/http_stub', __FILE__)
|
6
|
+
require File.expand_path('../../examples/configurer_with_alias', __FILE__)
|
6
7
|
|
7
8
|
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
|
@@ -3,7 +3,7 @@ shared_context "server integration" do
|
|
3
3
|
FIVE_SECONDS = 5
|
4
4
|
|
5
5
|
before(:all) do
|
6
|
-
@pid = Process.spawn("rake
|
6
|
+
@pid = Process.spawn("rake start_example_server --trace")
|
7
7
|
wait_until("http stub server started") { Net::HTTP.get_response("localhost", "/", 8001) }
|
8
8
|
end
|
9
9
|
|
@@ -17,7 +17,7 @@ shared_context "server integration" do
|
|
17
17
|
begin
|
18
18
|
block.call
|
19
19
|
return
|
20
|
-
rescue
|
20
|
+
rescue
|
21
21
|
if Time.now - start_time > FIVE_SECONDS
|
22
22
|
raise "Timed out waiting until #{description}"
|
23
23
|
end
|
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.2.
|
4
|
+
version: 0.2.1
|
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-02-
|
13
|
+
date: 2013-02-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: sinatra
|
@@ -191,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
191
191
|
version: '0'
|
192
192
|
segments:
|
193
193
|
- 0
|
194
|
-
hash:
|
194
|
+
hash: -1031332495644956129
|
195
195
|
requirements: []
|
196
196
|
rubyforge_project: http_stub
|
197
197
|
rubygems_version: 1.8.25
|