http_stub 0.17.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,11 +17,12 @@ describe HttpStub::Configurer::DSL::ScenarioBuilder do
17
17
 
18
18
  describe "#add_stub!" do
19
19
 
20
- let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
20
+ let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder, invoke: nil) }
21
21
 
22
22
  context "when a block is provided" do
23
23
 
24
- let(:block) { lambda { |_builder| "some block" } }
24
+ let(:block_verifier) { double("BlockVerifier") }
25
+ let(:block) { lambda { block_verifier.verify } }
25
26
 
26
27
  subject { scenario_builder.add_stub!(&block) }
27
28
 
@@ -33,8 +34,9 @@ describe HttpStub::Configurer::DSL::ScenarioBuilder do
33
34
  subject
34
35
  end
35
36
 
36
- it "yields the stub builder to the provided block" do
37
- expect(block).to receive(:call).with(stub_builder)
37
+ it "requests the stub builder invoke the provided block" do
38
+ expect(stub_builder).to receive(:invoke).and_yield
39
+ expect(block_verifier).to receive(:verify)
38
40
 
39
41
  subject
40
42
  end
@@ -81,7 +83,7 @@ describe HttpStub::Configurer::DSL::ScenarioBuilder do
81
83
 
82
84
  let(:stubs) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
83
85
  let(:stub_builders) do
84
- stubs.map { |stub| instance_double(HttpStub::Configurer::DSL::StubBuilder, build: stub) }
86
+ stubs.map { |stub| instance_double(HttpStub::Configurer::DSL::StubBuilder, invoke: nil, build: stub) }
85
87
  end
86
88
 
87
89
  before(:example) do
@@ -4,6 +4,14 @@ describe HttpStub::Configurer::DSL::Server do
4
4
 
5
5
  let(:server) { HttpStub::Configurer::DSL::Server.new(server_facade) }
6
6
 
7
+ shared_context "establish response defaults" do
8
+
9
+ let(:response_defaults) { { key: "value" } }
10
+
11
+ before(:example) { server.response_defaults = { key: "value" } }
12
+
13
+ end
14
+
7
15
  it "produces stub builders" do
8
16
  expect(server).to be_a(HttpStub::Configurer::DSL::StubBuilderProducer)
9
17
  end
@@ -70,9 +78,13 @@ describe HttpStub::Configurer::DSL::Server do
70
78
 
71
79
  context "when a stub builder is not provided" do
72
80
 
73
- let(:block) { lambda { |_stub| "some block" } }
81
+ let(:block_verifier) { double("BlockVerifier") }
82
+ let(:block) { lambda { block_verifier.verify } }
74
83
 
75
- before(:example) { allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).and_return(stub_builder) }
84
+ before(:example) do
85
+ allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).and_return(stub_builder)
86
+ allow(stub_builder).to receive(:invoke)
87
+ end
76
88
 
77
89
  subject { server.add_stub!(&block) }
78
90
 
@@ -82,8 +94,9 @@ describe HttpStub::Configurer::DSL::Server do
82
94
  subject
83
95
  end
84
96
 
85
- it "yields the created builder to the provided block" do
86
- expect(block).to receive(:call).with(stub_builder)
97
+ it "requests the created builder invoke the provided block" do
98
+ expect(stub_builder).to receive(:invoke).and_yield
99
+ expect(block_verifier).to receive(:verify)
87
100
 
88
101
  subject
89
102
  end
@@ -112,9 +125,7 @@ describe HttpStub::Configurer::DSL::Server do
112
125
 
113
126
  context "when response defaults have been established" do
114
127
 
115
- let(:response_defaults) { { key: "value" } }
116
-
117
- before(:example) { server.response_defaults = { key: "value" } }
128
+ include_context "establish response defaults"
118
129
 
119
130
  it "creates a scenario builder containing the response defaults" do
120
131
  expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(response_defaults, anything)
@@ -160,6 +171,102 @@ describe HttpStub::Configurer::DSL::Server do
160
171
 
161
172
  end
162
173
 
174
+ describe "#add_one_stub_scenario!" do
175
+
176
+ let(:scenario_name) { "some_scenario_name" }
177
+ let(:block_verifier) { double("BlockVerifier") }
178
+ let(:block) { lambda { block_verifier.verify } }
179
+
180
+ let(:scenario_builder) { instance_double(HttpStub::Configurer::DSL::ScenarioBuilder) }
181
+ let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder, invoke: nil) }
182
+
183
+ subject { server.add_one_stub_scenario!(scenario_name, &block) }
184
+
185
+ before(:each) do
186
+ allow(server).to receive(:add_scenario!).and_yield(scenario_builder)
187
+ allow(scenario_builder).to receive(:add_stub!).and_yield(stub_builder)
188
+ end
189
+
190
+ it "adds a scenario with the provided name" do
191
+ expect(server).to receive(:add_scenario!).with(scenario_name)
192
+
193
+ subject
194
+ end
195
+
196
+ it "adds a stub to the scenario" do
197
+ expect(scenario_builder).to receive(:add_stub!)
198
+
199
+ subject
200
+ end
201
+
202
+ it "requests the stub builder invoke the provided block" do
203
+ expect(stub_builder).to receive(:invoke).and_yield
204
+ expect(block_verifier).to receive(:verify)
205
+
206
+ subject
207
+ end
208
+
209
+ end
210
+
211
+ describe "#endpoint_template" do
212
+
213
+ let(:block_verifier) { double("BlockVerifier") }
214
+ let(:block) { lambda { block_verifier.verify } }
215
+ let(:endpoint_template) { instance_double(HttpStub::Configurer::DSL::EndpointTemplate, invoke: nil) }
216
+
217
+ subject { server.endpoint_template(&block) }
218
+
219
+ before(:example) do
220
+ allow(HttpStub::Configurer::DSL::EndpointTemplate).to receive(:new).and_return(endpoint_template)
221
+ end
222
+
223
+ it "requests the endpoint template invoke the provided block" do
224
+ expect(endpoint_template).to receive(:invoke).and_yield
225
+ expect(block_verifier).to receive(:verify)
226
+
227
+ subject
228
+ end
229
+
230
+ it "returns the created endpoint template" do
231
+ expect(subject).to eql(endpoint_template)
232
+ end
233
+
234
+ it "creates a template for the server" do
235
+ expect(HttpStub::Configurer::DSL::EndpointTemplate).to(
236
+ receive(:new).with(server, anything).and_return(endpoint_template)
237
+ )
238
+
239
+ subject
240
+ end
241
+
242
+ context "when response defaults have been established" do
243
+
244
+ include_context "establish response defaults"
245
+
246
+ it "creates a template with the defaults" do
247
+ expect(HttpStub::Configurer::DSL::EndpointTemplate).to(
248
+ receive(:new).with(anything, response_defaults).and_return(endpoint_template)
249
+ )
250
+
251
+ subject
252
+ end
253
+
254
+ end
255
+
256
+ context "when no response defaults have been established" do
257
+
258
+ it "creates a template with empty response defaults" do
259
+ expect(HttpStub::Configurer::DSL::EndpointTemplate).to(
260
+ receive(:new).with(anything, {}).and_return(endpoint_template)
261
+ )
262
+
263
+ subject
264
+ end
265
+
266
+ end
267
+
268
+ end
269
+
163
270
  describe "#add_activator!" do
164
271
 
165
272
  let(:scenario) { instance_double(HttpStub::Configurer::Request::Scenario) }
@@ -34,40 +34,20 @@ describe HttpStub::Configurer::DSL::StubBuilderProducer do
34
34
 
35
35
  context "when a block is provided" do
36
36
 
37
- context "that declares a stub builder parameter" do
37
+ let(:block) { lambda { |_stub_builder| "some block" } }
38
38
 
39
- let(:block) { lambda { |_stub_builder| "some block" } }
39
+ subject { producer.build_stub(&block) }
40
40
 
41
- subject { producer.build_stub(&block) }
41
+ before(:example) { allow(stub_builder).to receive(:invoke) }
42
42
 
43
- it "yields the created builder to the provided block" do
44
- expect(block).to receive(:call).with(stub_builder)
45
-
46
- subject
47
- end
48
-
49
- it "returns the created builder" do
50
- expect(subject).to eql(stub_builder)
51
- end
43
+ it "requests the stub builder invoke block" do
44
+ expect(stub_builder).to receive(:invoke).and_yield(block)
52
45
 
46
+ subject
53
47
  end
54
48
 
55
- context "that has no parameters" do
56
-
57
- subject { producer.build_stub { match_requests("/some/request/uri") } }
58
-
59
- before(:example) { allow(stub_builder).to receive(:match_requests) }
60
-
61
- it "executes the block in the context of the builder" do
62
- expect(stub_builder).to receive(:match_requests).with("/some/request/uri")
63
-
64
- subject
65
- end
66
-
67
- it "returns the created builder" do
68
- expect(subject).to eql(stub_builder)
69
- end
70
-
49
+ it "returns the stub builder" do
50
+ expect(subject).to eql(stub_builder)
71
51
  end
72
52
 
73
53
  end
@@ -76,7 +56,7 @@ describe HttpStub::Configurer::DSL::StubBuilderProducer do
76
56
 
77
57
  subject { producer.build_stub }
78
58
 
79
- it "returns the built stub" do
59
+ it "returns the stub builder" do
80
60
  expect(subject).to eql(stub_builder)
81
61
  end
82
62
 
@@ -84,6 +84,269 @@ describe HttpStub::Configurer::DSL::StubBuilder do
84
84
 
85
85
  end
86
86
 
87
+ describe "#invoke" do
88
+
89
+ context "when the block accepts an argument" do
90
+
91
+ subject { builder.invoke { |builder| builder.match_requests("/some_uri") } }
92
+
93
+ it "invokes the block with the builder as the argument" do
94
+ expect(builder).to receive(:match_requests).with("/some_uri")
95
+
96
+ subject
97
+ end
98
+
99
+ end
100
+
101
+ context "when the block accepts no arguments" do
102
+
103
+ subject { builder.invoke { match_requests("/some_uri") } }
104
+
105
+ it "invokes the block in the context of the builder" do
106
+ expect(builder).to receive(:match_requests).with("/some_uri")
107
+
108
+ subject
109
+ end
110
+
111
+ end
112
+
113
+ end
114
+
115
+ describe "#merge!" do
116
+
117
+ subject { builder.merge!(provided_builder) }
118
+
119
+ shared_context "a completely configured provided builder" do
120
+
121
+ let(:provided_triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
122
+
123
+ let(:provided_builder) do
124
+ HttpStub::Configurer::DSL::StubBuilder.new({}).tap do |builder|
125
+ builder.match_requests("/replacement_uri", method: :put,
126
+ headers: { request_header_key: "replacement request header value",
127
+ other_request_header_key: "other request header value" },
128
+ parameters: { parameter_key: "replacement parameter value",
129
+ other_request_parameter_key: "other request parameter value" })
130
+ builder.respond_with(status: 203,
131
+ headers: { response_header_key: "reaplcement response header value",
132
+ other_response_header_key: "other response header value" },
133
+ body: "replacement body value",
134
+ delay_in_seconds: 3)
135
+ builder.trigger(provided_triggers)
136
+ end
137
+ end
138
+
139
+ end
140
+
141
+ context "when the builder has been completely configured" do
142
+
143
+ let(:original_triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
144
+
145
+ before(:example) do
146
+ builder.match_requests("/original_uri", method: :get,
147
+ headers: { request_header_key: "original request header value" },
148
+ parameters: { parameter_key: "original parameter value" })
149
+ builder.respond_with(status: 202,
150
+ headers: { response_header_key: "original response header value" },
151
+ body: "original body",
152
+ delay_in_seconds: 2)
153
+ builder.trigger(original_triggers)
154
+ end
155
+
156
+ context "and a builder that is completely configured is provided" do
157
+
158
+ include_context "a completely configured provided builder"
159
+
160
+ it "replaces the uri" do
161
+ subject
162
+
163
+ expect(builder.request).to include(uri: "/replacement_uri")
164
+ end
165
+
166
+ it "replaces the request method" do
167
+ subject
168
+
169
+ expect(builder.request).to include(method: :put)
170
+ end
171
+
172
+ it "deeply merges the request headers" do
173
+ subject
174
+
175
+ expect(builder.request).to include(headers: { request_header_key: "replacement request header value",
176
+ other_request_header_key: "other request header value" })
177
+ end
178
+
179
+ it "deeply merges the request parameters" do
180
+ subject
181
+
182
+ expect(builder.request).to(
183
+ include(parameters: { parameter_key: "replacement parameter value",
184
+ other_request_parameter_key: "other request parameter value" })
185
+ )
186
+ end
187
+
188
+ it "replaces the response status" do
189
+ subject
190
+
191
+ expect(builder.response).to include(status: 203)
192
+ end
193
+
194
+ it "deeply merges the response headers" do
195
+ subject
196
+
197
+ expect(builder.response).to include(headers: { response_header_key: "reaplcement response header value",
198
+ other_response_header_key: "other response header value" })
199
+ end
200
+
201
+ it "replaces the body" do
202
+ subject
203
+
204
+ expect(builder.response).to include(body: "replacement body value")
205
+ end
206
+
207
+ it "replaces the response delay" do
208
+ subject
209
+
210
+ expect(builder.response).to include(delay_in_seconds: 3)
211
+ end
212
+
213
+ it "adds to the triggers" do
214
+ subject
215
+
216
+ expect(builder.triggers).to eql(original_triggers + provided_triggers)
217
+ end
218
+
219
+ end
220
+
221
+ context "and a builder that is empty is provided" do
222
+
223
+ let(:provided_builder) { HttpStub::Configurer::DSL::StubBuilder.new({}) }
224
+
225
+ it "preserves the uri" do
226
+ subject
227
+
228
+ expect(builder.request).to include(uri: "/original_uri")
229
+ end
230
+
231
+ it "preserves the request method" do
232
+ subject
233
+
234
+ expect(builder.request).to include(method: :get)
235
+ end
236
+
237
+ it "preserves the request headers" do
238
+ subject
239
+
240
+ expect(builder.request).to include(headers: { request_header_key: "original request header value" })
241
+ end
242
+
243
+ it "preserves the request parameters" do
244
+ subject
245
+
246
+ expect(builder.request).to include(parameters: { parameter_key: "original parameter value" })
247
+ end
248
+
249
+ it "preserves the response status" do
250
+ subject
251
+
252
+ expect(builder.response).to include(status: 202)
253
+ end
254
+
255
+ it "preserves the response headers" do
256
+ subject
257
+
258
+ expect(builder.response).to include(headers: { response_header_key: "original response header value" })
259
+ end
260
+
261
+ it "preserves the body" do
262
+ subject
263
+
264
+ expect(builder.response).to include(body: "original body")
265
+ end
266
+
267
+ it "preserves the response delay" do
268
+ subject
269
+
270
+ expect(builder.response).to include(delay_in_seconds: 2)
271
+ end
272
+
273
+ it "preserves the triggers" do
274
+ subject
275
+
276
+ expect(builder.triggers).to eql(original_triggers)
277
+ end
278
+
279
+ end
280
+
281
+ end
282
+
283
+ context "when the builder has not been previously configured" do
284
+
285
+ include_context "a completely configured provided builder"
286
+
287
+ it "assumes the provided uri" do
288
+ subject
289
+
290
+ expect(builder.request).to include(uri: "/replacement_uri")
291
+ end
292
+
293
+ it "assumes the provided request method" do
294
+ subject
295
+
296
+ expect(builder.request).to include(method: :put)
297
+ end
298
+
299
+ it "assumes the provided request headers" do
300
+ subject
301
+
302
+ expect(builder.request).to include(headers: { request_header_key: "replacement request header value",
303
+ other_request_header_key: "other request header value" })
304
+ end
305
+
306
+ it "assumes the provided request parameters" do
307
+ subject
308
+
309
+ expect(builder.request).to(
310
+ include(parameters: { parameter_key: "replacement parameter value",
311
+ other_request_parameter_key: "other request parameter value" })
312
+ )
313
+ end
314
+
315
+ it "assumes the provided response status" do
316
+ subject
317
+
318
+ expect(builder.response).to include(status: 203)
319
+ end
320
+
321
+ it "assumes the provided response headers" do
322
+ subject
323
+
324
+ expect(builder.response).to include(headers: { response_header_key: "reaplcement response header value",
325
+ other_response_header_key: "other response header value" })
326
+ end
327
+
328
+ it "assumes the provided response body" do
329
+ subject
330
+
331
+ expect(builder.response).to include(body: "replacement body value")
332
+ end
333
+
334
+ it "assumes the provided response delay" do
335
+ subject
336
+
337
+ expect(builder.response).to include(delay_in_seconds: 3)
338
+ end
339
+
340
+ it "assumes the provided triggers" do
341
+ subject
342
+
343
+ expect(builder.triggers).to eql(provided_triggers)
344
+ end
345
+
346
+ end
347
+
348
+ end
349
+
87
350
  describe "#build" do
88
351
 
89
352
  let(:fixture) { HttpStub::StubFixture.new }