http_stub 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/http_stub.rb CHANGED
@@ -24,6 +24,6 @@ require File.expand_path('../http_stub/server', __FILE__)
24
24
  require File.expand_path('../http_stub/configurer/stub_request', __FILE__)
25
25
  require File.expand_path('../http_stub/configurer/stub_activator_request', __FILE__)
26
26
  require File.expand_path('../http_stub/configurer/command', __FILE__)
27
- require File.expand_path('../http_stub/configurer/pre_initialize_command_processor', __FILE__)
28
- require File.expand_path('../http_stub/configurer/post_initialize_command_processor', __FILE__)
27
+ require File.expand_path('../http_stub/configurer/patient_command_chain', __FILE__)
28
+ require File.expand_path('../http_stub/configurer/impatient_command_chain', __FILE__)
29
29
  require File.expand_path('../http_stub/configurer', __FILE__)
@@ -19,52 +19,67 @@ module HttpStub
19
19
 
20
20
  def stub_activator(activation_uri, stub_uri, options)
21
21
  request = HttpStub::Configurer::StubActivatorRequest.new(activation_uri, stub_uri, options)
22
- handle(request, "registering activator '#{activation_uri}'")
22
+ handle(request: request, description: "registering activator '#{activation_uri}'")
23
23
  end
24
24
 
25
25
  def stub!(uri, options)
26
26
  request = HttpStub::Configurer::StubRequest.new(uri, options)
27
- handle(request, "stubbing '#{uri}'")
27
+ handle(request: request, description: "stubbing '#{uri}'", resetable: true)
28
28
  end
29
29
 
30
30
  alias_method :stub_response!, :stub!
31
31
 
32
32
  def activate!(uri)
33
- handle(Net::HTTP::Get.new(uri), "activating '#{uri}'")
33
+ handle(request: Net::HTTP::Get.new(uri), description: "activating '#{uri}'", resetable: true)
34
34
  end
35
35
 
36
36
  alias_method :activate_stub!, :activate!
37
37
 
38
+ def server_has_started!
39
+ @effective_command_chain = HttpStub::Configurer::ImpatientCommandChain.new()
40
+ end
41
+
38
42
  def initialize!
39
- processor.replay()
40
- @processor = HttpStub::Configurer::PostInitializeCommandProcessor.new(processor)
43
+ server_has_started!
44
+ initialize_command_chain.execute()
45
+ end
46
+
47
+ def reset!
48
+ clear!
49
+ initialize_command_chain.filter(&:resetable?).execute()
41
50
  end
42
51
 
43
52
  def clear_activators!
44
- handle(Net::HTTP::Delete.new("/stubs/activators"), "clearing activators")
53
+ handle(request: Net::HTTP::Delete.new("/stubs/activators"), description: "clearing activators")
45
54
  end
46
55
 
47
56
  def clear!
48
- handle(Net::HTTP::Delete.new("/stubs"), "clearing stubs")
57
+ handle(request: Net::HTTP::Delete.new("/stubs"), description: "clearing stubs")
49
58
  end
50
59
 
51
60
  alias_method :clear_stubs!, :clear!
52
61
 
53
62
  private
54
63
 
55
- def handle(request, description)
56
- processor.process(HttpStub::Configurer::Command.new(@host, @port, request, description))
64
+ def handle(command_options)
65
+ effective_command_chain <<
66
+ HttpStub::Configurer::Command.new({ host: @host, port: @port }.merge(command_options))
67
+ end
68
+
69
+ def effective_command_chain
70
+ @effective_command_chain ||= initialize_command_chain
57
71
  end
58
72
 
59
- def processor
60
- @processor ||= HttpStub::Configurer::PreInitializeCommandProcessor.new
73
+ def initialize_command_chain
74
+ @initialize_command_chain ||= HttpStub::Configurer::PatientCommandChain.new()
61
75
  end
62
76
 
63
77
  end
64
78
 
65
79
  module InstanceMethods
66
80
 
67
- DELEGATE_METHODS = %w{ stub_activator stub! stub_response! activate! activate_stub! clear_activators! clear! clear_stubs! }
81
+ DELEGATE_METHODS = %w{ stub_activator stub! stub_response! activate! activate_stub!
82
+ server_has_started! reset! clear_activators! clear! clear_stubs! }
68
83
 
69
84
  def self.included(mod)
70
85
  DELEGATE_METHODS.each do |method_name|
@@ -3,18 +3,23 @@ module HttpStub
3
3
 
4
4
  class Command
5
5
 
6
- def initialize(host, port, request, description)
7
- @host = host
8
- @port = port
9
- @request = request
10
- @description = description
6
+ def initialize(options)
7
+ @host = options[:host]
8
+ @port = options[:port]
9
+ @request = options[:request]
10
+ @description = options[:description]
11
+ @resetable_flag = !!options[:resetable]
11
12
  end
12
13
 
13
14
  def execute
14
- response = Net::HTTP.new(@host, @port).start { |http| http.request(@request) }
15
+ response = Net::HTTP.start(@host, @port) { |http| http.request(@request) }
15
16
  raise "Error occurred #{@description}: #{response.message}" unless response.code == "200"
16
17
  end
17
18
 
19
+ def resetable?
20
+ @resetable_flag
21
+ end
22
+
18
23
  end
19
24
 
20
25
  end
@@ -0,0 +1,13 @@
1
+ module HttpStub
2
+ module Configurer
3
+
4
+ class ImpatientCommandChain
5
+
6
+ def <<(command)
7
+ command.execute()
8
+ end
9
+
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,25 @@
1
+ module HttpStub
2
+ module Configurer
3
+
4
+ class PatientCommandChain
5
+
6
+ def initialize(commands=[])
7
+ @commands = commands
8
+ end
9
+
10
+ def <<(command)
11
+ @commands << command
12
+ end
13
+
14
+ def execute
15
+ @commands.each(&:execute)
16
+ end
17
+
18
+ def filter(&block)
19
+ HttpStub::Configurer::PatientCommandChain.new(@commands.select(&block))
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -1,7 +1,10 @@
1
1
  describe HttpStub::Configurer::Command, "when the server is running" do
2
2
  include_context "server integration"
3
3
 
4
- let(:command) { HttpStub::Configurer::Command.new("localhost", 8001, request, "performing an operation") }
4
+ let(:command) do
5
+ HttpStub::Configurer::Command.new(host: "localhost", port: 8001,
6
+ request: request, description: "performing an operation")
7
+ end
5
8
 
6
9
  describe "#execute" do
7
10
 
@@ -0,0 +1,31 @@
1
+ describe HttpStub::Configurer::Command do
2
+
3
+ let(:options) do
4
+ { host: "some_host", port: 8888, request: double("HttpRequest"), description: "Some Description" }
5
+ end
6
+
7
+ let(:command) { HttpStub::Configurer::Command.new(resetable: true) }
8
+
9
+ describe "#resetable" do
10
+
11
+ describe "when created with a resetable flag that is true" do
12
+
13
+ let(:options) { options.merge(resetable: true) }
14
+
15
+ it "should return true" do
16
+ command.should be_resetable
17
+ end
18
+
19
+ end
20
+
21
+ describe "when created without a resetable flag" do
22
+
23
+ it "should return false" do
24
+ command.should be_resetable
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,17 @@
1
+ describe HttpStub::Configurer::ImpatientCommandChain do
2
+
3
+ let(:command) { double(HttpStub::Configurer::Command) }
4
+
5
+ let(:command_chain) { HttpStub::Configurer::ImpatientCommandChain.new() }
6
+
7
+ describe "#<<" do
8
+
9
+ it "should immediately execute the provided command" do
10
+ command.should_receive(:execute)
11
+
12
+ command_chain << command
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,73 @@
1
+ describe HttpStub::Configurer::PatientCommandChain do
2
+
3
+ let(:command_chain) { HttpStub::Configurer::PatientCommandChain.new }
4
+
5
+ describe "#execute" do
6
+
7
+ describe "when a number of commands have been added" do
8
+
9
+ let(:commands) do
10
+ (1..3).map { |i| double("Command#{i}") }
11
+ end
12
+
13
+ before(:each) do
14
+ commands.each { |command| command_chain << command }
15
+ end
16
+
17
+ it "should execute each command added" do
18
+ commands.each { |command| command.should_receive(:execute) }
19
+
20
+ command_chain.execute()
21
+ end
22
+
23
+ end
24
+
25
+ describe "when no commands have been added" do
26
+
27
+ it "should execute without error" do
28
+ lambda { command_chain.execute() }.should_not raise_error
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ describe "#filter" do
36
+
37
+ describe "when a number of command have been added" do
38
+
39
+ let(:commands) do
40
+ (1..5).map { |i| double("Command#{i}", occasional_match_flag?: i % 2 == 0, no_match_flag?: false) }
41
+ end
42
+
43
+ before(:each) do
44
+ commands.each { |command| command_chain << command }
45
+ end
46
+
47
+ describe "when some commands match the filter provided" do
48
+
49
+ it "should return a chain containing the matching commands" do
50
+ [commands[1], commands[3]].each { |command| command.should_receive(:execute) }
51
+
52
+ filter_chain = command_chain.filter(&:occasional_match_flag?)
53
+
54
+ filter_chain.execute()
55
+ end
56
+
57
+ end
58
+
59
+ describe "when no commands match the filter provided" do
60
+
61
+ it "should return an empty chain" do
62
+ filter_chain = command_chain.filter(&:no_match_flag?)
63
+
64
+ filter_chain.execute()
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -47,7 +47,7 @@ describe HttpStub::Configurer, "when the server is running" do
47
47
 
48
48
  let(:configurer) { HttpStub::Examples::ConfigurerWithClassStub.new }
49
49
 
50
- it "the stub should be registered" do
50
+ it "should register the stub" do
51
51
  response = Net::HTTP.get_response("localhost", "/a_class_stub", 8001)
52
52
 
53
53
  response.code.should eql("201")
@@ -77,6 +77,60 @@ describe HttpStub::Configurer, "when the server is running" do
77
77
 
78
78
  end
79
79
 
80
+ describe "and the initializer contains stub activators that are activated and conventional stubs" do
81
+
82
+ let(:configurer) { HttpStub::Examples::ConfigurerWithComplexInitializer.new }
83
+
84
+ it "should register the activated activator" do
85
+ response = Net::HTTP.get_response("localhost", "/activated_during_initialization_stub_path", 8001)
86
+
87
+ response.code.should eql("200")
88
+ response.body.should eql("Activated during initialization body")
89
+ end
90
+
91
+ it "should register the stub" do
92
+ response = Net::HTTP.get_response("localhost", "/stubbed_during_initialization_path", 8001)
93
+
94
+ response.code.should eql("200")
95
+ response.body.should eql("Stubbed during initialization body")
96
+ end
97
+
98
+ describe "and another stub is registered" do
99
+
100
+ before(:each) do
101
+ configurer.stub_response!("/another_stub", method: :get, response: { body: "Another stub body" })
102
+ end
103
+
104
+ describe "and the configurer is reset" do
105
+
106
+ before(:each) { configurer.reset! }
107
+
108
+ it "should remove the stub registered post-initialization" do
109
+ response = Net::HTTP.get_response("localhost", "/another_stub", 8001)
110
+
111
+ response.code.should eql("404")
112
+ end
113
+
114
+ it "should retain the activated activator during initialization" do
115
+ response = Net::HTTP.get_response("localhost", "/activated_during_initialization_stub_path", 8001)
116
+
117
+ response.code.should eql("200")
118
+ response.body.should eql("Activated during initialization body")
119
+ end
120
+
121
+ it "should retain the stub registered during initialization" do
122
+ response = Net::HTTP.get_response("localhost", "/stubbed_during_initialization_path", 8001)
123
+
124
+ response.code.should eql("200")
125
+ response.body.should eql("Stubbed during initialization body")
126
+ end
127
+
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
80
134
  describe "and a response for a request is stubbed" do
81
135
 
82
136
  describe "that contains no headers or parameters" do
@@ -234,16 +288,45 @@ describe HttpStub::Configurer, "when the server is running" do
234
288
 
235
289
  describe "and the configurer is uninitialized" do
236
290
 
237
- describe "and an attempt is made to activate a stub" do
291
+ describe "and the configurer is informed that the server has started" do
238
292
 
239
- let(:response) { Net::HTTP.get_response("localhost", "/stub_path", 8001) }
293
+ before(:each) { configurer.server_has_started! }
240
294
 
241
- it "should raise an exception indicating an error occurred during activation" do
295
+ it "should not initialize the configurer" do
242
296
  activation_lambda = lambda { configurer.activate!("/an_activator") }
243
297
 
244
298
  activation_lambda.should raise_error(/error occurred activating '\/an_activator'/i)
245
299
  end
246
300
 
301
+ describe "and an attempt is made to register a stub" do
302
+
303
+ before(:each) do
304
+ configurer.stub_response!("/some_stub_path", method: :get, response: { body: "Some stub body" })
305
+ end
306
+
307
+ it "should register the stub" do
308
+ response = Net::HTTP.get_response("localhost", "/some_stub_path", 8001)
309
+
310
+ response.code.should eql("200")
311
+ response.body.should eql("Some stub body")
312
+ end
313
+
314
+ end
315
+
316
+ end
317
+
318
+ describe "and the configurer has not been informed that the server has started" do
319
+
320
+ describe "and an attempt is made to activate a stub" do
321
+
322
+ it "should raise an exception indicating an error occurred during activation" do
323
+ activation_lambda = lambda { configurer.activate!("/an_activator") }
324
+
325
+ activation_lambda.should raise_error(/error occurred activating '\/an_activator'/i)
326
+ end
327
+
328
+ end
329
+
247
330
  end
248
331
 
249
332
  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 97.2
4
+ minimum_coverage 97.3
5
5
  refuse_coverage_drop
6
6
  end if ENV["coverage"]
7
7
 
@@ -14,6 +14,7 @@ require File.expand_path('../../lib/http_stub/rake/task_generators', __FILE__)
14
14
  require File.expand_path('../../lib/http_stub', __FILE__)
15
15
  require File.expand_path('../../examples/configurer_with_class_activator', __FILE__)
16
16
  require File.expand_path('../../examples/configurer_with_class_stub', __FILE__)
17
+ require File.expand_path('../../examples/configurer_with_complex_initializer', __FILE__)
17
18
  require File.expand_path('../../examples/configurer_with_many_class_activators', __FILE__)
18
19
 
19
20
  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.6.0
4
+ version: 0.7.0
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-21 00:00:00.000000000 Z
13
+ date: 2013-04-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sinatra
@@ -211,8 +211,8 @@ extensions: []
211
211
  extra_rdoc_files: []
212
212
  files:
213
213
  - ./lib/http_stub/configurer/command.rb
214
- - ./lib/http_stub/configurer/post_initialize_command_processor.rb
215
- - ./lib/http_stub/configurer/pre_initialize_command_processor.rb
214
+ - ./lib/http_stub/configurer/impatient_command_chain.rb
215
+ - ./lib/http_stub/configurer/patient_command_chain.rb
216
216
  - ./lib/http_stub/configurer/stub_activator_request.rb
217
217
  - ./lib/http_stub/configurer/stub_request.rb
218
218
  - ./lib/http_stub/configurer.rb
@@ -239,8 +239,9 @@ files:
239
239
  - ./lib/http_stub.rb
240
240
  - ./spec/curl_samples.txt
241
241
  - ./spec/lib/http_stub/configurer/command_integration_spec.rb
242
- - ./spec/lib/http_stub/configurer/post_initialize_command_processor_spec.rb
243
- - ./spec/lib/http_stub/configurer/pre_initialize_command_processor_spec.rb
242
+ - ./spec/lib/http_stub/configurer/command_spec.rb
243
+ - ./spec/lib/http_stub/configurer/impatient_command_chain_spec.rb
244
+ - ./spec/lib/http_stub/configurer/patient_command_chain_spec.rb
244
245
  - ./spec/lib/http_stub/configurer/stub_activator_request_spec.rb
245
246
  - ./spec/lib/http_stub/configurer/stub_request_spec.rb
246
247
  - ./spec/lib/http_stub/configurer_integration_spec.rb
@@ -281,7 +282,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
281
282
  version: '0'
282
283
  segments:
283
284
  - 0
284
- hash: 3857634377426384844
285
+ hash: 1991227053042448790
285
286
  requirements: []
286
287
  rubyforge_project: http_stub
287
288
  rubygems_version: 1.8.25
@@ -291,8 +292,9 @@ summary: A HTTP Server replaying configured stub responses.
291
292
  test_files:
292
293
  - ./spec/curl_samples.txt
293
294
  - ./spec/lib/http_stub/configurer/command_integration_spec.rb
294
- - ./spec/lib/http_stub/configurer/post_initialize_command_processor_spec.rb
295
- - ./spec/lib/http_stub/configurer/pre_initialize_command_processor_spec.rb
295
+ - ./spec/lib/http_stub/configurer/command_spec.rb
296
+ - ./spec/lib/http_stub/configurer/impatient_command_chain_spec.rb
297
+ - ./spec/lib/http_stub/configurer/patient_command_chain_spec.rb
296
298
  - ./spec/lib/http_stub/configurer/stub_activator_request_spec.rb
297
299
  - ./spec/lib/http_stub/configurer/stub_request_spec.rb
298
300
  - ./spec/lib/http_stub/configurer_integration_spec.rb
@@ -1,21 +0,0 @@
1
- module HttpStub
2
- module Configurer
3
-
4
- class PostInitializeCommandProcessor
5
-
6
- def initialize(pre_initialize_processor)
7
- @pre_initialize_processor = pre_initialize_processor
8
- end
9
-
10
- def process(command)
11
- command.execute()
12
- end
13
-
14
- def replay
15
- @pre_initialize_processor.replay()
16
- end
17
-
18
- end
19
-
20
- end
21
- end
@@ -1,21 +0,0 @@
1
- module HttpStub
2
- module Configurer
3
-
4
- class PreInitializeCommandProcessor
5
-
6
- def initialize
7
- @cache = []
8
- end
9
-
10
- def process(command)
11
- @cache << command
12
- end
13
-
14
- def replay
15
- @cache.each { |command| command.execute() }
16
- end
17
-
18
- end
19
-
20
- end
21
- end
@@ -1,34 +0,0 @@
1
- describe HttpStub::Configurer::PostInitializeCommandProcessor do
2
-
3
- let(:command) { double(HttpStub::Configurer::Command).as_null_object }
4
-
5
- let(:pre_processor) { double(HttpStub::Configurer::PreInitializeCommandProcessor) }
6
- let(:post_processor) { HttpStub::Configurer::PostInitializeCommandProcessor.new(pre_processor) }
7
-
8
- describe "#process" do
9
-
10
- it "should immediately execute the provided command" do
11
- command.should_receive(:execute)
12
-
13
- post_processor.process(command)
14
- end
15
-
16
- it "should not execute the command via the pre initialize command processor" do
17
- pre_processor.should_not_receive(:execute)
18
-
19
- post_processor.process(command)
20
- end
21
-
22
- end
23
-
24
- describe "#replay" do
25
-
26
- it "should replay any commands known by the pre initialize command processor" do
27
- pre_processor.should_receive(:replay)
28
-
29
- post_processor.replay
30
- end
31
-
32
- end
33
-
34
- end
@@ -1,47 +0,0 @@
1
- describe HttpStub::Configurer::PreInitializeCommandProcessor do
2
-
3
- let(:processor) { HttpStub::Configurer::PreInitializeCommandProcessor.new }
4
-
5
- describe "#replay" 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 cached commands" do
18
- commands.each { |command| command.should_receive(:execute) }
19
-
20
- processor.replay()
21
- end
22
-
23
- describe "and those commands have already been replayed" do
24
-
25
- before(:each) { processor.replay() }
26
-
27
- it "should re-replay those commands" do
28
- commands.each { |command| command.should_receive(:execute) }
29
-
30
- processor.replay()
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.replay() }.should_not raise_error
41
- end
42
-
43
- end
44
-
45
- end
46
-
47
- end