http_stub 0.13.5 → 0.14.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +13 -4
  3. data/lib/http_stub/configurer.rb +9 -63
  4. data/lib/http_stub/configurer/deprecated_dsl.rb +57 -0
  5. data/lib/http_stub/configurer/request/stub.rb +7 -13
  6. data/lib/http_stub/configurer/request/stub_activator.rb +8 -4
  7. data/lib/http_stub/configurer/request/stub_activator_payload_builder.rb +25 -0
  8. data/lib/http_stub/configurer/request/stub_payload_builder.rb +44 -0
  9. data/lib/http_stub/configurer/server/buffered_command_processor.rb +24 -0
  10. data/lib/http_stub/configurer/server/command.rb +18 -0
  11. data/lib/http_stub/configurer/server/command_processor.rb +38 -0
  12. data/lib/http_stub/configurer/server/dsl.rb +56 -0
  13. data/lib/http_stub/configurer/server/facade.rb +57 -0
  14. data/lib/http_stub/configurer/server/request_processor.rb +30 -0
  15. data/lib/http_stub/models/registry.rb +4 -0
  16. data/lib/http_stub/models/stub.rb +2 -1
  17. data/lib/http_stub/models/stub_registry.rb +29 -0
  18. data/lib/http_stub/models/stub_triggers.rb +23 -0
  19. data/lib/http_stub/rake/server_tasks.rb +2 -5
  20. data/lib/http_stub/server.rb +14 -4
  21. data/lib/http_stub/version.rb +1 -1
  22. data/lib/http_stub/views/_stub.haml +7 -0
  23. data/spec/lib/http_stub/configurer/deprecated_dsl_spec.rb +232 -0
  24. data/spec/lib/http_stub/configurer/request/controllable_value_spec.rb +8 -8
  25. data/spec/lib/http_stub/configurer/request/omittable_spec.rb +8 -8
  26. data/spec/lib/http_stub/configurer/request/regexpable_spec.rb +14 -14
  27. data/spec/lib/http_stub/configurer/request/stub_activator_payload_builder_spec.rb +74 -0
  28. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +19 -26
  29. data/spec/lib/http_stub/configurer/request/stub_payload_builder_spec.rb +203 -0
  30. data/spec/lib/http_stub/configurer/request/stub_spec.rb +18 -157
  31. data/spec/lib/http_stub/configurer/server/buffered_command_processor_spec.rb +35 -0
  32. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +62 -0
  33. data/spec/lib/http_stub/configurer/server/command_spec.rb +25 -0
  34. data/spec/lib/http_stub/configurer/server/dsl_spec.rb +241 -0
  35. data/spec/lib/http_stub/configurer/server/facade_spec.rb +253 -0
  36. data/spec/lib/http_stub/configurer/server/request_processor_spec.rb +99 -0
  37. data/spec/lib/http_stub/configurer_integration_spec.rb +172 -122
  38. data/spec/lib/http_stub/configurer_spec.rb +4 -4
  39. data/spec/lib/http_stub/controllers/stub_activator_controller_spec.rb +25 -25
  40. data/spec/lib/http_stub/controllers/stub_controller_spec.rb +22 -22
  41. data/spec/lib/http_stub/hash_extensions_spec.rb +24 -24
  42. data/spec/lib/http_stub/models/exact_value_matcher_spec.rb +4 -4
  43. data/spec/lib/http_stub/models/hash_with_string_value_matchers_spec.rb +29 -29
  44. data/spec/lib/http_stub/models/headers_spec.rb +10 -10
  45. data/spec/lib/http_stub/models/omitted_value_matcher_spec.rb +8 -8
  46. data/spec/lib/http_stub/models/regexp_value_matcher_spec.rb +6 -6
  47. data/spec/lib/http_stub/models/registry_spec.rb +71 -28
  48. data/spec/lib/http_stub/models/request_header_parser_spec.rb +4 -4
  49. data/spec/lib/http_stub/models/request_pipeline_spec.rb +2 -2
  50. data/spec/lib/http_stub/models/response_spec.rb +38 -38
  51. data/spec/lib/http_stub/models/string_value_matcher_spec.rb +25 -25
  52. data/spec/lib/http_stub/models/stub_activator_spec.rb +15 -15
  53. data/spec/lib/http_stub/models/stub_headers_spec.rb +18 -18
  54. data/spec/lib/http_stub/models/stub_parameters_spec.rb +16 -16
  55. data/spec/lib/http_stub/models/stub_registry_integration_spec.rb +62 -0
  56. data/spec/lib/http_stub/models/stub_registry_spec.rb +152 -0
  57. data/spec/lib/http_stub/models/stub_spec.rb +67 -43
  58. data/spec/lib/http_stub/models/stub_triggers_spec.rb +64 -0
  59. data/spec/lib/http_stub/models/stub_uri_spec.rb +9 -9
  60. data/spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb +7 -7
  61. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +5 -5
  62. data/spec/lib/http_stub/rake/server_tasks_spec.rb +17 -15
  63. data/spec/lib/http_stub/server_daemon_spec.rb +16 -16
  64. data/spec/lib/http_stub/server_integration_spec.rb +112 -65
  65. data/spec/lib/http_stub/server_spec.rb +134 -96
  66. data/spec/spec_helper.rb +1 -1
  67. data/spec/support/http_stub/configurer/request/stub_payload_builder_shared_context.rb +27 -0
  68. data/spec/support/server_integration.rb +2 -2
  69. metadata +69 -30
  70. data/lib/http_stub/configurer/command.rb +0 -26
  71. data/lib/http_stub/configurer/command_processor.rb +0 -36
  72. data/lib/http_stub/configurer/impatient_command_chain.rb +0 -13
  73. data/lib/http_stub/configurer/patient_command_chain.rb +0 -25
  74. data/spec/lib/http_stub/configurer/command_processor_integration_spec.rb +0 -63
  75. data/spec/lib/http_stub/configurer/command_spec.rb +0 -40
  76. data/spec/lib/http_stub/configurer/impatient_command_chain_spec.rb +0 -17
  77. data/spec/lib/http_stub/configurer/patient_command_chain_spec.rb +0 -73
@@ -0,0 +1,57 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class Facade
6
+
7
+ def initialize(configurer)
8
+ @request_processor = HttpStub::Configurer::Server::RequestProcessor.new(configurer)
9
+ end
10
+
11
+ def stub_response(request)
12
+ @request_processor.submit(request: request, description: "stubbing '#{request.stub_uri}'")
13
+ end
14
+
15
+ def stub_activator(request)
16
+ @request_processor.submit(request: request, description: "registering activator '#{request.activation_uri}'")
17
+ end
18
+
19
+ def activate(uri)
20
+ @request_processor.submit(request: Net::HTTP::Get.new(uri), description: "activating '#{uri}'")
21
+ end
22
+
23
+ def remember_stubs
24
+ request = Net::HTTP::Post.new("/stubs/memory")
25
+ request.body = ""
26
+ @request_processor.submit(request: request, description: "committing stubs to memory")
27
+ end
28
+
29
+ def recall_stubs
30
+ @request_processor.submit(
31
+ request: Net::HTTP::Get.new("/stubs/memory"), description: "recalling stubs in memory"
32
+ )
33
+ end
34
+
35
+ def clear_stubs
36
+ @request_processor.submit(request: Net::HTTP::Delete.new("/stubs"), description: "clearing stubs")
37
+ end
38
+
39
+ def clear_activators
40
+ @request_processor.submit(
41
+ request: Net::HTTP::Delete.new("/stubs/activators"), description: "clearing activators"
42
+ )
43
+ end
44
+
45
+ def server_has_started
46
+ @request_processor.disable_buffering!
47
+ end
48
+
49
+ def flush_requests
50
+ @request_processor.flush!
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,30 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class RequestProcessor
6
+
7
+ def initialize(configurer)
8
+ @command_processor = HttpStub::Configurer::Server::CommandProcessor.new(configurer)
9
+ @buffered_command_processor = @active_processor =
10
+ HttpStub::Configurer::Server::BufferedCommandProcessor.new(@command_processor)
11
+ end
12
+
13
+ def submit(args)
14
+ @active_processor.process(HttpStub::Configurer::Server::Command.new(args))
15
+ end
16
+
17
+ def flush!
18
+ @buffered_command_processor.flush
19
+ self.disable_buffering!
20
+ end
21
+
22
+ def disable_buffering!
23
+ @active_processor = @command_processor
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -22,6 +22,10 @@ module HttpStub
22
22
  Array.new(@models)
23
23
  end
24
24
 
25
+ def copy
26
+ Marshal.load(Marshal.dump(self))
27
+ end
28
+
25
29
  def clear(request)
26
30
  request.logger.info "Clearing #{@model_name} registry"
27
31
  @models.clear
@@ -3,7 +3,7 @@ module HttpStub
3
3
 
4
4
  class Stub
5
5
 
6
- attr_reader :uri, :headers, :parameters, :response
6
+ attr_reader :uri, :headers, :parameters, :response, :triggers
7
7
 
8
8
  def initialize(args)
9
9
  @args = args
@@ -11,6 +11,7 @@ module HttpStub
11
11
  @headers = HttpStub::Models::StubHeaders.new(args["headers"])
12
12
  @parameters = HttpStub::Models::StubParameters.new(args["parameters"])
13
13
  @response = HttpStub::Models::Response.new(args["response"])
14
+ @triggers = HttpStub::Models::StubTriggers.new(args["triggers"])
14
15
  end
15
16
 
16
17
  def satisfies?(request)
@@ -0,0 +1,29 @@
1
+ module HttpStub
2
+ module Models
3
+
4
+ class StubRegistry
5
+
6
+ delegate :add, :all, :clear, to: :@registry
7
+
8
+ def initialize
9
+ @registry = HttpStub::Models::Registry.new("stub")
10
+ end
11
+
12
+ def find_for(request)
13
+ stub = @registry.find_for(request)
14
+ stub.triggers.add_to(self, request) if stub
15
+ stub
16
+ end
17
+
18
+ def remember
19
+ @remembered_registry = @registry.copy
20
+ end
21
+
22
+ def recall
23
+ @registry = @remembered_registry.copy if @remembered_registry
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ module HttpStub
2
+ module Models
3
+
4
+ class StubTriggers
5
+
6
+ delegate :each, to: :@triggers
7
+
8
+ def initialize(triggers)
9
+ @triggers = (triggers || []).map { |trigger| HttpStub::Models::Stub.new(trigger) }
10
+ end
11
+
12
+ def add_to(registry, request)
13
+ @triggers.each { |trigger| registry.add(trigger, request) }
14
+ end
15
+
16
+ def to_s
17
+ @triggers.reduce("") { |result, trigger| "#{result}\n#{trigger}" }
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
@@ -26,11 +26,8 @@ module HttpStub
26
26
  end
27
27
 
28
28
  def define_initialize_task(args)
29
- desc "Configures stub #{args[:name]} in its initial state"
30
- task(:configure) do
31
- args[:configurer].initialize!
32
- args[:configurer].reset!
33
- end
29
+ desc "Configures stub #{args[:name]}"
30
+ task(:configure) { args[:configurer].initialize! }
34
31
  end
35
32
 
36
33
  end
@@ -8,7 +8,7 @@ module HttpStub
8
8
 
9
9
  def initialize
10
10
  super()
11
- @stub_registry = HttpStub::Models::Registry.new("stub")
11
+ @stub_registry = HttpStub::Models::StubRegistry.new
12
12
  @stub_activator_registry = HttpStub::Models::Registry.new("stub_activator")
13
13
  @stub_controller = HttpStub::Controllers::StubController.new(@stub_registry)
14
14
  @stub_activator_controller =
@@ -17,7 +17,7 @@ module HttpStub
17
17
 
18
18
  private
19
19
 
20
- SUPPORTED_REQUEST_TYPES = [:get, :post, :put, :delete, :patch, :options].freeze
20
+ SUPPORTED_REQUEST_TYPES = [ :get, :post, :put, :delete, :patch, :options ].freeze
21
21
 
22
22
  def self.any_request_type(path, opts={}, &block)
23
23
  SUPPORTED_REQUEST_TYPES.each { |type| self.send(type, path, opts, &block) }
@@ -53,7 +53,17 @@ module HttpStub
53
53
 
54
54
  delete "/stubs" do
55
55
  @stub_controller.clear(request)
56
- halt 200
56
+ halt 200, "OK"
57
+ end
58
+
59
+ post "/stubs/memory" do
60
+ @stub_registry.remember
61
+ halt 200, "OK"
62
+ end
63
+
64
+ get "/stubs/memory" do
65
+ @stub_registry.recall
66
+ halt 200, "OK"
57
67
  end
58
68
 
59
69
  # Sample request body:
@@ -72,7 +82,7 @@ module HttpStub
72
82
 
73
83
  delete "/stubs/activators" do
74
84
  @stub_activator_controller.clear(request)
75
- halt 200
85
+ halt 200, "OK"
76
86
  end
77
87
 
78
88
  get "/application.css" do
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.13.5"
2
+ VERSION = "0.14.0.rc1"
3
3
  end
@@ -23,3 +23,10 @@
23
23
  %tr
24
24
  %td Response Delay:
25
25
  %td=h(the_stub.response.delay_in_seconds)
26
+ %tr
27
+ %td Triggers:
28
+ %td
29
+ - the_stub.triggers.each do |trigger|
30
+ = partial :stub, locals: { the_stub: trigger }
31
+ %br
32
+
@@ -0,0 +1,232 @@
1
+ describe HttpStub::Configurer::DeprecatedDSL do
2
+
3
+ let(:stub_server) { double("StubServer") }
4
+
5
+ shared_context "a method stubbing a response" do
6
+
7
+ let(:stub_uri) { "/some/stub/uri" }
8
+ let(:method) { :put }
9
+ let(:headers) { { header_key: "header value" } }
10
+ let(:parameters) { { parameter_key: "parameter value" } }
11
+ let(:response) { { response_key: "response value" } }
12
+ let(:options) { { method: method, headers: headers, parameters: parameters, response: response } }
13
+
14
+ before(:example) do
15
+ allow(builder).to receive(:match_request)
16
+ allow(builder).to receive(:with_response)
17
+ end
18
+
19
+ it "causes the builder being added to match on the provided uri" do
20
+ expect(builder).to receive(:match_request).with(stub_uri, anything)
21
+
22
+ subject
23
+ end
24
+
25
+ context "when a method is provided" do
26
+
27
+ it "causes the builder being added to match on the provided method" do
28
+ expect(builder).to receive(:match_request).with(anything, hash_including(method: method))
29
+
30
+ subject
31
+ end
32
+
33
+ end
34
+
35
+ context "when a method is omitted" do
36
+
37
+ let(:options) { {} }
38
+
39
+ it "causes the builder being added to not match on method" do
40
+ expect(builder).to receive(:match_request).with(anything, hash_excluding(:method))
41
+
42
+ subject
43
+ end
44
+
45
+ end
46
+
47
+ context "when parameters are provided" do
48
+
49
+ it "causes the builder being added to match on the provided parameters" do
50
+ expect(builder).to receive(:match_request).with(anything, hash_including(parameters: parameters))
51
+
52
+ subject
53
+ end
54
+
55
+ end
56
+
57
+ context "when parameters are omitted" do
58
+
59
+ let(:options) { {} }
60
+
61
+ it "causes the builder being added to not match on parameters" do
62
+ expect(builder).to receive(:match_request).with(anything, hash_excluding(:parameters))
63
+
64
+ subject
65
+ end
66
+
67
+ end
68
+
69
+ context "when headers are provided" do
70
+
71
+ it "causes the builder being added to match on the provided headers" do
72
+ expect(builder).to receive(:match_request).with(anything, hash_including(headers: headers))
73
+
74
+ subject
75
+ end
76
+
77
+ end
78
+
79
+ context "when headers are omitted" do
80
+
81
+ let(:options) { {} }
82
+
83
+ it "causes the builder being added to not match on headers" do
84
+ expect(builder).to receive(:match_request).with(anything, hash_excluding(:headers))
85
+
86
+ subject
87
+ end
88
+
89
+ end
90
+
91
+ it "adds the provided response data to the builder" do
92
+ expect(builder).to receive(:with_response).with(response)
93
+
94
+ subject
95
+ end
96
+
97
+ end
98
+
99
+ shared_examples_for "a deprecated DSL object" do
100
+
101
+ [ :recall_stubs!, :clear_stubs!, :clear_activators! ].each do |stub_server_delegate_method|
102
+
103
+ describe "##{stub_server_delegate_method}" do
104
+
105
+ it "delegates to the stub server available to the dsl" do
106
+ expect(stub_server).to receive(stub_server_delegate_method)
107
+
108
+ dsl_object.send(stub_server_delegate_method)
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ describe "#server_has_started!" do
116
+
117
+ let(:activation_uri) { "http://some/activation/uri" }
118
+
119
+ it "informs the stub server that it has started" do
120
+ expect(stub_server).to receive(:has_started!)
121
+
122
+ dsl_object.server_has_started!
123
+ end
124
+
125
+ end
126
+
127
+ describe "#activate!" do
128
+
129
+ let(:activation_uri) { "http://some/activation/uri" }
130
+
131
+ it "delegates to the stub server available to the dsl" do
132
+ expect(stub_server).to receive(:activate!).with(activation_uri)
133
+
134
+ dsl_object.activate!(activation_uri)
135
+ end
136
+
137
+ end
138
+
139
+ [ :stub!, :stub_response! ].each do |method|
140
+
141
+ describe "##{method}" do
142
+
143
+ include_context "a method stubbing a response"
144
+
145
+ let(:builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) }
146
+
147
+ subject { dsl_object.send(method, stub_uri, options) }
148
+
149
+ before(:example) { allow(stub_server).to receive(:add_stub!).and_yield(builder) }
150
+
151
+ it "adds a stub to the stub server" do
152
+ expect(stub_server).to receive(:add_stub!)
153
+
154
+ subject
155
+ end
156
+
157
+ end
158
+
159
+ end
160
+
161
+ describe "#stub_activator" do
162
+
163
+ let(:activation_uri) { "http://some/activator/uri" }
164
+
165
+ include_context "a method stubbing a response"
166
+
167
+ let(:builder) { instance_double(HttpStub::Configurer::Request::StubActivatorPayloadBuilder) }
168
+
169
+ subject { dsl_object.stub_activator(activation_uri, stub_uri, options) }
170
+
171
+ before(:example) do
172
+ allow(stub_server).to receive(:add_activator!).and_yield(builder)
173
+ allow(builder).to receive(:path)
174
+ end
175
+
176
+ it "adds an activator to the stub server" do
177
+ expect(stub_server).to receive(:add_activator!)
178
+
179
+ subject
180
+ end
181
+
182
+ it "causes the builder being added to activate on the provided uri" do
183
+ expect(builder).to receive(:path).with(activation_uri)
184
+
185
+ subject
186
+ end
187
+
188
+ end
189
+
190
+ end
191
+
192
+ context "when included in a class" do
193
+
194
+ class HttpStub::Configurer::DeprecatedDSLTest
195
+ include HttpStub::Configurer::DeprecatedDSL
196
+
197
+ class << self
198
+
199
+ attr_accessor :stub_server
200
+
201
+ end
202
+
203
+ attr_accessor :stub_server
204
+
205
+ end
206
+
207
+ describe "the class in which the module was included" do
208
+
209
+ let(:dsl_object) do
210
+ HttpStub::Configurer::DeprecatedDSLTest.stub_server = stub_server
211
+ HttpStub::Configurer::DeprecatedDSLTest
212
+ end
213
+
214
+ it_behaves_like "a deprecated DSL object"
215
+
216
+ end
217
+
218
+ describe "an instance of the class in which the module was included" do
219
+
220
+ let(:dsl_object) do
221
+ dsl_object = HttpStub::Configurer::DeprecatedDSLTest.new
222
+ dsl_object.stub_server = stub_server
223
+ dsl_object
224
+ end
225
+
226
+ it_behaves_like "a deprecated DSL object"
227
+
228
+ end
229
+
230
+ end
231
+
232
+ end