http_stub 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 establish stub alias: #{response.message}" unless response.code == "200"
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)
@@ -23,6 +23,10 @@ module HttpStub
23
23
  end
24
24
  end
25
25
 
26
+ def clear(request)
27
+ @alias_registry.clear(request)
28
+ end
29
+
26
30
  end
27
31
 
28
32
  end
@@ -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
- @stub_controller.clear(request)
58
+ delete "/stubs/aliases" do
59
+ @alias_controller.clear(request)
55
60
  halt 200
56
61
  end
57
62
 
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -1,46 +1,58 @@
1
1
  describe HttpStub::Configurer do
2
2
  include_context "server integration"
3
3
 
4
- before(:all) do
5
- class TestConfigurer
6
- include HttpStub::Configurer
4
+ let(:configurer) { HttpStub::Examples::ConfigurerWithAlias.new }
7
5
 
8
- host "localhost"
9
- port 8001
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
- let(:configurer) { TestConfigurer.new }
11
+ describe "when initialized" do
16
12
 
17
- after(:each) { configurer.clear! }
13
+ before(:each) { configurer.class.initialize! }
18
14
 
19
- describe "when a stub alias is activated" do
15
+ describe "and a stub alias is activated" do
20
16
 
21
- before(:each) { Net::HTTP.get_response("localhost", "/an_alias", 8001) }
17
+ before(:each) { configurer.activate!("/an_alias") }
22
18
 
23
- describe "and the stub request is made" do
19
+ describe "and the stub request is made" do
24
20
 
25
- let(:response) { Net::HTTP.get_response("localhost", "/path1", 8001) }
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 a stub alias is not activated" do
48
+ describe "when uninitialized" do
37
49
 
38
- describe "and the stub request is made" do
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 with a 404 status code" do
43
- response.code.should eql("404")
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
@@ -72,4 +72,14 @@ describe HttpStub::Controllers::AliasController do
72
72
 
73
73
  end
74
74
 
75
+ describe "#clear" do
76
+
77
+ it "should clear the alias registry" do
78
+ alias_registry.should_receive(:clear).with(request)
79
+
80
+ controller.clear(request)
81
+ end
82
+
83
+ end
84
+
75
85
  end
@@ -78,7 +78,7 @@ describe HttpStub::Server do
78
78
 
79
79
  end
80
80
 
81
- describe "when a request to clear the server has been received" do
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
@@ -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 start_test_server --trace")
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 => exc
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.0
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-21 00:00:00.000000000 Z
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: 3377634729008532417
194
+ hash: -1031332495644956129
195
195
  requirements: []
196
196
  rubyforge_project: http_stub
197
197
  rubygems_version: 1.8.25