http_stub 0.14.0.rc3 → 0.14.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae3af84821967d62af59d061889031ec39fdd2a2
4
- data.tar.gz: 60a07c4687d3dd2f0a5490c1059f30335ec28b87
3
+ metadata.gz: 6135f0cdbd62b324fd90c2c2262df63b191ada36
4
+ data.tar.gz: 43316b361b5b86172713fd5b89250576f5a7c20e
5
5
  SHA512:
6
- metadata.gz: 1379954004bf0aee00104bc4efc8212e1e79ca491ee5d2efe596d0bf0de54a9365b5524368aa943ceb5fb13de545c280b633d0b0ab2629c77d01452681395b50
7
- data.tar.gz: dc7d720c7a7aa31b1a75cf0fdab59761c5e6c6da3ceaa0cd06d3c11015acd2c8bcac38431d69b7c5543a4b52fa2f4deb1db889bdd8c6323ac687241b7ad2e5e1
6
+ metadata.gz: 2ab8fb26a5673af9a178a01279538a62419b5ce6f47165089ded6cb45cf79ac325af950256c94f6e30e9dfbf05f45b07c3fb1e606a3da98917732638a33a4254
7
+ data.tar.gz: f80869657a7da4fe4f613b322491d8c3d72297432d4150ef049cff8e289f0b6d370a9923b34e835846059fac31d7011d2c0bb32e81748c435b40f13a9831c0b7
data/lib/http_stub.rb CHANGED
@@ -7,6 +7,7 @@ require 'json'
7
7
  require 'http_server_manager'
8
8
  require 'active_support/core_ext/module/delegation'
9
9
  require 'active_support/core_ext/hash/slice'
10
+ require 'active_support/core_ext/hash/deep_merge'
10
11
 
11
12
  require_relative 'http_stub/hash_extensions'
12
13
  require_relative 'http_stub/models/headers'
@@ -4,10 +4,10 @@ module HttpStub
4
4
 
5
5
  class StubActivatorPayloadBuilder
6
6
 
7
- delegate :match_requests, :respond_with, :and_add_stub, :and_add_stubs, to: :@stub_payload_builder
7
+ delegate :match_requests, :respond_with, :trigger, to: :@stub_payload_builder
8
8
 
9
- def initialize
10
- @stub_payload_builder = HttpStub::Configurer::Request::StubPayloadBuilder.new
9
+ def initialize(response_defaults)
10
+ @stub_payload_builder = HttpStub::Configurer::Request::StubPayloadBuilder.new(response_defaults)
11
11
  end
12
12
 
13
13
  def on(uri)
@@ -4,8 +4,8 @@ module HttpStub
4
4
 
5
5
  class StubPayloadBuilder
6
6
 
7
- def initialize
8
- @args = { triggers: [] }
7
+ def initialize(response_defaults)
8
+ @args = { response: response_defaults ? response_defaults.clone : {}, triggers: [] }
9
9
  end
10
10
 
11
11
  def match_requests(uri, args={})
@@ -14,15 +14,13 @@ module HttpStub
14
14
  end
15
15
 
16
16
  def respond_with(args)
17
- @args.merge!(response: args)
17
+ @args[:response].deep_merge!(args)
18
18
  end
19
19
 
20
- def and_add_stub(stub_builder)
21
- @args[:triggers] << stub_builder
22
- end
23
-
24
- def and_add_stubs(stub_builders)
25
- @args[:triggers].concat(stub_builders)
20
+ def trigger(stub_builder_or_builders)
21
+ resolved_builders =
22
+ stub_builder_or_builders.is_a?(Array) ? stub_builder_or_builders : [ stub_builder_or_builders ]
23
+ @args[:triggers].concat(resolved_builders)
26
24
  end
27
25
 
28
26
  def build
@@ -5,7 +5,12 @@ module HttpStub
5
5
  class DSL
6
6
 
7
7
  def initialize(server_facade)
8
- @server_facade = server_facade
8
+ @server_facade = server_facade
9
+ @response_defaults = {}
10
+ end
11
+
12
+ def response_defaults=(args)
13
+ @response_defaults = args
9
14
  end
10
15
 
11
16
  def has_started!
@@ -13,7 +18,7 @@ module HttpStub
13
18
  end
14
19
 
15
20
  def build_stub(&block)
16
- builder = HttpStub::Configurer::Request::StubPayloadBuilder.new
21
+ builder = HttpStub::Configurer::Request::StubPayloadBuilder.new(@response_defaults)
17
22
  block.call(builder) if block_given?
18
23
  builder
19
24
  end
@@ -28,7 +33,7 @@ module HttpStub
28
33
  end
29
34
 
30
35
  def add_activator!(&block)
31
- builder = HttpStub::Configurer::Request::StubActivatorPayloadBuilder.new
36
+ builder = HttpStub::Configurer::Request::StubActivatorPayloadBuilder.new(@response_defaults)
32
37
  block.call(builder)
33
38
  @server_facade.stub_activator(HttpStub::Configurer::Request::StubActivator.new(builder.build))
34
39
  end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.14.0.rc3"
2
+ VERSION = "0.14.0.rc4".freeze
3
3
  end
@@ -2,15 +2,28 @@ describe HttpStub::Configurer::Request::StubActivatorPayloadBuilder do
2
2
 
3
3
  include_context "stub payload builder arguments"
4
4
 
5
- let(:stub_payload) { { "stub_payload_key" => "stub payload value" } }
6
- let(:stub_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder, build: stub_payload) }
5
+ let(:response_defaults) { {} }
6
+ let(:stub_payload) { { "stub_payload_key" => "stub payload value" } }
7
+ let(:stub_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder, build: stub_payload) }
7
8
 
8
- let(:builder) { HttpStub::Configurer::Request::StubActivatorPayloadBuilder.new }
9
+ let(:builder) { HttpStub::Configurer::Request::StubActivatorPayloadBuilder.new(response_defaults) }
9
10
 
10
11
  before(:example) do
11
12
  allow(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).and_return(stub_builder)
12
13
  end
13
14
 
15
+ describe "constructor" do
16
+
17
+ subject { builder }
18
+
19
+ it "creates a stub payload builder with the provided response defaults" do
20
+ expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).with(response_defaults)
21
+
22
+ subject
23
+ end
24
+
25
+ end
26
+
14
27
  describe "#match_requests" do
15
28
 
16
29
  it "delegates to a stub payload builder" do
@@ -31,26 +44,30 @@ describe HttpStub::Configurer::Request::StubActivatorPayloadBuilder do
31
44
 
32
45
  end
33
46
 
34
- describe "#and_add_stub" do
47
+ describe "#trigger" do
35
48
 
36
- let(:trigger_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) }
49
+ context "when one triggered stub is provided" do
37
50
 
38
- it "delegates to a stub payload builder" do
39
- expect(stub_builder).to receive(:and_add_stub).with(trigger_builder)
51
+ let(:trigger_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) }
52
+
53
+ it "delegates to a stub payload builder" do
54
+ expect(stub_builder).to receive(:trigger).with(trigger_builder)
55
+
56
+ builder.trigger(trigger_builder)
57
+ end
40
58
 
41
- builder.and_add_stub(trigger_builder)
42
59
  end
43
60
 
44
- end
61
+ context "when many triggered stubs are provided" do
45
62
 
46
- describe "#and_add_stubs" do
63
+ let(:trigger_builders) { (1..3).map { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) } }
47
64
 
48
- let(:trigger_builders) { (1..3).map { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) } }
65
+ it "delegates to a stub payload builder" do
66
+ expect(stub_builder).to receive(:trigger).with(trigger_builders)
49
67
 
50
- it "delegates to a stub payload builder" do
51
- expect(stub_builder).to receive(:and_add_stubs).with(trigger_builders)
68
+ builder.trigger(trigger_builders)
69
+ end
52
70
 
53
- builder.and_add_stubs(trigger_builders)
54
71
  end
55
72
 
56
73
  end
@@ -1,19 +1,21 @@
1
1
  describe HttpStub::Configurer::Request::StubPayloadBuilder do
2
2
 
3
- let(:builder) { HttpStub::Configurer::Request::StubPayloadBuilder.new }
3
+ let(:response_defaults) { {} }
4
4
 
5
- shared_context "add stub trigger" do
5
+ let(:builder) { HttpStub::Configurer::Request::StubPayloadBuilder.new(response_defaults) }
6
+
7
+ shared_context "triggers one stub" do
6
8
 
7
9
  let(:trigger_payload) { { "trigger_key" => "trigger value" } }
8
10
  let(:trigger_builder) do
9
11
  instance_double(HttpStub::Configurer::Request::StubPayloadBuilder, build: trigger_payload)
10
12
  end
11
13
 
12
- before(:example) { builder.and_add_stub(trigger_builder) }
14
+ before(:example) { builder.trigger(trigger_builder) }
13
15
 
14
16
  end
15
17
 
16
- shared_context "add stub triggers" do
18
+ shared_context "triggers many stubs" do
17
19
 
18
20
  let(:trigger_payloads) { (1..3).map { |i| { "trigger_#{i}_key" => "trigger #{i} value" } } }
19
21
  let(:trigger_builders) do
@@ -22,7 +24,19 @@ describe HttpStub::Configurer::Request::StubPayloadBuilder do
22
24
  end
23
25
  end
24
26
 
25
- before(:example) { builder.and_add_stubs(trigger_builders) }
27
+ before(:example) { builder.trigger(trigger_builders) }
28
+
29
+ end
30
+
31
+ describe "#respond_with" do
32
+
33
+ it "does not modify any provided response defaults" do
34
+ original_response_defaults = response_defaults.clone
35
+
36
+ builder.respond_with(status: 201)
37
+
38
+ expect(response_defaults).to eql(original_response_defaults)
39
+ end
26
40
 
27
41
  end
28
42
 
@@ -85,9 +99,9 @@ describe HttpStub::Configurer::Request::StubPayloadBuilder do
85
99
 
86
100
  end
87
101
 
88
- context "when stub triggers are added" do
102
+ context "when many triggers are added" do
89
103
 
90
- include_context "add stub triggers"
104
+ include_context "triggers many stubs"
91
105
 
92
106
  it "builds the payload for each trigger" do
93
107
  trigger_builders.each { |trigger_builder| expect(trigger_builder).to receive(:build) }
@@ -187,9 +201,9 @@ describe HttpStub::Configurer::Request::StubPayloadBuilder do
187
201
 
188
202
  end
189
203
 
190
- context "when a stub trigger is added" do
204
+ context "when a trigger is added" do
191
205
 
192
- include_context "add stub trigger"
206
+ include_context "triggers one stub"
193
207
 
194
208
  it "has a triggers entry containing the stub trigger payload" do
195
209
  expect(subject).to include(triggers: [ trigger_payload ])
@@ -197,9 +211,9 @@ describe HttpStub::Configurer::Request::StubPayloadBuilder do
197
211
 
198
212
  end
199
213
 
200
- context "when stub triggers are added" do
214
+ context "when many triggers are added" do
201
215
 
202
- include_context "add stub triggers"
216
+ include_context "triggers many stubs"
203
217
 
204
218
  it "has a triggers entry containing the stub trigger payloads" do
205
219
  expect(subject).to include(triggers: trigger_payloads)
@@ -207,7 +221,7 @@ describe HttpStub::Configurer::Request::StubPayloadBuilder do
207
221
 
208
222
  end
209
223
 
210
- context "when no stub triggers are added" do
224
+ context "when no triggers are added" do
211
225
 
212
226
  it "has a triggers entry containing an empty hash" do
213
227
  expect(subject).to include(triggers: [])
@@ -215,6 +229,72 @@ describe HttpStub::Configurer::Request::StubPayloadBuilder do
215
229
 
216
230
  end
217
231
 
232
+ context "when a root level response attribute is defaulted" do
233
+
234
+ let(:response_defaults) { { status: 204 } }
235
+
236
+ context "and is not overridden" do
237
+
238
+ let(:response_options) { {} }
239
+
240
+ it "assumes the defaulted value" do
241
+ expect(subject[:response]).to include(status: 204)
242
+ end
243
+
244
+ end
245
+
246
+ context "and is overridden" do
247
+
248
+ let(:response_status) { 302 }
249
+
250
+ it "assumes the override value" do
251
+ expect(subject[:response]).to include(status: 302)
252
+ end
253
+
254
+ end
255
+
256
+ end
257
+
258
+ context "when a nested response attribute is defaulted" do
259
+
260
+ let(:response_defaults) { { headers: { response_header_name: "default value" } } }
261
+
262
+ context "and the attribute has no other values defined" do
263
+
264
+ let(:response_headers) { {} }
265
+
266
+ it "assumes the defaults attributes" do
267
+ expect(subject[:response]).to include(response_defaults)
268
+ end
269
+
270
+ end
271
+
272
+ context "and the attribute has additional values defined" do
273
+
274
+ let(:response_headers) { { additional_header_name: "additional value" } }
275
+
276
+ it "includes the defaults values" do
277
+ expect(subject[:response][:headers]).to include(response_defaults[:headers])
278
+ end
279
+
280
+ it "includes the additional values" do
281
+ expect(subject[:response][:headers]).to include(response_headers)
282
+ end
283
+
284
+ end
285
+
286
+ context "and the attributes default values have been overridden" do
287
+
288
+ let(:response_headers) { { response_header_name: "override value"} }
289
+
290
+ it "assumes the defaults attributes" do
291
+ expect(subject[:response]).to include(headers: response_headers)
292
+ end
293
+
294
+ end
295
+
296
+ end
297
+
218
298
  end
219
299
 
220
300
  end
@@ -22,10 +22,30 @@ describe HttpStub::Configurer::Server::DSL do
22
22
  allow(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).and_return(stub_payload_builder)
23
23
  end
24
24
 
25
- it "creates a stub payload builder" do
26
- expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new)
25
+ subject { dsl.build_stub }
26
+
27
+ context "when response defaults have been established" do
28
+
29
+ let(:response_defaults) { { key: "value" } }
30
+
31
+ before(:example) { dsl.response_defaults = { key: "value" } }
32
+
33
+ it "creates a stub payload builder containing the response defaults" do
34
+ expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).with(response_defaults)
35
+
36
+ subject
37
+ end
38
+
39
+ end
40
+
41
+ context "when no response defaults have been established" do
42
+
43
+ it "creates a stub payload builder with empty response defaults" do
44
+ expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).with({})
45
+
46
+ subject
47
+ end
27
48
 
28
- dsl.build_stub
29
49
  end
30
50
 
31
51
  context "when a block is provided" do
@@ -199,6 +219,14 @@ describe HttpStub::Configurer::Server::DSL do
199
219
 
200
220
  subject { dsl.add_activator!(&block) }
201
221
 
222
+ context "when response defaults have been established" do
223
+
224
+ end
225
+
226
+ context "when no response defaults have been established" do
227
+
228
+ end
229
+
202
230
  it "creates a stub activator payload builder" do
203
231
  expect(HttpStub::Configurer::Request::StubActivatorPayloadBuilder).to receive(:new)
204
232
 
@@ -497,7 +497,7 @@ describe HttpStub::Configurer, "when the server is running" do
497
497
 
498
498
  context "that contains triggers" do
499
499
 
500
- let(:triggers) do
500
+ let(:triggered_stubs) do
501
501
  (1..3).map do |trigger_number|
502
502
  stub_server.build_stub do |stub|
503
503
  stub.match_requests("/triggered_stub_#{trigger_number}", method: :get)
@@ -510,7 +510,7 @@ describe HttpStub::Configurer, "when the server is running" do
510
510
  stub_server.add_stub! do |stub|
511
511
  stub.match_requests("/stub_with_triggers", method: :get)
512
512
  stub.respond_with(body: "Trigger stub body")
513
- stub.and_add_stubs(triggers)
513
+ stub.trigger(triggered_stubs)
514
514
  end
515
515
  end
516
516
 
@@ -559,6 +559,18 @@ describe HttpStub::Configurer, "when the server is running" do
559
559
 
560
560
  end
561
561
 
562
+ context "and the configurer declares response defaults" do
563
+
564
+ let(:configurer) { HttpStub::Examples::ConfigurerWithResponseDefaults.new }
565
+
566
+ it "includes the defaults in each response" do
567
+ response = Net::HTTP.get_response(server_host, "/response_with_defaults", server_port)
568
+
569
+ expect(response["defaulted_header"]).to eql("Header value")
570
+ end
571
+
572
+ end
573
+
562
574
  end
563
575
 
564
576
  context "and the configurer is uninitialized" do
data/spec/spec_helper.rb CHANGED
@@ -2,8 +2,11 @@ require 'bundler'
2
2
  Bundler.require(:development)
3
3
 
4
4
  SimpleCov.start do
5
+ coverage_dir "tmp/coverage"
6
+
5
7
  add_filter "/spec/"
6
8
  add_filter "/vendor/"
9
+
7
10
  minimum_coverage 99.41
8
11
  refuse_coverage_drop
9
12
  end if ENV["coverage"]
@@ -13,10 +16,11 @@ require 'http_server_manager/test_support'
13
16
  require_relative '../lib/http_stub/rake/task_generators'
14
17
  require_relative '../lib/http_stub'
15
18
  require_relative '../examples/configurer_with_class_activator'
19
+ require_relative '../examples/configurer_with_many_class_activators'
16
20
  require_relative '../examples/configurer_with_class_stub'
17
21
  require_relative '../examples/configurer_with_initialize_callback'
18
22
  require_relative '../examples/configurer_with_complex_initializer'
19
- require_relative '../examples/configurer_with_many_class_activators'
23
+ require_relative '../examples/configurer_with_response_defaults'
20
24
 
21
25
  HttpStub::ServerDaemon.log_dir = File.expand_path('../../tmp/log', __FILE__)
22
26
  HttpStub::ServerDaemon.pid_dir = File.expand_path('../../tmp/pids', __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.14.0.rc3
4
+ version: 0.14.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Ueckerman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-22 00:00:00.000000000 Z
12
+ date: 2015-01-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -17,14 +17,14 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '10.3'
20
+ version: '10.4'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
25
  - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '10.3'
27
+ version: '10.4'
28
28
  - !ruby/object:Gem::Dependency
29
29
  name: sinatra
30
30
  requirement: !ruby/object:Gem::Requirement
@@ -101,14 +101,14 @@ dependencies:
101
101
  requirements:
102
102
  - - "~>"
103
103
  - !ruby/object:Gem::Version
104
- version: '4.0'
104
+ version: '4.2'
105
105
  type: :runtime
106
106
  prerelease: false
107
107
  version_requirements: !ruby/object:Gem::Requirement
108
108
  requirements:
109
109
  - - "~>"
110
110
  - !ruby/object:Gem::Version
111
- version: '4.0'
111
+ version: '4.2'
112
112
  - !ruby/object:Gem::Dependency
113
113
  name: travis-lint
114
114
  requirement: !ruby/object:Gem::Requirement
@@ -235,7 +235,7 @@ dependencies:
235
235
  - - "~>"
236
236
  - !ruby/object:Gem::Version
237
237
  version: '1.8'
238
- description: fakeweb for a HTTP server, informing it to stub / fake responses.
238
+ description: fakeweb for a HTTP server, informing it to stub / fake responses
239
239
  email: matthew.ueckerman@myob.com
240
240
  executables: []
241
241
  extensions: []
@@ -359,7 +359,7 @@ rubyforge_project: http_stub
359
359
  rubygems_version: 2.2.2
360
360
  signing_key:
361
361
  specification_version: 4
362
- summary: A HTTP Server replaying configured stub responses.
362
+ summary: A HTTP Server replaying configured stub responses
363
363
  test_files:
364
364
  - "./spec/curl_samples.txt"
365
365
  - "./spec/lib/http_stub/configurer/deprecated_dsl_spec.rb"