http_stub 0.5.2 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -32,36 +32,9 @@ module HttpStub
32
32
  "body" => response_options[:body]
33
33
  }
34
34
  }.to_json
35
- activator_requests << request
35
+ handle(request, "registering activator '#{activation_uri}'")
36
36
  end
37
37
 
38
- def initialize!
39
- activator_requests.each do |request|
40
- response = submit(request)
41
- raise "Unable to initialize stub activator: #{response.message}" unless response.code == "200"
42
- end
43
- end
44
-
45
- def clear_activators!
46
- request = Net::HTTP::Delete.new("/stubs/activators")
47
- response = submit(request)
48
- raise "Unable to clear stub activators: #{response.message}" unless response.code == "200"
49
- end
50
-
51
- def submit(request)
52
- Net::HTTP.new(@host, @port).start { |http| http.request(request) }
53
- end
54
-
55
- private
56
-
57
- def activator_requests
58
- @activator_requests ||= []
59
- end
60
-
61
- end
62
-
63
- module InstanceMethods
64
-
65
38
  def stub!(uri, options)
66
39
  response_options = options[:response]
67
40
  request = Net::HTTP::Post.new("/stubs")
@@ -76,24 +49,61 @@ module HttpStub
76
49
  "body" => response_options[:body]
77
50
  }
78
51
  }.to_json
79
- response = self.class.submit(request)
80
- raise "Unable to establish stub: #{response.message}" unless response.code == "200"
52
+ handle(request, "stubbing '#{uri}'")
81
53
  end
82
54
 
83
55
  alias_method :stub_response!, :stub!
84
56
 
85
57
  def activate!(uri)
86
- request = Net::HTTP::Get.new(uri)
87
- response = self.class.submit(request)
88
- raise "Activator #{uri} not configured: #{response.message}" unless response.code == "200"
58
+ handle(Net::HTTP::Get.new(uri), "activating '#{uri}'")
89
59
  end
90
60
 
91
61
  alias_method :activate_stub!, :activate!
92
62
 
63
+ def initialize!
64
+ pending_requests.each { |request| request.submit() }
65
+ @initialized = true
66
+ end
67
+
68
+ def clear_activators!
69
+ handle(Net::HTTP::Delete.new("/stubs/activators"), "clearing activators")
70
+ end
71
+
93
72
  def clear!
94
- request = Net::HTTP::Delete.new("/stubs")
95
- response = self.class.submit(request)
96
- raise "Unable to clear stubs: #{response.message}" unless response.code == "200"
73
+ handle(Net::HTTP::Delete.new("/stubs"), "clearing stubs")
74
+ end
75
+
76
+ alias_method :clear_stubs!, :clear!
77
+
78
+ private
79
+
80
+ def handle(http_request, description)
81
+ request = HttpStub::ConfigurerRequest.new(@host, @port, http_request, description)
82
+ initialized? ? request.submit() : pending_requests << request
83
+ end
84
+
85
+ def pending_requests
86
+ @activator_requests ||= []
87
+ end
88
+
89
+ def initialized?
90
+ @initialized ||= false
91
+ end
92
+
93
+ end
94
+
95
+ module InstanceMethods
96
+
97
+ DELEGATE_METHODS = %w{ stub_activator stub! stub_response! activate! activate_stub! clear_activators! clear! clear_stubs! }
98
+
99
+ def self.included(mod)
100
+ DELEGATE_METHODS.each do |method_name|
101
+ mod.class_eval <<-METHOD_DEF
102
+ def #{method_name}(*args)
103
+ self.class.#{method_name}(*args)
104
+ end
105
+ METHOD_DEF
106
+ end
97
107
  end
98
108
 
99
109
  end
@@ -0,0 +1,18 @@
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,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.4"
3
3
  end
data/lib/http_stub.rb CHANGED
@@ -20,4 +20,5 @@ 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/configurer_request', __FILE__)
23
24
  require File.expand_path('../http_stub/configurer', __FILE__)
@@ -1,10 +1,10 @@
1
1
  describe HttpStub::Configurer, "when the server is running" do
2
2
  include_context "server integration"
3
3
 
4
- let(:configurer) { HttpStub::Examples::ConfigurerWithActivator.new }
4
+ let(:configurer) { HttpStub::Examples::ConfigurerWithClassActivator.new }
5
5
 
6
6
  after(:each) do
7
- configurer.clear!
7
+ configurer.clear_stubs!
8
8
  configurer.class.clear_activators!
9
9
  end
10
10
 
@@ -43,6 +43,19 @@ describe HttpStub::Configurer, "when the server is running" do
43
43
 
44
44
  end
45
45
 
46
+ describe "and an class stub is defined" do
47
+
48
+ let(:configurer) { HttpStub::Examples::ConfigurerWithClassStub.new }
49
+
50
+ it "the stub should be registered" do
51
+ response = Net::HTTP.get_response("localhost", "/a_class_stub", 8001)
52
+
53
+ response.code.should eql("201")
54
+ response.body.should eql("Class stub body")
55
+ end
56
+
57
+ end
58
+
46
59
  end
47
60
 
48
61
  describe "and the configurer is uninitialized" do
@@ -51,8 +64,10 @@ describe HttpStub::Configurer, "when the server is running" do
51
64
 
52
65
  let(:response) { Net::HTTP.get_response("localhost", "/path1", 8001) }
53
66
 
54
- it "should respond raise an exception indicating the activator is not configured" do
55
- lambda { configurer.activate!("/an_activator") }.should raise_error(/activator \/an_activator not configured/i)
67
+ it "should raise an exception indicating an error occurred during activation" do
68
+ activation_lambda = lambda { configurer.activate!("/an_activator") }
69
+
70
+ activation_lambda.should raise_error(/error occurred activating '\/an_activator'/i)
56
71
  end
57
72
 
58
73
  end
@@ -0,0 +1,30 @@
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
@@ -9,17 +9,31 @@ describe HttpStub::Models::RequestHeaderParser do
9
9
  "SERVER_NAME" => "localhost",
10
10
  }
11
11
  end
12
- let(:env) { non_http_env_elements.merge(request_headers) }
12
+ let(:env) { non_http_env_elements.merge(http_env_elements) }
13
13
  let(:request) { double("HttpRequest", env: env) }
14
14
 
15
15
  describe ".parse" do
16
+
17
+ describe "when the request contains request environment entries prefixed with 'HTTP_'" do
16
18
 
17
- let(:request_headers) { { "HTTP_KEY1" => "value1", "HTTP_KEY2" => "value2", "HTTP_KEY3" => "value3" } }
19
+ let(:http_env_elements) { { "HTTP_KEY1" => "value1", "HTTP_KEY2" => "value2", "HTTP_KEY3" => "value3" } }
18
20
 
19
- it "should return a hash containing request environment entries prefixed with HTTP_" do
20
- HttpStub::Models::RequestHeaderParser.parse(request).should eql({ "KEY1" => "value1",
21
- "KEY2" => "value2",
22
- "KEY3" => "value3" })
21
+ it "should return a hash containing only those entries with the prefix removed" do
22
+ HttpStub::Models::RequestHeaderParser.parse(request).should eql({ "KEY1" => "value1",
23
+ "KEY2" => "value2",
24
+ "KEY3" => "value3" })
25
+ end
26
+
27
+ end
28
+
29
+ describe "when the request does not contain request environment entries prefixed with 'HTTP_'" do
30
+
31
+ let(:http_env_elements) { {} }
32
+
33
+ it "should return an empty hash" do
34
+ HttpStub::Models::RequestHeaderParser.parse(request).should eql({})
35
+ end
36
+
23
37
  end
24
38
 
25
39
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'simplecov'
2
2
  SimpleCov.start do
3
3
  add_filter "/spec/"
4
- minimum_coverage 96.5
4
+ minimum_coverage 96.8
5
5
  refuse_coverage_drop
6
6
  end if ENV["coverage"]
7
7
 
@@ -11,7 +11,8 @@ require 'nokogiri'
11
11
 
12
12
  require File.expand_path('../../lib/http_stub/start_server_rake_task', __FILE__)
13
13
  require File.expand_path('../../lib/http_stub', __FILE__)
14
- require File.expand_path('../../examples/configurer_with_activator', __FILE__)
15
- require File.expand_path('../../examples/configurer_with_many_activators', __FILE__)
14
+ require File.expand_path('../../examples/configurer_with_class_activator', __FILE__)
15
+ require File.expand_path('../../examples/configurer_with_class_stub', __FILE__)
16
+ require File.expand_path('../../examples/configurer_with_many_class_activators', __FILE__)
16
17
 
17
18
  Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |file| require file }
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.2
4
+ version: 0.5.4
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-10 00:00:00.000000000 Z
13
+ date: 2013-03-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
@@ -195,6 +195,7 @@ extensions: []
195
195
  extra_rdoc_files: []
196
196
  files:
197
197
  - ./lib/http_stub/configurer.rb
198
+ - ./lib/http_stub/configurer_request.rb
198
199
  - ./lib/http_stub/controllers/stub_activator_controller.rb
199
200
  - ./lib/http_stub/controllers/stub_controller.rb
200
201
  - ./lib/http_stub/hash_extensions.rb
@@ -216,6 +217,7 @@ files:
216
217
  - ./lib/http_stub.rb
217
218
  - ./spec/curl_samples.txt
218
219
  - ./spec/lib/http_stub/configurer_integration_spec.rb
220
+ - ./spec/lib/http_stub/configurer_request_integration_spec.rb
219
221
  - ./spec/lib/http_stub/controllers/stub_activator_controller_spec.rb
220
222
  - ./spec/lib/http_stub/controllers/stub_controller_spec.rb
221
223
  - ./spec/lib/http_stub/hash_extensions_spec.rb
@@ -252,7 +254,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
254
  version: '0'
253
255
  segments:
254
256
  - 0
255
- hash: -845059187710201243
257
+ hash: -301953160599297370
256
258
  requirements: []
257
259
  rubyforge_project: http_stub
258
260
  rubygems_version: 1.8.25
@@ -262,6 +264,7 @@ summary: A HTTP Server replaying configured stub responses.
262
264
  test_files:
263
265
  - ./spec/curl_samples.txt
264
266
  - ./spec/lib/http_stub/configurer_integration_spec.rb
267
+ - ./spec/lib/http_stub/configurer_request_integration_spec.rb
265
268
  - ./spec/lib/http_stub/controllers/stub_activator_controller_spec.rb
266
269
  - ./spec/lib/http_stub/controllers/stub_controller_spec.rb
267
270
  - ./spec/lib/http_stub/hash_extensions_spec.rb