http_stub 0.18.2 → 0.19.0

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: 9b3e67c607aa3b80f5e886bd7de7fbbfd3749f72
4
- data.tar.gz: f0f313f13ccd2d1e0b2eaf62292e2c293b5b2592
3
+ metadata.gz: cd84700c863cf0c87b4be11b874723b35475c6fb
4
+ data.tar.gz: 2857f2d1bd0c3ab6b1bf4ca0a01753bf69b20234
5
5
  SHA512:
6
- metadata.gz: e3f87569921e42a1272453d37ef65a5585c148ec081a943bc128af5cbff417e59440beedee64b21dc08af0f1c71b3a14f428fcc215903c92104b1ba40787abec
7
- data.tar.gz: fb62118fd274913421bda1ef15a2776871a9af76dd2ad96f07b29b1a20417bc2ab60d102bd65ba1faaaefd91aeb7d21687a35dcb0c0a8587245d92eea760a91d
6
+ metadata.gz: f37b401cd641a3b8874f8cd40a4074f2db3ce886d8bedba62b533a7ef2e7dbc999a796cf3e9c86bdcf9f857cbd169276a0464ee40205984db01405aca531e499
7
+ data.tar.gz: 792b251f35f1fe21026106d931ee053162840b569aa7ffc3fa3c52a89921c9fc3c7dab7e01cf30563fbac3e8baa670833dcf4638b1aeef3d0e27e0625b55654b
data/lib/http_stub.rb CHANGED
@@ -11,6 +11,7 @@ require 'sass'
11
11
  require 'active_support/core_ext/module/aliasing'
12
12
  require 'active_support/core_ext/module/delegation'
13
13
  require 'active_support/core_ext/object/blank'
14
+ require 'active_support/core_ext/object/try'
14
15
  require 'active_support/core_ext/string/inflections'
15
16
  require 'active_support/core_ext/hash/slice'
16
17
  require 'active_support/core_ext/hash/deep_merge'
@@ -47,7 +47,7 @@ module HttpStub
47
47
  private
48
48
 
49
49
  def add_stub_options_to_builder(stub, uri, options)
50
- stub.match_requests(uri, options.slice(:method, :headers, :parameters))
50
+ stub.match_requests({ uri: uri }.merge(options.slice(:method, :headers, :parameters)))
51
51
  stub.respond_with(options[:response])
52
52
  end
53
53
 
@@ -20,7 +20,7 @@ module HttpStub
20
20
  end
21
21
 
22
22
  def add_scenario!(name, response_overrides={}, &block)
23
- @server.add_one_stub_scenario!(name) { |stub| compose_stub(stub, response_overrides, &block) }
23
+ @server.add_scenario_with_one_stub!(name) { |stub| compose_stub(stub, response_overrides, &block) }
24
24
  end
25
25
 
26
26
  private
@@ -6,8 +6,8 @@ module HttpStub
6
6
  include HttpStub::Configurer::DSL::StubBuilderProducer
7
7
  include HttpStub::Configurer::DSL::ScenarioActivator
8
8
 
9
- def initialize(response_defaults, name)
10
- @response_defaults = response_defaults
9
+ def initialize(default_stub_builder, name)
10
+ @default_stub_builder = default_stub_builder
11
11
  @name = name
12
12
  @stub_builders = []
13
13
  @triggered_scenario_names = []
@@ -9,16 +9,20 @@ module HttpStub
9
9
  attr_accessor :host, :port
10
10
 
11
11
  def initialize(server_facade)
12
- @server_facade = server_facade
13
- @response_defaults = {}
12
+ @server_facade = server_facade
13
+ @default_stub_builder = HttpStub::Configurer::DSL::StubBuilder.new
14
14
  end
15
15
 
16
16
  def base_uri
17
17
  "http://#{host}:#{port}"
18
18
  end
19
19
 
20
+ def request_defaults=(args)
21
+ @default_stub_builder.match_requests(args)
22
+ end
23
+
20
24
  def response_defaults=(args)
21
- @response_defaults = args
25
+ @default_stub_builder.respond_with(args)
22
26
  end
23
27
 
24
28
  def has_started!
@@ -31,12 +35,12 @@ module HttpStub
31
35
  end
32
36
 
33
37
  def add_scenario!(name, &block)
34
- builder = HttpStub::Configurer::DSL::ScenarioBuilder.new(@response_defaults, name)
38
+ builder = HttpStub::Configurer::DSL::ScenarioBuilder.new(@default_stub_builder, name)
35
39
  block.call(builder)
36
40
  @server_facade.define_scenario(builder.build)
37
41
  end
38
42
 
39
- def add_one_stub_scenario!(name, &block)
43
+ def add_scenario_with_one_stub!(name, &block)
40
44
  add_scenario!(name) do |scenario|
41
45
  scenario.add_stub! { |stub| stub.invoke(&block) }
42
46
  end
@@ -47,7 +51,7 @@ module HttpStub
47
51
  end
48
52
 
49
53
  def add_activator!(&block)
50
- builder = HttpStub::Configurer::DSL::StubActivatorBuilder.new(@response_defaults)
54
+ builder = HttpStub::Configurer::DSL::StubActivatorBuilder.new(@default_stub_builder)
51
55
  block.call(builder)
52
56
  @server_facade.define_scenario(builder.build)
53
57
  end
@@ -7,13 +7,13 @@ module HttpStub
7
7
  delegate :build, to: :@scenario_builder
8
8
  delegate :match_requests, :respond_with, :trigger, to: :@stub_builder
9
9
 
10
- def initialize(response_defaults)
11
- @response_defaults = response_defaults
10
+ def initialize(default_stub_builder)
11
+ @default_stub_builder = default_stub_builder
12
12
  end
13
13
 
14
14
  def on(activation_uri)
15
- @scenario_builder = HttpStub::Configurer::DSL::ScenarioBuilder.new(@response_defaults, activation_uri)
16
- @stub_builder = HttpStub::Configurer::DSL::StubBuilder.new(@response_defaults)
15
+ @scenario_builder = HttpStub::Configurer::DSL::ScenarioBuilder.new(@default_stub_builder, activation_uri)
16
+ @stub_builder = HttpStub::Configurer::DSL::StubBuilder.new(@default_stub_builder)
17
17
  @scenario_builder.add_stub!(@stub_builder)
18
18
  end
19
19
 
@@ -6,15 +6,15 @@ module HttpStub
6
6
 
7
7
  attr_reader :request, :response, :triggers
8
8
 
9
- public
10
-
11
- def initialize(response_defaults={})
12
- @response = response_defaults.clone
9
+ def initialize(default_builder=nil)
10
+ @request = {}
11
+ @response = {}
13
12
  @triggers = []
13
+ self.merge!(default_builder) if default_builder
14
14
  end
15
15
 
16
- def match_requests(uri, args={})
17
- self.tap { @request = { uri: uri }.merge(args) }
16
+ def match_requests(args)
17
+ self.tap { @request.deep_merge!(args) }
18
18
  end
19
19
 
20
20
  def schema(type, definition)
@@ -37,7 +37,7 @@ module HttpStub
37
37
  end
38
38
 
39
39
  def merge!(stub_builder)
40
- @request = (@request || {}).deep_merge(stub_builder.request || {})
40
+ self.match_requests(stub_builder.request)
41
41
  self.respond_with(stub_builder.response)
42
42
  self.trigger(stub_builder.triggers)
43
43
  end
@@ -5,7 +5,7 @@ module HttpStub
5
5
  module StubBuilderProducer
6
6
 
7
7
  def build_stub(&block)
8
- builder = HttpStub::Configurer::DSL::StubBuilder.new(@response_defaults)
8
+ builder = HttpStub::Configurer::DSL::StubBuilder.new(@default_stub_builder)
9
9
  builder.invoke(&block) if block_given?
10
10
  builder
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module HttpStub
2
- VERSION = "0.18.2".freeze
2
+ VERSION = "0.19.0".freeze
3
3
  end
@@ -0,0 +1,44 @@
1
+ describe "Server defaults acceptance" do
2
+ include_context "configurer integration"
3
+
4
+ context "when a configurer defines server defaults" do
5
+
6
+ let(:configurer) { HttpStub::Examples::ConfigurerWithServerDefaults.new }
7
+
8
+ before(:example) { configurer.class.initialize! }
9
+
10
+ it "matches requests that match the request default rules" do
11
+ response = issue_matching_request("has_defaults")
12
+
13
+ expect(response.code).to eql(203)
14
+ end
15
+
16
+ it "does not match requests that do not match the request default rules" do
17
+ response = HTTParty.get("#{server_uri}/has_defaults")
18
+
19
+ expect(response.code).to eql(404)
20
+ end
21
+
22
+ it "includes the response defaults in a match" do
23
+ response = issue_matching_request("has_defaults")
24
+
25
+ expect(response.code).to eql(203)
26
+ expect(response.body).to eql("Some body")
27
+ expect(response.headers["defaulted_response_header"]).to eql("Response header value")
28
+ end
29
+
30
+ it "includes the response defaults in all matches" do
31
+ response = issue_matching_request("also_has_defaults")
32
+
33
+ expect(response.code).to eql(203)
34
+ expect(response.body).to eql("Also some body")
35
+ expect(response.headers["defaulted_response_header"]).to eql("Response header value")
36
+ end
37
+
38
+ def issue_matching_request(uri)
39
+ HTTParty.get("#{server_uri}/#{uri}", headers: { "defaulted_request_header" => "Request header value"})
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -13,7 +13,7 @@ describe "Stub basics acceptance" do
13
13
 
14
14
  before(:example) do
15
15
  stub_server.add_stub! do
16
- match_requests("/stub_with_status", method: :get).respond_with(status: 201, body: "Stub body")
16
+ match_requests(uri: "/stub_with_status", method: :get).respond_with(status: 201, body: "Stub body")
17
17
  end
18
18
  end
19
19
 
@@ -52,7 +52,7 @@ describe "Stub basics acceptance" do
52
52
  context "and it does not contain a response status" do
53
53
 
54
54
  before(:example) do
55
- stub_server.add_stub! { match_requests("/stub_without_status", method: :get).respond_with(body: "Stub body") }
55
+ stub_server.add_stub! { match_requests(uri: "/stub_without_status", method: :get).respond_with(body: "Stub body") }
56
56
  end
57
57
 
58
58
  context "and a matching request is made" do
@@ -70,7 +70,7 @@ describe "Stub basics acceptance" do
70
70
  context "and it does not contain a request method" do
71
71
 
72
72
  before(:example) do
73
- stub_server.add_stub! { match_requests("/stub_without_method").respond_with(body: "Stub body") }
73
+ stub_server.add_stub! { match_requests(uri: "/stub_without_method").respond_with(body: "Stub body") }
74
74
  end
75
75
 
76
76
  context "and a request is made with a matching uri" do
@@ -93,7 +93,7 @@ describe "Stub basics acceptance" do
93
93
 
94
94
  before(:example) do
95
95
  stub_server.add_stub! do
96
- match_requests("/stub_with_headers", method: :get, headers: { key: "value" })
96
+ match_requests(uri: "/stub_with_headers", method: :get, headers: { key: "value" })
97
97
  respond_with(status: 202, body: "Another stub body")
98
98
  end
99
99
  end
@@ -129,7 +129,7 @@ describe "Stub basics acceptance" do
129
129
 
130
130
  before(:example) do
131
131
  stub_server.add_stub! do
132
- match_requests("/stub_with_parameters", method: :get, parameters: { key: "value" })
132
+ match_requests(uri: "/stub_with_parameters", method: :get, parameters: { key: "value" })
133
133
  respond_with(status: 202, body: "Another stub body")
134
134
  end
135
135
  end
@@ -161,7 +161,7 @@ describe "Stub basics acceptance" do
161
161
 
162
162
  before(:example) do
163
163
  stub_server.add_stub! do
164
- match_requests("/stub_with_parameters", method: :get, parameters: { key: 88 })
164
+ match_requests(uri: "/stub_with_parameters", method: :get, parameters: { key: 88 })
165
165
  respond_with(status: 203, body: "Body for parameter number")
166
166
  end
167
167
  end
@@ -181,25 +181,13 @@ describe "Stub basics acceptance" do
181
181
 
182
182
  end
183
183
 
184
- context "that contains response defaults" do
185
-
186
- let(:configurer) { HttpStub::Examples::ConfigurerWithResponseDefaults.new }
187
-
188
- it "includes the defaults in each response" do
189
- response = Net::HTTP.get_response(server_host, "/response_with_defaults", server_port)
190
-
191
- expect(response["defaulted_header"]).to eql("Header value")
192
- end
193
-
194
- end
195
-
196
184
  describe "that contains response headers" do
197
185
 
198
186
  context "with a content-type header" do
199
187
 
200
188
  before(:example) do
201
189
  stub_server.add_stub! do
202
- match_requests("/some_stub_path", method: :get)
190
+ match_requests(uri: "/some_stub_path", method: :get)
203
191
  respond_with(body: "Some stub body", headers: { "content-type" => "application/xhtml" })
204
192
  end
205
193
  end
@@ -226,7 +214,7 @@ describe "Stub basics acceptance" do
226
214
 
227
215
  before(:example) do
228
216
  stub_server.add_stub! do |stub|
229
- stub.match_requests("/some_stub_path", method: :get)
217
+ stub.match_requests(uri: "/some_stub_path", method: :get)
230
218
  stub.respond_with(body: "Some stub body", headers: response_headers)
231
219
  end
232
220
  end
@@ -12,7 +12,7 @@ describe "Stub trigger acceptance" do
12
12
  let(:triggered_stubs) do
13
13
  (1..3).map do |trigger_number|
14
14
  stub_server.build_stub do |stub|
15
- stub.match_requests("/triggered_stub_#{trigger_number}", method: :get)
15
+ stub.match_requests(uri: "/triggered_stub_#{trigger_number}", method: :get)
16
16
  stub.respond_with(status: 200 + trigger_number, body: "Triggered stub body #{trigger_number}")
17
17
  end
18
18
  end
@@ -20,7 +20,7 @@ describe "Stub trigger acceptance" do
20
20
 
21
21
  before(:example) do
22
22
  stub_server.add_stub! do |stub|
23
- stub.match_requests("/stub_with_triggers", method: :get)
23
+ stub.match_requests(uri: "/stub_with_triggers", method: :get)
24
24
  stub.respond_with(body: "Trigger stub body")
25
25
  stub.trigger(triggered_stubs)
26
26
  end
@@ -59,7 +59,7 @@ describe "Stub trigger acceptance" do
59
59
  let(:pdf_file_path) { "#{HttpStub::Spec::RESOURCES_DIR}/sample.pdf" }
60
60
  let(:pdf_file_trigger) do
61
61
  stub_server.build_stub do |stub|
62
- stub.match_requests("/triggered_stub_pdf_file", method: :get)
62
+ stub.match_requests(uri: "/triggered_stub_pdf_file", method: :get)
63
63
  stub.respond_with(
64
64
  status: 201,
65
65
  headers: { "content-type" => "application/pdf" },
@@ -71,7 +71,7 @@ describe "Stub trigger acceptance" do
71
71
  let(:text_body) { "Sample trigger stub body" }
72
72
  let(:text_trigger) do
73
73
  stub_server.build_stub do |stub|
74
- stub.match_requests("/triggered_stub_text", method: :get)
74
+ stub.match_requests(uri: "/triggered_stub_text", method: :get)
75
75
  stub.respond_with(status: 202, body: "Sample trigger stub body")
76
76
  end
77
77
  end
@@ -79,7 +79,7 @@ describe "Stub trigger acceptance" do
79
79
  let(:txt_file_path) { "#{HttpStub::Spec::RESOURCES_DIR}/sample.txt" }
80
80
  let(:txt_file_trigger) do
81
81
  stub_server.build_stub do |stub|
82
- stub.match_requests("/triggered_stub_txt_file", method: :get)
82
+ stub.match_requests(uri: "/triggered_stub_txt_file", method: :get)
83
83
  stub.respond_with(
84
84
  status: 203,
85
85
  headers: { "content-type" => "text/plain" },
@@ -92,7 +92,7 @@ describe "Stub trigger acceptance" do
92
92
 
93
93
  before(:example) do
94
94
  stub_server.add_stub! do |stub|
95
- stub.match_requests("/stub_with_triggers", method: :get)
95
+ stub.match_requests(uri: "/stub_with_triggers", method: :get)
96
96
  stub.respond_with(body: "Trigger stub body")
97
97
  stub.trigger(triggered_stubs)
98
98
  end
@@ -17,7 +17,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
17
17
  end
18
18
 
19
19
  it "causes the builder being added to match requests on the provided uri" do
20
- expect(builder).to receive(:match_requests).with(stub_uri, anything)
20
+ expect(builder).to receive(:match_requests).with(hash_including(uri: stub_uri))
21
21
 
22
22
  subject
23
23
  end
@@ -25,7 +25,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
25
25
  context "when a method is provided" do
26
26
 
27
27
  it "causes the builder being added to match requests on the provided method" do
28
- expect(builder).to receive(:match_requests).with(anything, hash_including(method: method))
28
+ expect(builder).to receive(:match_requests).with(hash_including(method: method))
29
29
 
30
30
  subject
31
31
  end
@@ -37,7 +37,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
37
37
  let(:options) { {} }
38
38
 
39
39
  it "causes the builder being added to not match requests based on method" do
40
- expect(builder).to receive(:match_requests).with(anything, hash_excluding(:method))
40
+ expect(builder).to receive(:match_requests).with(hash_excluding(:method))
41
41
 
42
42
  subject
43
43
  end
@@ -47,7 +47,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
47
47
  context "when parameters are provided" do
48
48
 
49
49
  it "causes the builder being added to match requests on the provided parameters" do
50
- expect(builder).to receive(:match_requests).with(anything, hash_including(parameters: parameters))
50
+ expect(builder).to receive(:match_requests).with(hash_including(parameters: parameters))
51
51
 
52
52
  subject
53
53
  end
@@ -59,7 +59,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
59
59
  let(:options) { {} }
60
60
 
61
61
  it "causes the builder being added to not match requests based on parameters" do
62
- expect(builder).to receive(:match_requests).with(anything, hash_excluding(:parameters))
62
+ expect(builder).to receive(:match_requests).with(hash_excluding(:parameters))
63
63
 
64
64
  subject
65
65
  end
@@ -69,7 +69,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
69
69
  context "when headers are provided" do
70
70
 
71
71
  it "causes the builder being added to match requests on the provided headers" do
72
- expect(builder).to receive(:match_requests).with(anything, hash_including(headers: headers))
72
+ expect(builder).to receive(:match_requests).with(hash_including(headers: headers))
73
73
 
74
74
  subject
75
75
  end
@@ -81,7 +81,7 @@ describe HttpStub::Configurer::DSL::Deprecated do
81
81
  let(:options) { {} }
82
82
 
83
83
  it "causes the builder being added to not match requests based on headers" do
84
- expect(builder).to receive(:match_requests).with(anything, hash_excluding(:headers))
84
+ expect(builder).to receive(:match_requests).with(hash_excluding(:headers))
85
85
 
86
86
  subject
87
87
  end
@@ -18,13 +18,12 @@ describe HttpStub::Configurer::DSL::EndpointTemplate do
18
18
 
19
19
  describe "#match_requests" do
20
20
 
21
- let(:uri) { "/some/uri" }
22
21
  let(:args) { { key: :value } }
23
22
 
24
- subject { endpoint_template.match_requests(uri, args) }
23
+ subject { endpoint_template.match_requests(args) }
25
24
 
26
25
  it "delegates to the templates stub builder" do
27
- expect(template_stub_builder).to receive(:match_requests).with(uri, args)
26
+ expect(template_stub_builder).to receive(:match_requests).with(args)
28
27
 
29
28
  subject
30
29
  end
@@ -229,7 +228,7 @@ describe HttpStub::Configurer::DSL::EndpointTemplate do
229
228
  let(:name) { "some_scenario_name" }
230
229
  let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder).as_null_object }
231
230
 
232
- before(:example) { allow(server).to receive(:add_one_stub_scenario!).and_yield(stub_builder) }
231
+ before(:example) { allow(server).to receive(:add_scenario_with_one_stub!).and_yield(stub_builder) }
233
232
 
234
233
  def subject_without_overrides_and_block
235
234
  endpoint_template.add_scenario!(name)
@@ -244,7 +243,7 @@ describe HttpStub::Configurer::DSL::EndpointTemplate do
244
243
  end
245
244
 
246
245
  it "adds a one stub scenario to the server" do
247
- expect(server).to receive(:add_one_stub_scenario!).with(name)
246
+ expect(server).to receive(:add_scenario_with_one_stub!).with(name)
248
247
 
249
248
  subject_without_overrides_and_block
250
249
  end
@@ -1,21 +1,22 @@
1
1
  describe HttpStub::Configurer::DSL::Server do
2
2
 
3
- let(:server_facade) { instance_double(HttpStub::Configurer::Server::Facade) }
3
+ let(:server_facade) { instance_double(HttpStub::Configurer::Server::Facade) }
4
+ let(:default_stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
4
5
 
5
6
  let(:server) { HttpStub::Configurer::DSL::Server.new(server_facade) }
6
7
 
7
- shared_context "establish response defaults" do
8
-
9
- let(:response_defaults) { { key: "value" } }
10
-
11
- before(:example) { server.response_defaults = { key: "value" } }
12
-
8
+ before(:example) do
9
+ allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).with(no_args).and_return(default_stub_builder)
13
10
  end
14
11
 
15
12
  it "produces stub builders" do
16
13
  expect(server).to be_a(HttpStub::Configurer::DSL::StubBuilderProducer)
17
14
  end
18
15
 
16
+ it "activates scenarios" do
17
+ expect(server).to be_a(HttpStub::Configurer::DSL::ScenarioActivator)
18
+ end
19
+
19
20
  describe "#base_uri" do
20
21
 
21
22
  subject { server.base_uri }
@@ -35,6 +36,30 @@ describe HttpStub::Configurer::DSL::Server do
35
36
 
36
37
  end
37
38
 
39
+ describe "#request_defaults=" do
40
+
41
+ let(:args) { { request_rule_key: "request rule value" } }
42
+
43
+ it "establishes request matcher rules on the default stub builder" do
44
+ expect(default_stub_builder).to receive(:match_requests).with(args)
45
+
46
+ server.request_defaults = args
47
+ end
48
+
49
+ end
50
+
51
+ describe "#response_defaults=" do
52
+
53
+ let(:args) { { response_key: "response value" } }
54
+
55
+ it "establishes response values on the default stub builder" do
56
+ expect(default_stub_builder).to receive(:respond_with).with(args)
57
+
58
+ server.response_defaults = args
59
+ end
60
+
61
+ end
62
+
38
63
  describe "#has_started!" do
39
64
 
40
65
  it "informs the facade that the server has started" do
@@ -123,32 +148,16 @@ describe HttpStub::Configurer::DSL::Server do
123
148
 
124
149
  subject { server.add_scenario!(scenario_name, &block) }
125
150
 
126
- context "when response defaults have been established" do
127
-
128
- include_context "establish response defaults"
129
-
130
- it "creates a scenario builder containing the response defaults" do
131
- expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(response_defaults, anything)
132
-
133
- subject
134
- end
135
-
136
- it "creates a scenario builder containing the provided scenario name" do
137
- expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(anything, scenario_name)
138
-
139
- subject
140
- end
151
+ it "creates a scenario builder containing the servers default stub builder" do
152
+ expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(default_stub_builder, anything)
141
153
 
154
+ subject
142
155
  end
143
156
 
144
- context "when no response defaults have been established" do
145
-
146
- it "creates a scenario builder with empty response defaults" do
147
- expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with({}, anything)
148
-
149
- subject
150
- end
157
+ it "creates a scenario builder containing the provided scenario name" do
158
+ expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(anything, scenario_name)
151
159
 
160
+ subject
152
161
  end
153
162
 
154
163
  it "yields the created builder to the provided block" do
@@ -171,7 +180,7 @@ describe HttpStub::Configurer::DSL::Server do
171
180
 
172
181
  end
173
182
 
174
- describe "#add_one_stub_scenario!" do
183
+ describe "#add_scenario_with_one_stub!" do
175
184
 
176
185
  let(:scenario_name) { "some_scenario_name" }
177
186
  let(:block_verifier) { double("BlockVerifier") }
@@ -180,7 +189,7 @@ describe HttpStub::Configurer::DSL::Server do
180
189
  let(:scenario_builder) { instance_double(HttpStub::Configurer::DSL::ScenarioBuilder) }
181
190
  let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder, invoke: nil) }
182
191
 
183
- subject { server.add_one_stub_scenario!(scenario_name, &block) }
192
+ subject { server.add_scenario_with_one_stub!(scenario_name, &block) }
184
193
 
185
194
  before(:each) do
186
195
  allow(server).to receive(:add_scenario!).and_yield(scenario_builder)
@@ -256,28 +265,10 @@ describe HttpStub::Configurer::DSL::Server do
256
265
 
257
266
  subject { server.add_activator!(&block) }
258
267
 
259
- context "when response defaults have been established" do
260
-
261
- let(:response_defaults) { { key: "value" } }
262
-
263
- before(:example) { server.response_defaults = { key: "value" } }
264
-
265
- it "creates a stub activator builder containing the response defaults" do
266
- expect(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).with(response_defaults)
267
-
268
- subject
269
- end
270
-
271
- end
272
-
273
- context "when no response defaults have been established" do
274
-
275
- it "creates a stub activator builder with empty response defaults" do
276
- expect(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).with({})
277
-
278
- subject
279
- end
268
+ it "creates a stub activator builder containing the servers default stub builder" do
269
+ expect(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).with(default_stub_builder)
280
270
 
271
+ subject
281
272
  end
282
273
 
283
274
  it "yields the created builder to the provided block" do
@@ -48,9 +48,9 @@ describe HttpStub::Configurer::DSL::StubActivatorBuilder do
48
48
  before(:example) { builder.on(activation_uri) }
49
49
 
50
50
  it "delegates to a stub builder" do
51
- expect(stub_builder).to receive(:match_requests).with(stub_fixture.request.uri, request_payload)
51
+ expect(stub_builder).to receive(:match_requests).with(request_payload)
52
52
 
53
- builder.match_requests(stub_fixture.request.uri, request_payload)
53
+ builder.match_requests(request_payload)
54
54
  end
55
55
 
56
56
  end
@@ -3,8 +3,8 @@ describe HttpStub::Configurer::DSL::StubBuilderProducer do
3
3
  class HttpStub::Configurer::DSL::TestableStubBuilderProducer
4
4
  include HttpStub::Configurer::DSL::StubBuilderProducer
5
5
 
6
- def initialize(response_defaults)
7
- @response_defaults = response_defaults
6
+ def initialize(default_stub_builder)
7
+ @default_stub_builder = default_stub_builder
8
8
  @builders = []
9
9
  end
10
10
 
@@ -14,9 +14,9 @@ describe HttpStub::Configurer::DSL::StubBuilderProducer do
14
14
 
15
15
  end
16
16
 
17
- let(:response_defaults) { { default_key: "default value" } }
17
+ let(:default_stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
18
18
 
19
- let(:producer) { HttpStub::Configurer::DSL::TestableStubBuilderProducer.new(response_defaults) }
19
+ let(:producer) { HttpStub::Configurer::DSL::TestableStubBuilderProducer.new(default_stub_builder) }
20
20
 
21
21
  describe "#build_stub" do
22
22
 
@@ -26,8 +26,8 @@ describe HttpStub::Configurer::DSL::StubBuilderProducer do
26
26
 
27
27
  subject { producer.build_stub }
28
28
 
29
- it "creates a stub builder containing the producers response defaults" do
30
- expect(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).with(response_defaults)
29
+ it "creates a stub builder containing the producers default stub builder" do
30
+ expect(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).with(default_stub_builder)
31
31
 
32
32
  subject
33
33
  end
@@ -1,8 +1,8 @@
1
1
  describe HttpStub::Configurer::DSL::StubBuilder do
2
2
 
3
- let(:response_defaults) { {} }
3
+ let(:default_builder) { nil }
4
4
 
5
- let(:builder) { HttpStub::Configurer::DSL::StubBuilder.new(response_defaults) }
5
+ let(:builder) { HttpStub::Configurer::DSL::StubBuilder.new(default_builder) }
6
6
 
7
7
  shared_context "triggers one stub" do
8
8
 
@@ -26,11 +26,56 @@ describe HttpStub::Configurer::DSL::StubBuilder do
26
26
 
27
27
  end
28
28
 
29
+ describe "constructor" do
30
+
31
+ class HttpStub::Configurer::DSL::StubBuilderWithObservedMerge < HttpStub::Configurer::DSL::StubBuilder
32
+
33
+ attr_reader :merged_stub_builders
34
+
35
+ def initialize(default_builder)
36
+ @merged_stub_builders = []
37
+ super(default_builder)
38
+ end
39
+
40
+ def merge!(stub_builder)
41
+ @merged_stub_builders << stub_builder
42
+ end
43
+
44
+ end
45
+
46
+ context "when a default builder is provided" do
47
+
48
+ let(:default_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
49
+
50
+ let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(default_builder) }
51
+
52
+ it "merges the default builder" do
53
+ builder
54
+
55
+ expect(builder.merged_stub_builders).to eql([ default_builder ])
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ context "when a default builder is not provided" do
63
+
64
+ let(:builder) { HttpStub::Configurer::DSL::StubBuilderWithObservedMerge.new(nil) }
65
+
66
+ it "does not merge a builder" do
67
+ builder
68
+
69
+ expect(builder.merged_stub_builders).to eql([])
70
+ end
71
+
72
+ end
73
+
29
74
  describe "#match_requests" do
30
75
 
31
76
  let(:fixture) { HttpStub::StubFixture.new }
32
77
 
33
- subject { builder.match_requests(fixture.request.uri, fixture.request.symbolized) }
78
+ subject { builder.match_requests(fixture.request.symbolized) }
34
79
 
35
80
  it "returns the builder to support method chaining" do
36
81
  expect(subject).to eql(builder)
@@ -60,14 +105,6 @@ describe HttpStub::Configurer::DSL::StubBuilder do
60
105
 
61
106
  subject { builder.respond_with(status: 201) }
62
107
 
63
- it "does not modify any provided response defaults" do
64
- original_response_defaults = response_defaults.clone
65
-
66
- subject
67
-
68
- expect(response_defaults).to eql(original_response_defaults)
69
- end
70
-
71
108
  it "returns the builder to support method chaining" do
72
109
  expect(subject).to eql(builder)
73
110
  end
@@ -88,10 +125,10 @@ describe HttpStub::Configurer::DSL::StubBuilder do
88
125
 
89
126
  context "when the block accepts an argument" do
90
127
 
91
- subject { builder.invoke { |builder| builder.match_requests("/some_uri") } }
128
+ subject { builder.invoke { |builder| builder.match_requests(uri: "/some_uri") } }
92
129
 
93
130
  it "invokes the block with the builder as the argument" do
94
- expect(builder).to receive(:match_requests).with("/some_uri")
131
+ expect(builder).to receive(:match_requests).with(uri: "/some_uri")
95
132
 
96
133
  subject
97
134
  end
@@ -100,10 +137,10 @@ describe HttpStub::Configurer::DSL::StubBuilder do
100
137
 
101
138
  context "when the block accepts no arguments" do
102
139
 
103
- subject { builder.invoke { match_requests("/some_uri") } }
140
+ subject { builder.invoke { match_requests(uri: "/some_uri") } }
104
141
 
105
142
  it "invokes the block in the context of the builder" do
106
- expect(builder).to receive(:match_requests).with("/some_uri")
143
+ expect(builder).to receive(:match_requests).with(uri: "/some_uri")
107
144
 
108
145
  subject
109
146
  end
@@ -121,8 +158,8 @@ describe HttpStub::Configurer::DSL::StubBuilder do
121
158
  let(:provided_triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
122
159
 
123
160
  let(:provided_builder) do
124
- HttpStub::Configurer::DSL::StubBuilder.new({}).tap do |builder|
125
- builder.match_requests("/replacement_uri", method: :put,
161
+ HttpStub::Configurer::DSL::StubBuilder.new.tap do |builder|
162
+ builder.match_requests(uri: "/replacement_uri", method: :put,
126
163
  headers: { request_header_key: "replacement request header value",
127
164
  other_request_header_key: "other request header value" },
128
165
  parameters: { parameter_key: "replacement parameter value",
@@ -143,7 +180,7 @@ describe HttpStub::Configurer::DSL::StubBuilder do
143
180
  let(:original_triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
144
181
 
145
182
  before(:example) do
146
- builder.match_requests("/original_uri", method: :get,
183
+ builder.match_requests(uri: "/original_uri", method: :get,
147
184
  headers: { request_header_key: "original request header value" },
148
185
  parameters: { parameter_key: "original parameter value" })
149
186
  builder.respond_with(status: 202,
@@ -220,7 +257,7 @@ describe HttpStub::Configurer::DSL::StubBuilder do
220
257
 
221
258
  context "and a builder that is empty is provided" do
222
259
 
223
- let(:provided_builder) { HttpStub::Configurer::DSL::StubBuilder.new({}) }
260
+ let(:provided_builder) { HttpStub::Configurer::DSL::StubBuilder.new }
224
261
 
225
262
  it "preserves the uri" do
226
263
  subject
@@ -349,12 +386,14 @@ describe HttpStub::Configurer::DSL::StubBuilder do
349
386
 
350
387
  describe "#build" do
351
388
 
352
- let(:fixture) { HttpStub::StubFixture.new }
353
- let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
389
+ let(:fixture) { HttpStub::StubFixture.new }
390
+ let(:triggers) { [] }
391
+ let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
354
392
 
355
393
  subject do
356
- builder.match_requests(fixture.request.uri, fixture.request.symbolized)
394
+ builder.match_requests(fixture.request.symbolized)
357
395
  builder.respond_with(fixture.response.symbolized)
396
+ builder.trigger(triggers)
358
397
 
359
398
  builder.build
360
399
  end
@@ -364,7 +403,7 @@ describe HttpStub::Configurer::DSL::StubBuilder do
364
403
  context "when provided a request match and response data" do
365
404
 
366
405
  it "creates a stub payload with request options that include the uri and the provided request options" do
367
- expect_stub_to_be_created_with(request: { uri: fixture.request.uri }.merge(fixture.request.symbolized))
406
+ expect_stub_to_be_created_with(request: fixture.request.symbolized)
368
407
 
369
408
  subject
370
409
  end
@@ -375,44 +414,118 @@ describe HttpStub::Configurer::DSL::StubBuilder do
375
414
  subject
376
415
  end
377
416
 
378
- context "when response default options are established" do
417
+ describe "creates a stub payload with triggers that" do
379
418
 
380
- let(:response_defaults) { { some_options: "default value" } }
419
+ context "when a trigger is added" do
381
420
 
382
- it "creates a stub payload with response arguments that includes the defaults" do
383
- expect_stub_to_be_created_with(response: fixture.response.symbolized.merge(response_defaults))
421
+ include_context "triggers one stub"
422
+
423
+ it "contain the provided trigger builder" do
424
+ expect_stub_to_be_created_with(triggers: [ trigger_builder ])
425
+
426
+ subject
427
+ end
428
+
429
+ end
430
+
431
+ context "when many triggers are added" do
432
+
433
+ include_context "triggers many stubs"
434
+
435
+ it "contain the provided trigger builders" do
436
+ expect_stub_to_be_created_with(triggers: trigger_builders)
437
+
438
+ subject
439
+ end
384
440
 
385
- subject
386
441
  end
387
442
 
388
- context "and response options provided match the defaults" do
443
+ end
389
444
 
390
- let(:response_defaults) do
445
+ context "when a default stub builder is provided that contains defaults" do
446
+
447
+ let(:request_defaults) do
448
+ {
449
+ uri: "/uri/value",
450
+ headers: { "request_header_name_1" => "request header value 1",
451
+ "request_header_name_2" => "request header value 2" },
452
+ parameters: { "parameter_name_1" => "parameter value 1",
453
+ "parameter_name_2" => "parameter value 2" },
454
+ body: "body value"
455
+ }
456
+ end
457
+ let(:response_defaults) do
458
+ {
459
+ status: 203,
460
+ headers: { "response_header_name_1" => "response header value 1",
461
+ "response_header_name_2" => "response header value 2" },
462
+ body: "some body",
463
+ delay_in_seconds: 8
464
+ }
465
+ end
466
+ let(:trigger_defaults) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
467
+
468
+ let(:default_builder) do
469
+ instance_double(HttpStub::Configurer::DSL::StubBuilder, request: request_defaults,
470
+ response: response_defaults,
471
+ triggers: trigger_defaults)
472
+ end
473
+
474
+ describe "the built request payload" do
475
+
476
+ let(:request_overrides) do
391
477
  {
392
- status: 203,
393
- headers: { "header-name-1" => "value 1", "header-name-2" => "value 2" },
394
- body: "some body",
395
- delay_in_seconds: 8
478
+ uri: "/some/updated/uri",
479
+ headers: { "request_header_name_2" => "updated request header value 2",
480
+ "request_header_name_3" => "request header value 3" },
481
+ parameters: { "parameter_name_2" => "updated parameter value 2",
482
+ "parameter_name_3" => "parameter value 3" },
483
+ body: "updated body value"
396
484
  }
397
485
  end
486
+
487
+ before(:example) { fixture.request = request_overrides }
488
+
489
+ it "overrides any defaults with values established in the stub" do
490
+ expect_stub_to_be_created_with(
491
+ request: {
492
+ uri: "/some/updated/uri",
493
+ headers: { "request_header_name_1" => "request header value 1",
494
+ "request_header_name_2" => "updated request header value 2",
495
+ "request_header_name_3" => "request header value 3" },
496
+ parameters: { "parameter_name_1" => "parameter value 1",
497
+ "parameter_name_2" => "updated parameter value 2",
498
+ "parameter_name_3" => "parameter value 3" },
499
+ body: "updated body value"
500
+ }
501
+ )
502
+
503
+ subject
504
+ end
505
+
506
+ end
507
+
508
+ describe "the built response payload" do
509
+
398
510
  let(:response_overrides) do
399
511
  {
400
512
  status: 302,
401
- headers: { "header-name-2" => "updated value 2", "header-name-3" => "value 3" },
402
- body: "another body"
513
+ headers: { "response_header_name_2" => "updated response header value 2",
514
+ "response_header_name_3" => "response header value 3" },
515
+ body: "updated body"
403
516
  }
404
517
  end
405
518
 
406
519
  before(:example) { fixture.response = response_overrides }
407
520
 
408
- it "overriddes the defaults with the provided options" do
521
+ it "overrides any defaults with values established in the stub" do
409
522
  expect_stub_to_be_created_with(
410
523
  response: {
411
524
  status: 302,
412
- headers: { "header-name-1" => "value 1",
413
- "header-name-2" => "updated value 2",
414
- "header-name-3" => "value 3" },
415
- body: "another body",
525
+ headers: { "response_header_name_1" => "response header value 1",
526
+ "response_header_name_2" => "updated response header value 2",
527
+ "response_header_name_3" => "response header value 3" },
528
+ body: "updated body",
416
529
  delay_in_seconds: 8
417
530
  }
418
531
  )
@@ -422,28 +535,16 @@ describe HttpStub::Configurer::DSL::StubBuilder do
422
535
 
423
536
  end
424
537
 
425
- end
426
-
427
- context "when a trigger is added" do
428
-
429
- include_context "triggers one stub"
430
-
431
- it "creates a stub payload with the provided trigger builder" do
432
- expect_stub_to_be_created_with(triggers: [ trigger_builder ])
433
-
434
- subject
435
- end
436
-
437
- end
538
+ describe "the built response triggers" do
438
539
 
439
- context "when many triggers are added" do
540
+ let(:triggers) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
440
541
 
441
- include_context "triggers many stubs"
542
+ it "combines any defaults with values values established in the stub" do
543
+ expect_stub_to_be_created_with(triggers: trigger_defaults + triggers)
442
544
 
443
- it "creates a stub payload with the provided trigger builders" do
444
- expect_stub_to_be_created_with(triggers: trigger_builders)
545
+ subject
546
+ end
445
547
 
446
- subject
447
548
  end
448
549
 
449
550
  end
data/spec/spec_helper.rb CHANGED
@@ -21,7 +21,7 @@ require_relative '../examples/configurer_with_trivial_scenarios'
21
21
  require_relative '../examples/configurer_with_exhaustive_scenarios'
22
22
  require_relative '../examples/configurer_with_initialize_callback'
23
23
  require_relative '../examples/configurer_with_complex_initializer'
24
- require_relative '../examples/configurer_with_response_defaults'
24
+ require_relative '../examples/configurer_with_server_defaults'
25
25
  require_relative '../examples/configurer_with_stub_triggers'
26
26
  require_relative '../examples/configurer_with_file_responses'
27
27
  require_relative '../examples/configurer_with_stub_request_body'
@@ -57,6 +57,10 @@ module HttpStub
57
57
  @trigger_fixtures = []
58
58
  end
59
59
 
60
+ def request=(options)
61
+ self.tap { @request.payload = options }
62
+ end
63
+
60
64
  def response=(options)
61
65
  self.tap { @response.payload = options }
62
66
  end
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.18.2
4
+ version: 0.19.0
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: 2015-06-23 00:00:00.000000000 Z
12
+ date: 2015-06-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -351,6 +351,7 @@ files:
351
351
  - "./spec/acceptance/configurer_part_spec.rb"
352
352
  - "./spec/acceptance/endpoint_template_spec.rb"
353
353
  - "./spec/acceptance/scenario_spec.rb"
354
+ - "./spec/acceptance/server_defaults_spec.rb"
354
355
  - "./spec/acceptance/stub_body_request_matching_spec.rb"
355
356
  - "./spec/acceptance/stub_control_values_spec.rb"
356
357
  - "./spec/acceptance/stub_spec.rb"
@@ -461,6 +462,7 @@ test_files:
461
462
  - "./spec/acceptance/configurer_part_spec.rb"
462
463
  - "./spec/acceptance/endpoint_template_spec.rb"
463
464
  - "./spec/acceptance/scenario_spec.rb"
465
+ - "./spec/acceptance/server_defaults_spec.rb"
464
466
  - "./spec/acceptance/stub_body_request_matching_spec.rb"
465
467
  - "./spec/acceptance/stub_control_values_spec.rb"
466
468
  - "./spec/acceptance/stub_spec.rb"