http_stub 0.14.2 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub/configurer/request/http/basic.rb +21 -0
  3. data/lib/http_stub/configurer/request/http/factory.rb +43 -0
  4. data/lib/http_stub/configurer/request/http/multipart.rb +33 -0
  5. data/lib/http_stub/configurer/request/stub.rb +24 -8
  6. data/lib/http_stub/configurer/request/stub_activator.rb +12 -8
  7. data/lib/http_stub/configurer/request/{stub_activator_payload_builder.rb → stub_activator_builder.rb} +4 -4
  8. data/lib/http_stub/configurer/request/stub_builder.rb +34 -0
  9. data/lib/http_stub/configurer/request/stub_response.rb +40 -0
  10. data/lib/http_stub/configurer/request/stub_response_file.rb +20 -0
  11. data/lib/http_stub/configurer/server/command.rb +5 -1
  12. data/lib/http_stub/configurer/server/command_processor.rb +1 -1
  13. data/lib/http_stub/configurer/server/dsl.rb +4 -4
  14. data/lib/http_stub/configurer/server/facade.rb +25 -11
  15. data/lib/http_stub/extensions/core/hash.rb +26 -0
  16. data/lib/http_stub/rake/server_daemon_tasks.rb +1 -1
  17. data/lib/http_stub/rake/server_tasks.rb +1 -1
  18. data/lib/http_stub/rake/task_generators.rb +3 -3
  19. data/lib/http_stub/server/application.rb +117 -0
  20. data/lib/http_stub/server/daemon.rb +39 -0
  21. data/lib/http_stub/{models → server}/exact_value_matcher.rb +1 -1
  22. data/lib/http_stub/{models → server}/hash_with_string_value_matchers.rb +2 -2
  23. data/lib/http_stub/{models → server}/headers.rb +1 -1
  24. data/lib/http_stub/{models → server}/omitted_value_matcher.rb +1 -1
  25. data/lib/http_stub/{models → server}/regexp_value_matcher.rb +1 -1
  26. data/lib/http_stub/{models → server}/registry.rb +10 -3
  27. data/lib/http_stub/{models → server}/request_header_parser.rb +1 -1
  28. data/lib/http_stub/server/response.rb +13 -0
  29. data/lib/http_stub/server/response_pipeline.rb +18 -0
  30. data/lib/http_stub/{models → server}/string_value_matcher.rb +4 -4
  31. data/lib/http_stub/server/stub.rb +37 -0
  32. data/lib/http_stub/{models → server}/stub_activator.rb +10 -2
  33. data/lib/http_stub/{controllers → server}/stub_activator_controller.rb +5 -5
  34. data/lib/http_stub/{controllers → server}/stub_controller.rb +4 -4
  35. data/lib/http_stub/server/stub_factory.rb +30 -0
  36. data/lib/http_stub/{models → server}/stub_headers.rb +4 -4
  37. data/lib/http_stub/{models → server}/stub_parameters.rb +2 -2
  38. data/lib/http_stub/{models → server}/stub_registry.rb +4 -4
  39. data/lib/http_stub/server/stub_response/base.rb +62 -0
  40. data/lib/http_stub/server/stub_response/file.rb +48 -0
  41. data/lib/http_stub/server/stub_response/text.rb +17 -0
  42. data/lib/http_stub/server/stub_response.rb +14 -0
  43. data/lib/http_stub/{models → server}/stub_triggers.rb +6 -2
  44. data/lib/http_stub/{models → server}/stub_uri.rb +2 -2
  45. data/lib/http_stub/{views → server/views}/_stub.haml +0 -0
  46. data/lib/http_stub/{views → server/views}/application.sass +0 -0
  47. data/lib/http_stub/{views → server/views}/layout.haml +0 -0
  48. data/lib/http_stub/{views → server/views}/stub_activators.haml +0 -0
  49. data/lib/http_stub/{views → server/views}/stubs.haml +0 -0
  50. data/lib/http_stub/version.rb +1 -1
  51. data/lib/http_stub.rb +41 -24
  52. data/spec/lib/http_stub/configurer/deprecated_dsl_spec.rb +2 -2
  53. data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +15 -0
  54. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +148 -0
  55. data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +74 -0
  56. data/spec/lib/http_stub/configurer/request/stub_activator_builder_spec.rb +119 -0
  57. data/spec/lib/http_stub/configurer/request/stub_activator_spec.rb +24 -22
  58. data/spec/lib/http_stub/configurer/request/stub_builder_spec.rb +154 -0
  59. data/spec/lib/http_stub/configurer/request/stub_response_spec.rb +136 -0
  60. data/spec/lib/http_stub/configurer/request/stub_spec.rb +238 -17
  61. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +7 -3
  62. data/spec/lib/http_stub/configurer/server/command_spec.rb +7 -6
  63. data/spec/lib/http_stub/configurer/server/dsl_spec.rb +58 -94
  64. data/spec/lib/http_stub/configurer/server/facade_spec.rb +56 -41
  65. data/spec/lib/http_stub/configurer_integration_spec.rb +180 -81
  66. data/spec/lib/http_stub/{hash_extensions_spec.rb → extensions/core/hash_spec.rb} +1 -1
  67. data/spec/lib/http_stub/rake/server_daemon_tasks_smoke_spec.rb +1 -1
  68. data/spec/lib/http_stub/{server_integration_spec.rb → server/application_integration_spec.rb} +1 -1
  69. data/spec/lib/http_stub/server/application_spec.rb +205 -0
  70. data/spec/lib/http_stub/server/daemon_integration_spec.rb +7 -0
  71. data/spec/lib/http_stub/{server_daemon_spec.rb → server/daemon_spec.rb} +4 -4
  72. data/spec/lib/http_stub/{models → server}/exact_value_matcher_spec.rb +2 -2
  73. data/spec/lib/http_stub/{models → server}/hash_with_string_value_matchers_spec.rb +5 -5
  74. data/spec/lib/http_stub/{models → server}/headers_spec.rb +2 -2
  75. data/spec/lib/http_stub/{models → server}/omitted_value_matcher_spec.rb +2 -2
  76. data/spec/lib/http_stub/{models → server}/regexp_value_matcher_spec.rb +2 -2
  77. data/spec/lib/http_stub/{models → server}/registry_spec.rb +52 -55
  78. data/spec/lib/http_stub/{models → server}/request_header_parser_spec.rb +4 -4
  79. data/spec/lib/http_stub/server/response_pipeline_spec.rb +29 -0
  80. data/spec/lib/http_stub/server/response_spec.rb +41 -0
  81. data/spec/lib/http_stub/{models → server}/string_value_matcher_spec.rb +11 -11
  82. data/spec/lib/http_stub/server/stub_activator_controller_spec.rb +85 -0
  83. data/spec/lib/http_stub/server/stub_activator_spec.rb +124 -0
  84. data/spec/lib/http_stub/server/stub_controller_spec.rb +74 -0
  85. data/spec/lib/http_stub/server/stub_factory_spec.rb +117 -0
  86. data/spec/lib/http_stub/{models → server}/stub_headers_spec.rb +9 -9
  87. data/spec/lib/http_stub/{models → server}/stub_parameters_spec.rb +6 -6
  88. data/spec/lib/http_stub/{models → server}/stub_registry_integration_spec.rb +3 -3
  89. data/spec/lib/http_stub/{models → server}/stub_registry_spec.rb +15 -35
  90. data/spec/lib/http_stub/server/stub_response/base_spec.rb +225 -0
  91. data/spec/lib/http_stub/server/stub_response/file_spec.rb +140 -0
  92. data/spec/lib/http_stub/server/stub_response/text_spec.rb +103 -0
  93. data/spec/lib/http_stub/server/stub_response_spec.rb +49 -0
  94. data/spec/lib/http_stub/{models → server}/stub_spec.rb +34 -18
  95. data/spec/lib/http_stub/{models → server}/stub_triggers_spec.rb +26 -16
  96. data/spec/lib/http_stub/{models → server}/stub_uri_spec.rb +5 -5
  97. data/spec/resources/sample.pdf +0 -0
  98. data/spec/resources/sample.txt +1 -0
  99. data/spec/spec_helper.rb +13 -4
  100. data/spec/support/stub_fixture.rb +106 -0
  101. metadata +148 -102
  102. data/lib/http_stub/configurer/request/stub_payload_builder.rb +0 -46
  103. data/lib/http_stub/hash_extensions.rb +0 -22
  104. data/lib/http_stub/models/request_pipeline.rb +0 -13
  105. data/lib/http_stub/models/response.rb +0 -43
  106. data/lib/http_stub/models/stub.rb +0 -35
  107. data/lib/http_stub/server.rb +0 -114
  108. data/lib/http_stub/server_daemon.rb +0 -36
  109. data/spec/lib/http_stub/configurer/request/stub_activator_payload_builder_spec.rb +0 -103
  110. data/spec/lib/http_stub/configurer/request/stub_payload_builder_spec.rb +0 -304
  111. data/spec/lib/http_stub/controllers/stub_activator_controller_spec.rb +0 -94
  112. data/spec/lib/http_stub/controllers/stub_controller_spec.rb +0 -83
  113. data/spec/lib/http_stub/models/request_pipeline_spec.rb +0 -17
  114. data/spec/lib/http_stub/models/response_spec.rb +0 -206
  115. data/spec/lib/http_stub/models/stub_activator_spec.rb +0 -76
  116. data/spec/lib/http_stub/server_daemon_integration_spec.rb +0 -7
  117. data/spec/lib/http_stub/server_spec.rb +0 -234
  118. data/spec/support/http_stub/configurer/request/stub_payload_builder_shared_context.rb +0 -27
@@ -1,41 +1,262 @@
1
1
  describe HttpStub::Configurer::Request::Stub do
2
2
 
3
- describe "#initialize" do
3
+ let(:fixture) { HttpStub::StubFixture.new }
4
+ let(:stub_response) { instance_double(HttpStub::Configurer::Request::StubResponse).as_null_object }
5
+ let(:trigger_builders) { [] }
4
6
 
5
- context "when provided a payload" do
7
+ let(:stub) do
8
+ HttpStub::Configurer::Request::Stub.new(fixture.configurer_payload.merge(triggers: trigger_builders))
9
+ end
10
+
11
+ before(:example) { allow(HttpStub::Configurer::Request::StubResponse).to receive(:new).and_return(stub_response) }
12
+
13
+ shared_context "triggers one stub" do
14
+
15
+ let(:trigger_payload) { HttpStub::StubFixture.new.configurer_payload }
16
+ let(:trigger_files) { [] }
17
+ let(:trigger) do
18
+ instance_double(HttpStub::Configurer::Request::Stub,
19
+ payload: trigger_payload, response_files: trigger_files)
20
+ end
21
+ let(:trigger_builders) do
22
+ [ instance_double(HttpStub::Configurer::Request::StubBuilder, build: trigger) ]
23
+ end
24
+
25
+ end
26
+
27
+ shared_context "triggers many stubs" do
28
+
29
+ let(:trigger_payloads) { (1..3).map { HttpStub::StubFixture.new.configurer_payload } }
30
+ let(:triggers_files) { trigger_payloads.map { [] } }
31
+ let(:triggers) do
32
+ trigger_payloads.zip(triggers_files).map do |payload, files|
33
+ instance_double(HttpStub::Configurer::Request::Stub, payload: payload, response_files: files)
34
+ end
35
+ end
36
+ let(:trigger_builders) do
37
+ triggers.map do |trigger|
38
+ instance_double(HttpStub::Configurer::Request::StubBuilder, build: trigger)
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ describe "#payload" do
45
+
46
+ subject { stub.payload }
47
+
48
+ before(:example) { allow(HttpStub::Configurer::Request::ControllableValue).to receive(:format) }
49
+
50
+ context "when request headers are provided" do
51
+
52
+ it "formats the headers into control values" do
53
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.headers)
54
+
55
+ subject
56
+ end
57
+
58
+ end
59
+
60
+ context "when no request header is provided" do
61
+
62
+ before(:example) do
63
+ fixture.request.headers = nil
64
+ end
65
+
66
+ it "formats an empty header hash" do
67
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
68
+
69
+ subject
70
+ end
71
+
72
+ end
73
+
74
+ context "when a request parameter is provided" do
75
+
76
+ it "formats the request parameters into control values" do
77
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with(fixture.request.parameters)
78
+
79
+ subject
80
+ end
81
+
82
+ end
83
+
84
+ context "when no request parameter is provided" do
85
+
86
+ before(:example) { fixture.request.parameters = nil }
87
+
88
+ it "formats an empty parameter hash" do
89
+ expect(HttpStub::Configurer::Request::ControllableValue).to receive(:format).with({})
90
+
91
+ subject
92
+ end
93
+
94
+ end
95
+
96
+ it "creates a stub response with the provided response options" do
97
+ expect(HttpStub::Configurer::Request::StubResponse).to(
98
+ receive(:new).with(anything, fixture.response.symbolized).and_return(stub_response)
99
+ )
100
+
101
+ subject
102
+ end
103
+
104
+ context "when many triggers are provided" do
105
+
106
+ include_context "triggers many stubs"
107
+
108
+ it "builds the request object for each trigger" do
109
+ trigger_builders.each { |trigger_builder| expect(trigger_builder).to receive(:build) }
110
+
111
+ subject
112
+ end
113
+
114
+ end
115
+
116
+ context "builds a hash that" do
117
+
118
+ it "has an entry containing the control value representation of the uri" do
119
+ allow(HttpStub::Configurer::Request::ControllableValue).to(
120
+ receive(:format).with(fixture.request.uri).and_return("uri as a string")
121
+ )
122
+
123
+ expect(subject).to include(uri: "uri as a string")
124
+ end
125
+
126
+ it "has an entry for the method option" do
127
+ expect(subject).to include(method: fixture.request.http_method)
128
+ end
6
129
 
7
- let(:payload) { { payload_key: "payload value" } }
130
+ it "has an entry containing the string representation of the request headers" do
131
+ allow(HttpStub::Configurer::Request::ControllableValue).to(
132
+ receive(:format).with(fixture.request.headers).and_return("request headers as string")
133
+ )
8
134
 
9
- let(:request) { HttpStub::Configurer::Request::Stub.new(payload) }
135
+ expect(subject).to include(headers: "request headers as string")
136
+ end
137
+
138
+ it "has an entry containing the string representation of the request parameters" do
139
+ allow(HttpStub::Configurer::Request::ControllableValue).to(
140
+ receive(:format).with(fixture.request.parameters).and_return("request parameters as string")
141
+ )
10
142
 
11
- it "creates a HTTP POST request" do
12
- expect(request.method).to eql("POST")
143
+ expect(subject).to include(parameters: "request parameters as string")
13
144
  end
14
145
 
15
- it "submits the request to '/stubs'" do
16
- expect(request.path).to eql("/stubs")
146
+ it "has an entry containing the response payload" do
147
+ allow(stub_response).to receive(:payload).and_return("response payload")
148
+
149
+ expect(subject).to include(response: "response payload")
150
+ end
151
+
152
+ context "when a trigger is added" do
153
+
154
+ include_context "triggers one stub"
155
+
156
+ it "has a triggers entry containing the stub trigger payload" do
157
+ expect(subject).to include(triggers: [ trigger_payload ])
158
+ end
159
+
17
160
  end
18
161
 
19
- it "sets the content type to json" do
20
- expect(request.content_type).to eql("application/json")
162
+ context "when many triggers are added" do
163
+
164
+ include_context "triggers many stubs"
165
+
166
+ it "has a triggers entry containing the stub trigger payloads" do
167
+ expect(subject).to include(triggers: trigger_payloads)
168
+ end
169
+
21
170
  end
22
171
 
23
- it "sets the body to the JSON representation of the provided payload" do
24
- expect(request.body).to eql(payload.to_json)
172
+ context "when no triggers are added" do
173
+
174
+ it "has a triggers entry containing an empty hash" do
175
+ expect(subject).to include(triggers: [])
176
+ end
177
+
25
178
  end
26
179
 
27
180
  end
28
181
 
29
182
  end
30
183
 
31
- describe "#stub_uri" do
184
+ describe "#response_files" do
185
+
186
+ subject { stub.response_files }
187
+
188
+ context "when the response contains a file" do
189
+
190
+ let(:response_file) { instance_double(HttpStub::Configurer::Request::StubResponseFile) }
191
+
192
+ before(:example) { allow(stub_response).to receive(:file).and_return(response_file) }
193
+
194
+ context "and the triggers contain files" do
195
+
196
+ include_context "triggers many stubs"
197
+
198
+ let(:triggers_files) do
199
+ trigger_payloads.map { (1..3).map { instance_double(HttpStub::Configurer::Request::StubResponseFile) } }
200
+ end
201
+
202
+ it "returns the stub response's file and the triggers files" do
203
+ expect(subject).to eql(([ response_file ] + triggers_files).flatten)
204
+ end
32
205
 
33
- let(:stub_uri) { "/some/stub/uri" }
206
+ end
207
+
208
+ context "and the triggers contain no files" do
209
+
210
+ include_context "triggers many stubs"
211
+
212
+ let(:triggers_files) { trigger_payloads.map { (1..3).map { [] } } }
213
+
214
+ it "returns the stub response's file" do
215
+ expect(subject).to eql([ response_file ])
216
+ end
217
+
218
+ end
219
+
220
+ end
221
+
222
+ context "when the response body does not contain a file" do
223
+
224
+ before(:example) { allow(stub_response).to receive(:file).and_return(nil) }
225
+
226
+ context "and the triggers contain files" do
227
+
228
+ include_context "triggers many stubs"
229
+
230
+ let(:triggers_files) do
231
+ trigger_payloads.map { (1..3).map { instance_double(HttpStub::Configurer::Request::StubResponseFile) } }
232
+ end
233
+
234
+ it "returns the stub response's file and the triggers files" do
235
+ expect(subject).to eql(triggers_files.flatten)
236
+ end
237
+
238
+ end
239
+
240
+ context "and no triggers contain a file" do
241
+
242
+ include_context "triggers many stubs"
243
+
244
+ let(:triggers_files) { trigger_payloads.map { (1..3).map { [] } } }
245
+
246
+ it "returns a empty array" do
247
+ expect(subject).to eql([])
248
+ end
249
+
250
+ end
251
+
252
+ end
253
+
254
+ end
34
255
 
35
- let(:request) { HttpStub::Configurer::Request::Stub.new(uri: stub_uri) }
256
+ describe "#to_s" do
36
257
 
37
- it "should return the uri from the payload" do
38
- expect(request.stub_uri).to eql(stub_uri)
258
+ it "returns the request uri" do
259
+ expect(stub.to_s).to eql(fixture.request.uri)
39
260
  end
40
261
 
41
262
  end
@@ -19,7 +19,7 @@ describe HttpStub::Configurer::Server::CommandProcessor do
19
19
 
20
20
  describe "and the server responds with a 200 response" do
21
21
 
22
- let(:request) { Net::HTTP::Get.new("/stubs") }
22
+ let(:request) { create_get_request("/stubs") }
23
23
 
24
24
  it "executes without error" do
25
25
  expect { subject }.not_to raise_error
@@ -29,7 +29,7 @@ describe HttpStub::Configurer::Server::CommandProcessor do
29
29
 
30
30
  describe "and the server responds with a non-200 response" do
31
31
 
32
- let(:request) { Net::HTTP::Get.new("/causes_error") }
32
+ let(:request) { create_get_request("/causes_error") }
33
33
 
34
34
  it "raises an exception that includes the server base URI" do
35
35
  expect { subject }.to raise_error(/#{server_base_uri}/)
@@ -45,7 +45,7 @@ describe HttpStub::Configurer::Server::CommandProcessor do
45
45
 
46
46
  context "when the server is unavailable" do
47
47
 
48
- let(:request) { Net::HTTP::Get.new("/does/not/exist") }
48
+ let(:request) { create_get_request("/does/not/exist") }
49
49
 
50
50
  it "raises an exception that includes the server base URI" do
51
51
  expect { subject }.to raise_error(/#{server_base_uri}/)
@@ -57,6 +57,10 @@ describe HttpStub::Configurer::Server::CommandProcessor do
57
57
 
58
58
  end
59
59
 
60
+ def create_get_request(path)
61
+ HttpStub::Configurer::Request::Http::Basic.new(Net::HTTP::Get.new(path))
62
+ end
63
+
60
64
  end
61
65
 
62
66
  end
@@ -1,15 +1,16 @@
1
1
  describe HttpStub::Configurer::Server::Command do
2
2
 
3
- let(:request) { double("HttpRequest") }
4
- let(:description) { "Some Description" }
5
- let(:args) { { request: request, description: description } }
3
+ let(:http_request) { double("HttpRequest") }
4
+ let(:request) { double("HttpStubRequest", to_http_request: http_request) }
5
+ let(:description) { "Some Description" }
6
+ let(:args) { { request: request, description: description } }
6
7
 
7
8
  let(:command) { HttpStub::Configurer::Server::Command.new(args) }
8
9
 
9
- describe "#request" do
10
+ describe "#http_request" do
10
11
 
11
- it "exposes the provided request" do
12
- expect(command.request).to eql(request)
12
+ it "returns the provided request adapted to a http request" do
13
+ expect(command.http_request).to eql(http_request)
13
14
  end
14
15
 
15
16
  end
@@ -16,10 +16,10 @@ describe HttpStub::Configurer::Server::DSL do
16
16
 
17
17
  describe "#build_stub" do
18
18
 
19
- let(:stub_payload_builder) { instance_double(HttpStub::Configurer::Request::StubPayloadBuilder) }
19
+ let(:stub_builder) { instance_double(HttpStub::Configurer::Request::StubBuilder) }
20
20
 
21
21
  before(:example) do
22
- allow(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).and_return(stub_payload_builder)
22
+ allow(HttpStub::Configurer::Request::StubBuilder).to receive(:new).and_return(stub_builder)
23
23
  end
24
24
 
25
25
  subject { dsl.build_stub }
@@ -30,8 +30,8 @@ describe HttpStub::Configurer::Server::DSL do
30
30
 
31
31
  before(:example) { dsl.response_defaults = { key: "value" } }
32
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)
33
+ it "creates a stub builder containing the response defaults" do
34
+ expect(HttpStub::Configurer::Request::StubBuilder).to receive(:new).with(response_defaults)
35
35
 
36
36
  subject
37
37
  end
@@ -40,8 +40,8 @@ describe HttpStub::Configurer::Server::DSL do
40
40
 
41
41
  context "when no response defaults have been established" do
42
42
 
43
- it "creates a stub payload builder with empty response defaults" do
44
- expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).with({})
43
+ it "creates a stub builder with empty response defaults" do
44
+ expect(HttpStub::Configurer::Request::StubBuilder).to receive(:new).with({})
45
45
 
46
46
  subject
47
47
  end
@@ -55,13 +55,13 @@ describe HttpStub::Configurer::Server::DSL do
55
55
  subject { dsl.build_stub(&block) }
56
56
 
57
57
  it "yields the created builder to the provided block" do
58
- expect(block).to receive(:call).with(stub_payload_builder)
58
+ expect(block).to receive(:call).with(stub_builder)
59
59
 
60
60
  subject
61
61
  end
62
62
 
63
63
  it "returns the created builder" do
64
- expect(subject).to eql(stub_payload_builder)
64
+ expect(subject).to eql(stub_builder)
65
65
  end
66
66
 
67
67
  end
@@ -71,7 +71,7 @@ describe HttpStub::Configurer::Server::DSL do
71
71
  subject { dsl.build_stub }
72
72
 
73
73
  it "returns the built stub" do
74
- expect(subject).to eql(stub_payload_builder)
74
+ expect(subject).to eql(stub_builder)
75
75
  end
76
76
 
77
77
  end
@@ -80,70 +80,56 @@ describe HttpStub::Configurer::Server::DSL do
80
80
 
81
81
  describe "#add_stub!" do
82
82
 
83
- let(:stub_payload) { { stub_payload_key: "stub payload value" } }
84
- let(:stub_payload_builder) do
85
- instance_double(HttpStub::Configurer::Request::StubPayloadBuilder, build: stub_payload)
86
- end
87
- let(:stub_request) { instance_double(HttpStub::Configurer::Request::Stub) }
88
-
89
- before(:example) do
90
- allow(HttpStub::Configurer::Request::Stub).to receive(:new).and_return(stub_request)
91
- allow(server_facade).to receive(:stub_response)
92
- end
83
+ let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
84
+ let(:stub_builder) { instance_double(HttpStub::Configurer::Request::StubBuilder, build: stub) }
93
85
 
94
- shared_examples_for "adding a stub payload" do
86
+ before(:example) { allow(server_facade).to receive(:stub_response) }
95
87
 
96
- it "builds the stub payload" do
97
- expect(stub_payload_builder).to receive(:build).and_return(stub_payload)
88
+ shared_examples_for "adding a stub request" do
98
89
 
99
- subject
100
- end
101
-
102
- it "creates a stub request with the built payload" do
103
- expect(HttpStub::Configurer::Request::Stub).to receive(:new).with(stub_payload).and_return(stub_request)
90
+ it "builds the stub request" do
91
+ expect(stub_builder).to receive(:build).and_return(stub)
104
92
 
105
93
  subject
106
94
  end
107
95
 
108
96
  it "informs the server facade of the stub request" do
109
- expect(server_facade).to receive(:stub_response).with(stub_request)
97
+ expect(server_facade).to receive(:stub_response).with(stub)
110
98
 
111
99
  subject
112
100
  end
113
101
 
114
102
  end
115
103
 
116
- context "when a stub payload builder is provided" do
104
+ context "when a stub builder is provided" do
117
105
 
118
- subject { dsl.add_stub!(stub_payload_builder) }
106
+ subject { dsl.add_stub!(stub_builder) }
119
107
 
120
- it_behaves_like "adding a stub payload"
108
+ it_behaves_like "adding a stub request"
121
109
 
122
110
  end
123
111
 
124
- context "when a stub payload builder is not provided" do
112
+ context "when a stub builder is not provided" do
125
113
 
126
114
  let(:block) { lambda { |_stub| "some block" } }
127
115
 
128
- before(:example) do
129
- allow(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new).and_return(stub_payload_builder)
130
- end
116
+ before(:example) { allow(HttpStub::Configurer::Request::StubBuilder).to receive(:new).and_return(stub_builder) }
131
117
 
132
118
  subject { dsl.add_stub!(&block) }
133
119
 
134
- it "creates a stub payload builder" do
135
- expect(HttpStub::Configurer::Request::StubPayloadBuilder).to receive(:new)
120
+ it "creates a stub builder" do
121
+ expect(HttpStub::Configurer::Request::StubBuilder).to receive(:new)
136
122
 
137
123
  subject
138
124
  end
139
125
 
140
126
  it "yields the created builder to the provided block" do
141
- expect(block).to receive(:call).with(stub_payload_builder)
127
+ expect(block).to receive(:call).with(stub_builder)
142
128
 
143
129
  subject
144
130
  end
145
131
 
146
- it_behaves_like "adding a stub payload"
132
+ it_behaves_like "adding a stub request"
147
133
 
148
134
  end
149
135
 
@@ -151,43 +137,25 @@ describe HttpStub::Configurer::Server::DSL do
151
137
 
152
138
  describe "#add_stubs!" do
153
139
 
154
- context "when multiple stub payload builders are provided" do
140
+ context "when multiple stub builders are provided" do
155
141
 
156
- let(:stub_payloads) { (1..3).map { |i| { "stub_payload_key_#{i}" => "stub payload value #{i}" } } }
157
- let(:stub_payload_builders) do
158
- stub_payloads.map do |payload|
159
- instance_double(HttpStub::Configurer::Request::StubPayloadBuilder, build: payload)
160
- end
161
- end
162
- let(:stub_requests) do
163
- (1..stub_payloads.length).map { instance_double(HttpStub::Configurer::Request::Stub) }
164
- end
165
-
166
- subject { dsl.add_stubs!(stub_payload_builders) }
167
-
168
- before(:example) do
169
- allow(HttpStub::Configurer::Request::Stub).to receive(:new).and_return(*stub_requests)
170
- allow(server_facade).to receive(:stub_response)
142
+ let(:stubs) { (1..3).map { instance_double(HttpStub::Configurer::Request::Stub) } }
143
+ let(:stub_builders) do
144
+ stubs.map { |stub| instance_double(HttpStub::Configurer::Request::StubBuilder, build: stub) }
171
145
  end
172
146
 
173
- it "builds the each stub payload" do
174
- stub_payload_builders.zip(stub_payloads).each do |stub_payload_builder, stub_payload|
175
- expect(stub_payload_builder).to receive(:build).and_return(stub_payload)
176
- end
147
+ subject { dsl.add_stubs!(stub_builders) }
177
148
 
178
- subject
179
- end
149
+ before(:example) { allow(server_facade).to receive(:stub_response) }
180
150
 
181
- it "creates a stub request for each built payload" do
182
- stub_payloads.zip(stub_requests).each do |stub_payload, stub_request|
183
- expect(HttpStub::Configurer::Request::Stub).to receive(:new).with(stub_payload).and_return(stub_request)
184
- end
151
+ it "builds each stub" do
152
+ stubs.zip(stub_builders).each { |stub, stub_builder| expect(stub_builder).to receive(:build).and_return(stub) }
185
153
 
186
154
  subject
187
155
  end
188
156
 
189
- it "informs the server facade of each stub request" do
190
- stub_requests.each { |stub_request| expect(server_facade).to receive(:stub_response).with(stub_request) }
157
+ it "informs the server facade of each built stub" do
158
+ stubs.each { |stub| expect(server_facade).to receive(:stub_response).with(stub) }
191
159
 
192
160
  subject
193
161
  end
@@ -198,63 +166,59 @@ describe HttpStub::Configurer::Server::DSL do
198
166
 
199
167
  describe "#add_activator!" do
200
168
 
201
- let(:stub_activator_payload) { { activator_payload_key: "activator payload value" } }
202
- let(:stub_activator_payload_builder) do
203
- instance_double(HttpStub::Configurer::Request::StubActivatorPayloadBuilder, build: stub_activator_payload)
169
+ let(:stub_activator) { instance_double(HttpStub::Configurer::Request::StubActivator) }
170
+ let(:stub_activator_builder) do
171
+ instance_double(HttpStub::Configurer::Request::StubActivatorBuilder, build: stub_activator)
204
172
  end
205
- let(:stub_activator_request) { instance_double(HttpStub::Configurer::Request::StubActivator) }
206
173
 
207
- before(:example) do
208
- allow(HttpStub::Configurer::Request::StubActivator).to receive(:new).and_return(stub_activator_request)
209
- allow(server_facade).to receive(:stub_activator)
210
- end
174
+ before(:example) { allow(server_facade).to receive(:stub_activator) }
211
175
 
212
176
  let(:block) { lambda { |_activator| "some block" } }
213
177
 
214
178
  before(:example) do
215
- allow(HttpStub::Configurer::Request::StubActivatorPayloadBuilder).to(
216
- receive(:new).and_return(stub_activator_payload_builder)
217
- )
179
+ allow(HttpStub::Configurer::Request::StubActivatorBuilder).to receive(:new).and_return(stub_activator_builder)
218
180
  end
219
181
 
220
182
  subject { dsl.add_activator!(&block) }
221
183
 
222
184
  context "when response defaults have been established" do
223
185
 
224
- end
186
+ let(:response_defaults) { { key: "value" } }
225
187
 
226
- context "when no response defaults have been established" do
188
+ before(:example) { dsl.response_defaults = { key: "value" } }
227
189
 
228
- end
190
+ it "creates a stub activator builder containing the response defaults" do
191
+ expect(HttpStub::Configurer::Request::StubActivatorBuilder).to receive(:new).with(response_defaults)
229
192
 
230
- it "creates a stub activator payload builder" do
231
- expect(HttpStub::Configurer::Request::StubActivatorPayloadBuilder).to receive(:new)
193
+ subject
194
+ end
232
195
 
233
- subject
234
196
  end
235
197
 
236
- it "yields the created builder to the provided block" do
237
- expect(block).to receive(:call).with(stub_activator_payload_builder)
198
+ context "when no response defaults have been established" do
199
+
200
+ it "creates a stub activator builder with empty response defaults" do
201
+ expect(HttpStub::Configurer::Request::StubActivatorBuilder).to receive(:new).with({})
202
+
203
+ subject
204
+ end
238
205
 
239
- subject
240
206
  end
241
207
 
242
- it "builds the stub activator payload" do
243
- expect(stub_activator_payload_builder).to receive(:build).and_return(stub_activator_payload)
208
+ it "yields the created builder to the provided block" do
209
+ expect(block).to receive(:call).with(stub_activator_builder)
244
210
 
245
211
  subject
246
212
  end
247
213
 
248
- it "creates a stub activator request with the built payload" do
249
- expect(HttpStub::Configurer::Request::StubActivator).to(
250
- receive(:new).with(stub_activator_payload).and_return(stub_activator_request)
251
- )
214
+ it "builds the stub activator request" do
215
+ expect(stub_activator_builder).to receive(:build).and_return(stub_activator)
252
216
 
253
217
  subject
254
218
  end
255
219
 
256
220
  it "informs the server facade to submit the stub activator request" do
257
- expect(server_facade).to receive(:stub_activator).with(stub_activator_request)
221
+ expect(server_facade).to receive(:stub_activator).with(stub_activator)
258
222
 
259
223
  subject
260
224
  end