http_stub 0.25.1 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +32 -9
  3. data/lib/http_stub/configurer.rb +2 -10
  4. data/lib/http_stub/configurer/dsl/scenario_builder.rb +13 -11
  5. data/lib/http_stub/configurer/dsl/server.rb +32 -43
  6. data/lib/http_stub/configurer/dsl/server_endpoint_template.rb +23 -0
  7. data/lib/http_stub/configurer/dsl/session.rb +55 -0
  8. data/lib/http_stub/configurer/dsl/session_endpoint_template.rb +22 -0
  9. data/lib/http_stub/configurer/dsl/session_factory.rb +34 -0
  10. data/lib/http_stub/configurer/dsl/stub_builder.rb +18 -8
  11. data/lib/http_stub/configurer/dsl/stub_builder_template.rb +26 -0
  12. data/lib/http_stub/configurer/request/http/basic.rb +9 -3
  13. data/lib/http_stub/configurer/request/http/factory.rb +7 -16
  14. data/lib/http_stub/configurer/request/http/multipart.rb +5 -7
  15. data/lib/http_stub/configurer/request/stub.rb +3 -3
  16. data/lib/http_stub/configurer/request/triggers.rb +25 -0
  17. data/lib/http_stub/configurer/server/command_processor.rb +8 -10
  18. data/lib/http_stub/configurer/server/configuration.rb +33 -0
  19. data/lib/http_stub/configurer/server/facade.rb +21 -43
  20. data/lib/http_stub/configurer/server/request_processor.rb +2 -2
  21. data/lib/http_stub/configurer/server/session_facade.rb +64 -0
  22. data/lib/http_stub/extensions/core/uri.rb +28 -0
  23. data/lib/http_stub/extensions/rack/handler.rb +1 -0
  24. data/lib/http_stub/extensions/sinatra/namespace.rb +9 -0
  25. data/lib/http_stub/rake/server_tasks.rb +1 -4
  26. data/lib/http_stub/server/application/application.rb +17 -98
  27. data/lib/http_stub/server/application/configuration.rb +38 -0
  28. data/lib/http_stub/server/application/cross_origin_support.rb +7 -5
  29. data/lib/http_stub/server/application/request_support.rb +38 -0
  30. data/lib/http_stub/server/application/response_support.rb +19 -0
  31. data/lib/http_stub/server/application/routes/memory.rb +37 -0
  32. data/lib/http_stub/server/application/routes/resource.rb +23 -0
  33. data/lib/http_stub/server/application/routes/scenario.rb +52 -0
  34. data/lib/http_stub/server/application/routes/session.rb +65 -0
  35. data/lib/http_stub/server/application/routes/stub.rb +75 -0
  36. data/lib/http_stub/server/application/session_uri_support.rb +15 -0
  37. data/lib/http_stub/server/memory/controller.rb +25 -0
  38. data/lib/http_stub/server/memory/memory.rb +32 -0
  39. data/lib/http_stub/server/registry.rb +25 -8
  40. data/lib/http_stub/server/request/factory.rb +23 -0
  41. data/lib/http_stub/server/request/parameters.rb +2 -2
  42. data/lib/http_stub/server/request/request.rb +6 -11
  43. data/lib/http_stub/server/request/sinatra_request.rb +26 -0
  44. data/lib/http_stub/server/scenario/controller.rb +16 -11
  45. data/lib/http_stub/server/scenario/not_found_error.rb +15 -0
  46. data/lib/http_stub/server/session.rb +14 -0
  47. data/lib/http_stub/server/session/configuration.rb +32 -0
  48. data/lib/http_stub/server/session/controller.rb +40 -0
  49. data/lib/http_stub/server/session/empty.rb +15 -0
  50. data/lib/http_stub/server/session/identifier_strategy.rb +24 -0
  51. data/lib/http_stub/server/session/registry.rb +38 -0
  52. data/lib/http_stub/server/session/session.rb +80 -0
  53. data/lib/http_stub/server/stub/controller.rb +18 -12
  54. data/lib/http_stub/server/stub/empty.rb +1 -1
  55. data/lib/http_stub/server/stub/match/controller.rb +9 -5
  56. data/lib/http_stub/server/stub/payload/response_body_modifier.rb +3 -1
  57. data/lib/http_stub/server/stub/registry.rb +6 -13
  58. data/lib/http_stub/server/stub/triggers.rb +11 -9
  59. data/lib/http_stub/server/views/_home.haml +2 -0
  60. data/lib/http_stub/server/views/_post_link_function.haml +26 -0
  61. data/lib/http_stub/server/views/_session.haml +16 -0
  62. data/lib/http_stub/server/views/_stub.haml +8 -2
  63. data/lib/http_stub/server/views/_stubs.haml +4 -0
  64. data/lib/http_stub/server/views/application.sass +4 -1
  65. data/lib/http_stub/server/views/layout.haml +1 -0
  66. data/lib/http_stub/server/views/scenario.haml +4 -3
  67. data/lib/http_stub/server/views/scenarios.haml +7 -6
  68. data/lib/http_stub/server/views/session.haml +3 -0
  69. data/lib/http_stub/server/views/sessions.haml +6 -0
  70. data/lib/http_stub/server/views/stubs.haml +1 -4
  71. data/lib/http_stub/server/views/transactional_session.haml +2 -0
  72. data/lib/http_stub/version.rb +1 -1
  73. data/spec/acceptance/configurer_initialization_spec.rb +40 -51
  74. data/spec/acceptance/configurer_part_spec.rb +5 -7
  75. data/spec/acceptance/cross_origin_support_spec.rb +7 -13
  76. data/spec/acceptance/endpoint_template_spec.rb +2 -4
  77. data/spec/acceptance/request_reference_spec.rb +1 -3
  78. data/spec/acceptance/scenario_spec.rb +7 -9
  79. data/spec/acceptance/server_defaults_spec.rb +2 -4
  80. data/spec/acceptance/server_memory_spec.rb +20 -0
  81. data/spec/acceptance/session_spec.rb +125 -0
  82. data/spec/acceptance/stub_body_request_matching_spec.rb +2 -4
  83. data/spec/acceptance/stub_control_values_spec.rb +19 -18
  84. data/spec/acceptance/stub_match_last_spec.rb +0 -4
  85. data/spec/acceptance/stub_match_list_spec.rb +0 -4
  86. data/spec/acceptance/stub_miss_list_spec.rb +0 -4
  87. data/spec/acceptance/stub_spec.rb +11 -11
  88. data/spec/acceptance/stub_trigger_spec.rb +60 -13
  89. data/spec/lib/http_stub/configurer/dsl/scenario_builder_spec.rb +83 -30
  90. data/spec/lib/http_stub/configurer/dsl/server_endpoint_template_spec.rb +167 -0
  91. data/spec/lib/http_stub/configurer/dsl/server_spec.rb +306 -176
  92. data/spec/lib/http_stub/configurer/dsl/session_endpoint_template_spec.rb +150 -0
  93. data/spec/lib/http_stub/configurer/dsl/session_factory_spec.rb +97 -0
  94. data/spec/lib/http_stub/configurer/dsl/session_spec.rb +222 -0
  95. data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +209 -48
  96. data/spec/lib/http_stub/configurer/dsl/stub_builder_template_spec.rb +203 -0
  97. data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +80 -4
  98. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +115 -56
  99. data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +39 -6
  100. data/spec/lib/http_stub/configurer/request/stub_spec.rb +50 -108
  101. data/spec/lib/http_stub/configurer/request/triggers_spec.rb +101 -0
  102. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +11 -10
  103. data/spec/lib/http_stub/configurer/server/configuration_spec.rb +139 -0
  104. data/spec/lib/http_stub/configurer/server/facade_spec.rb +65 -142
  105. data/spec/lib/http_stub/configurer/server/request_processor_spec.rb +7 -6
  106. data/spec/lib/http_stub/configurer/server/session_facade_spec.rb +336 -0
  107. data/spec/lib/http_stub/configurer_spec.rb +105 -0
  108. data/spec/lib/http_stub/extensions/core/uri_spec.rb +75 -0
  109. data/spec/lib/http_stub/extensions/rack/handler_spec.rb +1 -1
  110. data/spec/lib/http_stub/extensions/sinatra/namespace_spec.rb +36 -0
  111. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +4 -3
  112. data/spec/lib/http_stub/rake/server_tasks_spec.rb +47 -10
  113. data/spec/lib/http_stub/server/application/application_spec.rb +44 -301
  114. data/spec/lib/http_stub/server/application/configuration_spec.rb +59 -0
  115. data/spec/lib/http_stub/server/application/cross_origin_support_spec.rb +23 -20
  116. data/spec/lib/http_stub/server/application/request_support_integration_spec.rb +93 -0
  117. data/spec/lib/http_stub/server/application/request_support_spec.rb +61 -0
  118. data/spec/lib/http_stub/server/application/response_support_spec.rb +30 -0
  119. data/spec/lib/http_stub/server/application/routes/memory_spec.rb +41 -0
  120. data/spec/lib/http_stub/server/application/routes/resource_spec.rb +16 -0
  121. data/spec/lib/http_stub/server/application/routes/scenario_spec.rb +117 -0
  122. data/spec/lib/http_stub/server/application/routes/session_integration_spec.rb +46 -0
  123. data/spec/lib/http_stub/server/application/routes/session_spec.rb +186 -0
  124. data/spec/lib/http_stub/server/application/{application_integration_spec.rb → routes/stub_integration_spec.rb} +16 -38
  125. data/spec/lib/http_stub/server/application/routes/stub_spec.rb +202 -0
  126. data/spec/lib/http_stub/server/application/session_uri_support_spec.rb +48 -0
  127. data/spec/lib/http_stub/server/application/text_formatting_support_spec.rb +3 -3
  128. data/spec/lib/http_stub/server/memory/controller_spec.rb +47 -0
  129. data/spec/lib/http_stub/server/memory/memory_spec.rb +122 -0
  130. data/spec/lib/http_stub/server/registry_spec.rb +153 -34
  131. data/spec/lib/http_stub/server/request/factory_spec.rb +74 -0
  132. data/spec/lib/http_stub/server/request/parameters_spec.rb +5 -8
  133. data/spec/lib/http_stub/server/request/request_spec.rb +52 -52
  134. data/spec/lib/http_stub/server/request/sinatra_request_spec.rb +132 -0
  135. data/spec/lib/http_stub/server/scenario/controller_spec.rb +121 -35
  136. data/spec/lib/http_stub/server/scenario/not_found_error_spec.rb +25 -0
  137. data/spec/lib/http_stub/server/session/configuration_spec.rb +94 -0
  138. data/spec/lib/http_stub/server/session/controller_spec.rb +106 -0
  139. data/spec/lib/http_stub/server/session/empty_spec.rb +13 -0
  140. data/spec/lib/http_stub/server/session/identifier_strategy_spec.rb +126 -0
  141. data/spec/lib/http_stub/server/session/registry_spec.rb +214 -0
  142. data/spec/lib/http_stub/server/session/session_spec.rb +444 -0
  143. data/spec/lib/http_stub/server/stub/controller_spec.rb +89 -37
  144. data/spec/lib/http_stub/server/stub/empty_spec.rb +2 -2
  145. data/spec/lib/http_stub/server/stub/match/controller_spec.rb +59 -16
  146. data/spec/lib/http_stub/server/stub/payload/response_body_modifier_spec.rb +7 -8
  147. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +42 -15
  148. data/spec/lib/http_stub/server/stub/registry_spec.rb +38 -59
  149. data/spec/lib/http_stub/server/stub/triggers_spec.rb +93 -34
  150. data/spec/spec_helper.rb +13 -1
  151. data/spec/support/contain_file.rb +23 -0
  152. data/spec/support/cross_origin_server/application.rb +1 -1
  153. data/spec/support/cross_origin_server/index_page.rb +2 -2
  154. data/spec/support/cross_origin_server/integration.rb +3 -2
  155. data/spec/support/cross_origin_server/public/index.html +2 -1
  156. data/spec/support/http_stub/configurer_integration.rb +12 -14
  157. data/spec/support/http_stub/server/application/http_stub_rack_application_test.rb +47 -0
  158. data/spec/support/http_stub/server/driver.rb +56 -0
  159. data/spec/support/http_stub/server/memory_fixture.rb +17 -0
  160. data/spec/support/http_stub/server/request_fixture.rb +7 -6
  161. data/spec/support/http_stub/server/scenario_fixture.rb +13 -0
  162. data/spec/support/http_stub/server/session_fixture.rb +27 -0
  163. data/spec/support/http_stub/server_integration.rb +12 -17
  164. data/spec/support/http_stub/stub_fixture.rb +19 -6
  165. data/spec/support/include_in_json.rb +4 -4
  166. data/spec/support/rack/rack_application_test.rb +9 -0
  167. data/spec/support/rack/request_fixture.rb +13 -0
  168. data/spec/support/surpressed_output.rb +10 -0
  169. metadata +149 -36
  170. data/lib/http_stub/configurer/dsl/deprecated.rb +0 -60
  171. data/lib/http_stub/configurer/dsl/endpoint_template.rb +0 -38
  172. data/lib/http_stub/configurer/dsl/scenario_activator.rb +0 -15
  173. data/lib/http_stub/configurer/dsl/stub_activator_builder.rb +0 -24
  174. data/lib/http_stub/configurer/dsl/stub_builder_producer.rb +0 -21
  175. data/lib/http_stub/server/request.rb +0 -12
  176. data/lib/http_stub/server/scenario/activator.rb +0 -25
  177. data/lib/http_stub/server/views/_activate_scenario.haml +0 -23
  178. data/lib/http_stub/server/views/index.haml +0 -8
  179. data/spec/acceptance/activator_spec.rb +0 -81
  180. data/spec/lib/http_stub/configurer/dsl/deprecated_spec.rb +0 -240
  181. data/spec/lib/http_stub/configurer/dsl/endpoint_template_spec.rb +0 -255
  182. data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +0 -62
  183. data/spec/lib/http_stub/configurer/dsl/stub_activator_builder_spec.rb +0 -120
  184. data/spec/lib/http_stub/configurer/dsl/stub_builder_producer_spec.rb +0 -85
  185. data/spec/lib/http_stub/server/request_spec.rb +0 -24
  186. data/spec/lib/http_stub/server/scenario/activator_spec.rb +0 -111
  187. data/spec/support/http_stub/server/scenario/scenario_fixture.rb +0 -15
@@ -4,23 +4,39 @@ describe HttpStub::Configurer::DSL::StubBuilder do
4
4
 
5
5
  let(:builder) { HttpStub::Configurer::DSL::StubBuilder.new(default_builder) }
6
6
 
7
+ shared_context "triggers one scenario" do
8
+
9
+ let(:trigger_scenario) { "Some triggered scenario" }
10
+
11
+ before(:example) { builder.trigger(scenario: trigger_scenario) }
12
+
13
+ end
14
+
15
+ shared_context "triggers many scenarios" do
16
+
17
+ let(:trigger_scenarios) { (1..3).map { |i| "Some triggered scenario #{i}" } }
18
+
19
+ before(:example) { builder.trigger(scenarios: trigger_scenarios) }
20
+
21
+ end
22
+
7
23
  shared_context "triggers one stub" do
8
24
 
9
- let(:trigger_stub) { instance_double(HttpStub::Configurer::Request::Stub) }
10
- let(:trigger_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder, build: trigger_stub) }
25
+ let(:trigger_stub) { instance_double(HttpStub::Configurer::Request::Stub) }
26
+ let(:trigger_stub_builder) { instance_double(described_class, build: trigger_stub) }
11
27
 
12
- before(:example) { builder.trigger(trigger_builder) }
28
+ before(:example) { builder.trigger(stub: trigger_stub_builder) }
13
29
 
14
30
  end
15
31
 
16
32
  shared_context "triggers many stubs" do
17
33
 
18
- let(:trigger_stubs) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
19
- let(:trigger_builders) do
20
- trigger_stubs.map { |stub| instance_double(HttpStub::Configurer::DSL::StubBuilder, build: stub) }
34
+ let(:trigger_stubs) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
35
+ let(:trigger_stub_builders) do
36
+ trigger_stubs.map { |stub| instance_double(described_class, build: stub) }
21
37
  end
22
38
 
23
- before(:example) { builder.trigger(trigger_builders) }
39
+ before(:example) { builder.trigger(stubs: trigger_stub_builders) }
24
40
 
25
41
  end
26
42
 
@@ -30,9 +46,9 @@ describe HttpStub::Configurer::DSL::StubBuilder do
30
46
 
31
47
  attr_reader :merged_stub_builders
32
48
 
33
- def initialize(default_builder)
49
+ def initialize(parent_builder=nil, &block)
34
50
  @merged_stub_builders = []
35
- super(default_builder)
51
+ super(parent_builder, &block)
36
52
  end
37
53
 
38
54
  def merge!(stub_builder)
@@ -41,34 +57,56 @@ describe HttpStub::Configurer::DSL::StubBuilder do
41
57
 
42
58
  end
43
59
 
44
- context "when a default builder is provided" do
60
+ context "when a parent builder is provided" do
45
61
 
46
- let(:default_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
62
+ let(:parent_builder) { instance_double(described_class) }
47
63
 
48
- let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(default_builder) }
64
+ let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(parent_builder) }
49
65
 
50
- it "merges the default builder" do
66
+ it "merges the parent builder" do
51
67
  builder
52
68
 
53
- expect(builder.merged_stub_builders).to eql([ default_builder ])
69
+ expect(builder.merged_stub_builders).to eql([ parent_builder ])
54
70
  end
55
71
 
56
72
  end
57
73
 
58
- end
74
+ context "when a block is provided" do
75
+
76
+ let(:block_verifier) { double("BlockVerifier", verify: nil) }
77
+ let(:block) { lambda { |builder| block_verifier.verify(builder) } }
78
+
79
+ let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(&block) }
59
80
 
60
- context "when a default builder is not provided" do
81
+ it "creates a builder that is yielded to the provided block" do
82
+ expect(block_verifier).to receive(:verify).with(a_kind_of(described_class))
61
83
 
62
- let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(nil) }
84
+ builder
85
+ end
86
+
87
+ it "does not merge any parent builders" do
88
+ builder
89
+
90
+ expect(builder.merged_stub_builders).to eql([])
91
+ end
92
+
93
+ end
94
+
95
+ context "when neither a parent builder or block is provided" do
96
+
97
+ let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(nil) }
98
+
99
+ it "does not merge any parent builder" do
100
+ builder
63
101
 
64
- it "does not merge a builder" do
65
- builder
102
+ expect(builder.merged_stub_builders).to eql([])
103
+ end
66
104
 
67
- expect(builder.merged_stub_builders).to eql([])
68
105
  end
69
106
 
70
107
  end
71
108
 
109
+
72
110
  describe "#match_requests" do
73
111
 
74
112
  let(:fixture) { HttpStub::StubFixture.new }
@@ -139,12 +177,70 @@ describe HttpStub::Configurer::DSL::StubBuilder do
139
177
 
140
178
  describe "#trigger" do
141
179
 
142
- subject { builder.trigger(instance_double(HttpStub::Configurer::DSL::StubBuilder)) }
180
+ let(:args) { {} }
181
+
182
+ subject { builder.trigger(args) }
143
183
 
144
184
  it "returns the builder to support method chaining" do
145
185
  expect(subject).to eql(builder)
146
186
  end
147
187
 
188
+ context "when scenarios are provided" do
189
+
190
+ let(:scenarios) { (1..3).map { |i| "Scenario name #{i}" } }
191
+
192
+ let(:args) { { scenarios: scenarios } }
193
+
194
+ it "adds the scenarios" do
195
+ subject
196
+
197
+ expect(builder.triggers).to include(scenarios: scenarios)
198
+ end
199
+
200
+ end
201
+
202
+ context "when a scenario is provided" do
203
+
204
+ let(:scenario) { "Some scenario name" }
205
+
206
+ let(:args) { { scenario: scenario } }
207
+
208
+ it "adds the scenario" do
209
+ subject
210
+
211
+ expect(builder.triggers).to include(scenarios: [ scenario ])
212
+ end
213
+
214
+ end
215
+
216
+ context "when stubs are provided" do
217
+
218
+ let(:stubs) { (1..3).map { instance_double(described_class) } }
219
+
220
+ let(:args) { { stubs: stubs } }
221
+
222
+ it "adds the stubs" do
223
+ subject
224
+
225
+ expect(builder.triggers).to include(stubs: stubs)
226
+ end
227
+
228
+ end
229
+
230
+ context "when a stub is provided" do
231
+
232
+ let(:stub) { instance_double(described_class) }
233
+
234
+ let(:args) { { stub: stub } }
235
+
236
+ it "adds the stub" do
237
+ subject
238
+
239
+ expect(builder.triggers).to include(stubs: [ stub ])
240
+ end
241
+
242
+ end
243
+
148
244
  end
149
245
 
150
246
  describe "#invoke" do
@@ -181,10 +277,11 @@ describe HttpStub::Configurer::DSL::StubBuilder do
181
277
 
182
278
  shared_context "a completely configured provided builder" do
183
279
 
184
- let(:provided_triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
280
+ let(:provided_trigger_scenarios) { (1..3).map { |i| "Triggered scenario #{i}" } }
281
+ let(:provided_trigger_stub_builders) { (1..3).map { instance_double(described_class) } }
185
282
 
186
283
  let(:provided_builder) do
187
- HttpStub::Configurer::DSL::StubBuilder.new.tap do |builder|
284
+ described_class.new.tap do |builder|
188
285
  builder.match_requests(uri: "/replacement_uri", method: :put,
189
286
  headers: { request_header_key: "replacement request header value",
190
287
  other_request_header_key: "other request header value" },
@@ -195,7 +292,8 @@ describe HttpStub::Configurer::DSL::StubBuilder do
195
292
  other_response_header_key: "other response header value" },
196
293
  body: "replacement body value",
197
294
  delay_in_seconds: 3)
198
- builder.trigger(provided_triggers)
295
+ builder.trigger(scenarios: provided_trigger_scenarios,
296
+ stubs: provided_trigger_stub_builders)
199
297
  end
200
298
  end
201
299
 
@@ -203,7 +301,8 @@ describe HttpStub::Configurer::DSL::StubBuilder do
203
301
 
204
302
  context "when the builder has been completely configured" do
205
303
 
206
- let(:original_triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
304
+ let(:original_trigger_scenarios) { (1..3).map { |i| "Original trigger scenario #{i}" } }
305
+ let(:original_trigger_stub_builders) { (1..3).map { instance_double(described_class) } }
207
306
 
208
307
  before(:example) do
209
308
  builder.match_requests(uri: "/original_uri", method: :get,
@@ -213,7 +312,8 @@ describe HttpStub::Configurer::DSL::StubBuilder do
213
312
  headers: { response_header_key: "original response header value" },
214
313
  body: "original body",
215
314
  delay_in_seconds: 2)
216
- builder.trigger(original_triggers)
315
+ builder.trigger(scenarios: original_trigger_scenarios,
316
+ stubs: original_trigger_stub_builders)
217
317
  end
218
318
 
219
319
  context "and a builder that is completely configured is provided" do
@@ -272,17 +372,23 @@ describe HttpStub::Configurer::DSL::StubBuilder do
272
372
  expect(builder.response).to include(delay_in_seconds: 3)
273
373
  end
274
374
 
275
- it "adds to the triggers" do
375
+ it "adds to the triggered scenarios" do
376
+ subject
377
+
378
+ expect(builder.triggers).to include(scenarios: original_trigger_scenarios + provided_trigger_scenarios)
379
+ end
380
+
381
+ it "adds to the triggered stubs" do
276
382
  subject
277
383
 
278
- expect(builder.triggers).to eql(original_triggers + provided_triggers)
384
+ expect(builder.triggers).to include(stubs: original_trigger_stub_builders + provided_trigger_stub_builders)
279
385
  end
280
386
 
281
387
  end
282
388
 
283
389
  context "and a builder that is empty is provided" do
284
390
 
285
- let(:provided_builder) { HttpStub::Configurer::DSL::StubBuilder.new }
391
+ let(:provided_builder) { described_class.new }
286
392
 
287
393
  it "preserves the uri" do
288
394
  subject
@@ -332,10 +438,16 @@ describe HttpStub::Configurer::DSL::StubBuilder do
332
438
  expect(builder.response).to include(delay_in_seconds: 2)
333
439
  end
334
440
 
335
- it "preserves the triggers" do
441
+ it "preserves the triggered scenarios" do
442
+ subject
443
+
444
+ expect(builder.triggers).to include(scenarios: original_trigger_scenarios)
445
+ end
446
+
447
+ it "preserves the triggered stubs" do
336
448
  subject
337
449
 
338
- expect(builder.triggers).to eql(original_triggers)
450
+ expect(builder.triggers).to include(stubs: original_trigger_stub_builders)
339
451
  end
340
452
 
341
453
  end
@@ -398,10 +510,16 @@ describe HttpStub::Configurer::DSL::StubBuilder do
398
510
  expect(builder.response).to include(delay_in_seconds: 3)
399
511
  end
400
512
 
401
- it "assumes the provided triggers" do
513
+ it "assumes the provided triggered scenarios" do
402
514
  subject
403
515
 
404
- expect(builder.triggers).to eql(provided_triggers)
516
+ expect(builder.triggers).to include(scenarios: provided_trigger_scenarios)
517
+ end
518
+
519
+ it "assumes the provided triggered stubs" do
520
+ subject
521
+
522
+ expect(builder.triggers).to include(stubs: provided_trigger_stub_builders)
405
523
  end
406
524
 
407
525
  end
@@ -411,7 +529,7 @@ describe HttpStub::Configurer::DSL::StubBuilder do
411
529
  describe "#build" do
412
530
 
413
531
  let(:fixture) { HttpStub::StubFixture.new }
414
- let(:triggers) { [] }
532
+ let(:triggers) { { scenarios: [], stubs: [] } }
415
533
  let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
416
534
 
417
535
  subject do
@@ -440,22 +558,44 @@ describe HttpStub::Configurer::DSL::StubBuilder do
440
558
 
441
559
  describe "creates a stub payload with triggers that" do
442
560
 
443
- context "when a trigger is added" do
561
+ context "when a scenario trigger is added" do
562
+ include_context "triggers one scenario"
563
+
564
+ it "contains the provided trigger scenario name" do
565
+ expect_stub_to_be_created_with_triggers(scenario_names: [ trigger_scenario ])
566
+
567
+ subject
568
+ end
569
+
570
+ end
571
+
572
+ context "when many scenario triggers are added" do
573
+ include_context "triggers many scenarios"
574
+
575
+ it "contains the provided trigger scenario names" do
576
+ expect_stub_to_be_created_with_triggers(scenario_names: trigger_scenarios)
577
+
578
+ subject
579
+ end
580
+
581
+ end
582
+
583
+ context "when a stub trigger is added" do
444
584
  include_context "triggers one stub"
445
585
 
446
- it "contain the provided trigger builder" do
447
- expect_stub_to_be_created_with(triggers: [ trigger_builder ])
586
+ it "contains the provided trigger builder" do
587
+ expect_stub_to_be_created_with_triggers(stubs: [ trigger_stub ])
448
588
 
449
589
  subject
450
590
  end
451
591
 
452
592
  end
453
593
 
454
- context "when many triggers are added" do
594
+ context "when many stub triggers are added" do
455
595
  include_context "triggers many stubs"
456
596
 
457
- it "contain the provided trigger builders" do
458
- expect_stub_to_be_created_with(triggers: trigger_builders)
597
+ it "contains the provided trigger builders" do
598
+ expect_stub_to_be_created_with_triggers(stubs: trigger_stubs)
459
599
 
460
600
  subject
461
601
  end
@@ -485,12 +625,17 @@ describe HttpStub::Configurer::DSL::StubBuilder do
485
625
  delay_in_seconds: 8
486
626
  }
487
627
  end
488
- let(:trigger_defaults) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
628
+ let(:trigger_scenario_defaults) { (1..3).map { |i| "Default trigger scenario #{i}" } }
629
+ let(:trigger_stub_defaults) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
630
+ let(:trigger_stub_builder_defaults) do
631
+ trigger_stub_defaults.map { |stub| instance_double(described_class, build: stub) }
632
+ end
489
633
 
490
634
  let(:default_builder) do
491
- instance_double(HttpStub::Configurer::DSL::StubBuilder, request: request_defaults,
492
- response: response_defaults,
493
- triggers: trigger_defaults)
635
+ instance_double(described_class, request: request_defaults,
636
+ response: response_defaults,
637
+ triggers: { scenarios: trigger_scenario_defaults,
638
+ stubs: trigger_stub_builder_defaults })
494
639
  end
495
640
 
496
641
  describe "the built request payload" do
@@ -557,12 +702,24 @@ describe HttpStub::Configurer::DSL::StubBuilder do
557
702
 
558
703
  end
559
704
 
560
- describe "the built response triggers" do
705
+ describe "the built triggers payload" do
706
+
707
+ let(:trigger_scenarios) { (1..3).map { |i| "Trigger scenario #{i}" } }
708
+ let(:trigger_stubs) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
709
+ let(:trigger_stub_builders) do
710
+ trigger_stubs.map { |stub| instance_double(described_class, build: stub) }
711
+ end
712
+
713
+ let(:triggers) { { scenarios: trigger_scenarios, stubs: trigger_stub_builders } }
714
+
715
+ it "combines any scenario defaults with values established in the stub" do
716
+ expect_stub_to_be_created_with_triggers(scenario_names: trigger_scenario_defaults + trigger_scenarios)
561
717
 
562
- let(:triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
718
+ subject
719
+ end
563
720
 
564
- it "combines any defaults with values values established in the stub" do
565
- expect_stub_to_be_created_with(triggers: trigger_defaults + triggers)
721
+ it "combines any stub defaults with values established in the stub" do
722
+ expect_stub_to_be_created_with_triggers(stubs: trigger_stub_defaults + trigger_stubs)
566
723
 
567
724
  subject
568
725
  end
@@ -581,6 +738,10 @@ describe HttpStub::Configurer::DSL::StubBuilder do
581
738
  expect(HttpStub::Configurer::Request::Stub).to receive(:new).with(hash_including(args))
582
739
  end
583
740
 
741
+ def expect_stub_to_be_created_with_triggers(args)
742
+ expect_stub_to_be_created_with(triggers: hash_including(args))
743
+ end
744
+
584
745
  end
585
746
 
586
747
  end
@@ -0,0 +1,203 @@
1
+ describe HttpStub::Configurer::DSL::StubBuilderTemplate do
2
+
3
+ let(:parent_uri) { "/some/parent/uri" }
4
+ let(:parent_template) do
5
+ described_class.new.tap do |template|
6
+ template.match_requests(uri: parent_uri)
7
+ template.respond_with(body: "parent template body")
8
+ end
9
+ end
10
+ let(:initial_request_method) { :put }
11
+ let(:initial_block) { lambda { |stub| stub.match_requests(method: initial_request_method) } }
12
+
13
+ let(:stub_builder_template) { described_class.new }
14
+
15
+ shared_context "mocked stub builder" do
16
+
17
+ let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
18
+
19
+ before(:example) { allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).and_return(stub_builder) }
20
+
21
+ end
22
+
23
+ it "creates a stub builder to hold values established on the template" do
24
+ allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new)
25
+
26
+ stub_builder_template
27
+ end
28
+
29
+ describe "#match_requests" do
30
+ include_context "mocked stub builder"
31
+
32
+ let(:request_matching_rules) { { uri: "/some/stub/uri", method: :get } }
33
+
34
+ subject { stub_builder_template.match_requests(request_matching_rules) }
35
+
36
+ it "delegates to the templates stub builder" do
37
+ expect(stub_builder).to receive(:match_requests).with(request_matching_rules)
38
+
39
+ subject
40
+ end
41
+
42
+ end
43
+
44
+ describe "#schema" do
45
+ include_context "mocked stub builder"
46
+
47
+ let(:type) { :some_schema_type }
48
+ let(:definition) { "some schema definition" }
49
+
50
+ subject { stub_builder_template.schema(type, definition) }
51
+
52
+ it "delegates to the templates stub builder" do
53
+ expect(stub_builder).to receive(:schema).with(type, definition)
54
+
55
+ subject
56
+ end
57
+
58
+ it "returns the created schema" do
59
+ schema = { schema: :some_schema }
60
+ expect(stub_builder).to receive(:schema).and_return(schema)
61
+
62
+ expect(subject).to eql(schema)
63
+ end
64
+
65
+ end
66
+
67
+ describe "#respond_with" do
68
+ include_context "mocked stub builder"
69
+
70
+ let(:response_settings) { { status: 204 } }
71
+
72
+ subject { stub_builder_template.respond_with(response_settings) }
73
+
74
+ it "delegates to the templates stub builder" do
75
+ expect(stub_builder).to receive(:respond_with).with(response_settings)
76
+
77
+ subject
78
+ end
79
+
80
+ end
81
+
82
+ describe "#trigger" do
83
+ include_context "mocked stub builder"
84
+
85
+ let(:trigger_settings) { { scenario: "some scenario name" } }
86
+
87
+ subject { stub_builder_template.trigger(trigger_settings) }
88
+
89
+ it "delegates to the templates stub builder" do
90
+ expect(stub_builder).to receive(:trigger).with(trigger_settings)
91
+
92
+ subject
93
+ end
94
+
95
+ end
96
+
97
+ describe "#invoke" do
98
+ include_context "mocked stub builder"
99
+
100
+ let(:block_verifier) { double("BlockVerifier") }
101
+ let(:block) do
102
+ lambda do |stub|
103
+ @yielded_stub = stub
104
+ block_verifier.verify
105
+ end
106
+ end
107
+
108
+ let(:stub_builder) { HttpStub::Configurer::DSL::StubBuilder.new }
109
+
110
+ subject { stub_builder_template.invoke(&block) }
111
+
112
+ it "invokes the block with the templates stub builder" do
113
+ expect(block_verifier).to receive(:verify)
114
+
115
+ subject
116
+
117
+ expect(@yielded_stub).to eql(stub_builder)
118
+ end
119
+
120
+ end
121
+
122
+ describe "#build_stub" do
123
+
124
+ let(:stub_builder_template) { described_class.new(parent_template) }
125
+ let(:response_overrides) { { status: 201, body: "response overrides body" } }
126
+ let(:block) do
127
+ lambda do |stub|
128
+ stub.match_requests(uri: "/some/block/uri")
129
+ stub.respond_with(body: "block body")
130
+ end
131
+ end
132
+
133
+ let(:stub_builder_template) { described_class.new(parent_template) }
134
+
135
+ subject { stub_builder_template.build_stub }
136
+
137
+ context "when a parent template had been provided" do
138
+
139
+ it "creates a stub capable of containing values established in the parent template" do
140
+ expect(subject.request[:uri]).to eql(parent_uri)
141
+ end
142
+
143
+ end
144
+
145
+ context "when an initial block had been provided" do
146
+
147
+ let(:stub_builder_template) { described_class.new(&initial_block) }
148
+
149
+ it "creates a stub capable of containing values established in the initial block" do
150
+ expect(subject.request[:method]).to eql(initial_request_method)
151
+ end
152
+
153
+ end
154
+
155
+ context "when response overrides are provided upon building the stub" do
156
+
157
+ subject { stub_builder_template.build_stub(response_overrides) }
158
+
159
+ it "creates a stub containing the overrides" do
160
+ expect(subject.response[:status]).to eql(201)
161
+ end
162
+
163
+ it "overrides any values initially established" do
164
+ expect(subject.response[:body]).to eql("response overrides body")
165
+ end
166
+
167
+ end
168
+
169
+ context "when a block is provided upon building the stub" do
170
+
171
+ subject { stub_builder_template.build_stub(&block) }
172
+
173
+ it "creates a stub that is modified by the block" do
174
+ expect(subject.request[:uri]).to eql("/some/block/uri")
175
+ end
176
+
177
+ it "overrides any values initially established" do
178
+ expect(subject.response[:body]).to eql("block body")
179
+ end
180
+
181
+ end
182
+
183
+ context "when both response overrides and a block are provided" do
184
+
185
+ subject { stub_builder_template.build_stub(response_overrides, &block) }
186
+
187
+ it "creates a stub containing response overrides" do
188
+ expect(subject.response[:status]).to eql(201)
189
+ end
190
+
191
+ it "creates a stub that is modified by the block" do
192
+ expect(subject.request[:uri]).to eql("/some/block/uri")
193
+ end
194
+
195
+ it "creates a stub with values established in the block taking precedence over response overrides" do
196
+ expect(subject.response[:body]).to eql("block body")
197
+ end
198
+
199
+ end
200
+
201
+ end
202
+
203
+ end