http_stub 0.25.1 → 0.26.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +32 -9
  3. data/lib/http_stub/configurer.rb +2 -10
  4. data/lib/http_stub/configurer/dsl/scenario_builder.rb +13 -11
  5. data/lib/http_stub/configurer/dsl/server.rb +32 -43
  6. data/lib/http_stub/configurer/dsl/server_endpoint_template.rb +23 -0
  7. data/lib/http_stub/configurer/dsl/session.rb +55 -0
  8. data/lib/http_stub/configurer/dsl/session_endpoint_template.rb +22 -0
  9. data/lib/http_stub/configurer/dsl/session_factory.rb +34 -0
  10. data/lib/http_stub/configurer/dsl/stub_builder.rb +18 -8
  11. data/lib/http_stub/configurer/dsl/stub_builder_template.rb +26 -0
  12. data/lib/http_stub/configurer/request/http/basic.rb +9 -3
  13. data/lib/http_stub/configurer/request/http/factory.rb +7 -16
  14. data/lib/http_stub/configurer/request/http/multipart.rb +5 -7
  15. data/lib/http_stub/configurer/request/stub.rb +3 -3
  16. data/lib/http_stub/configurer/request/triggers.rb +25 -0
  17. data/lib/http_stub/configurer/server/command_processor.rb +8 -10
  18. data/lib/http_stub/configurer/server/configuration.rb +33 -0
  19. data/lib/http_stub/configurer/server/facade.rb +21 -43
  20. data/lib/http_stub/configurer/server/request_processor.rb +2 -2
  21. data/lib/http_stub/configurer/server/session_facade.rb +64 -0
  22. data/lib/http_stub/extensions/core/uri.rb +28 -0
  23. data/lib/http_stub/extensions/rack/handler.rb +1 -0
  24. data/lib/http_stub/extensions/sinatra/namespace.rb +9 -0
  25. data/lib/http_stub/rake/server_tasks.rb +1 -4
  26. data/lib/http_stub/server/application/application.rb +17 -98
  27. data/lib/http_stub/server/application/configuration.rb +38 -0
  28. data/lib/http_stub/server/application/cross_origin_support.rb +7 -5
  29. data/lib/http_stub/server/application/request_support.rb +38 -0
  30. data/lib/http_stub/server/application/response_support.rb +19 -0
  31. data/lib/http_stub/server/application/routes/memory.rb +37 -0
  32. data/lib/http_stub/server/application/routes/resource.rb +23 -0
  33. data/lib/http_stub/server/application/routes/scenario.rb +52 -0
  34. data/lib/http_stub/server/application/routes/session.rb +65 -0
  35. data/lib/http_stub/server/application/routes/stub.rb +75 -0
  36. data/lib/http_stub/server/application/session_uri_support.rb +15 -0
  37. data/lib/http_stub/server/memory/controller.rb +25 -0
  38. data/lib/http_stub/server/memory/memory.rb +32 -0
  39. data/lib/http_stub/server/registry.rb +25 -8
  40. data/lib/http_stub/server/request/factory.rb +23 -0
  41. data/lib/http_stub/server/request/parameters.rb +2 -2
  42. data/lib/http_stub/server/request/request.rb +6 -11
  43. data/lib/http_stub/server/request/sinatra_request.rb +26 -0
  44. data/lib/http_stub/server/scenario/controller.rb +16 -11
  45. data/lib/http_stub/server/scenario/not_found_error.rb +15 -0
  46. data/lib/http_stub/server/session.rb +14 -0
  47. data/lib/http_stub/server/session/configuration.rb +32 -0
  48. data/lib/http_stub/server/session/controller.rb +40 -0
  49. data/lib/http_stub/server/session/empty.rb +15 -0
  50. data/lib/http_stub/server/session/identifier_strategy.rb +24 -0
  51. data/lib/http_stub/server/session/registry.rb +38 -0
  52. data/lib/http_stub/server/session/session.rb +80 -0
  53. data/lib/http_stub/server/stub/controller.rb +18 -12
  54. data/lib/http_stub/server/stub/empty.rb +1 -1
  55. data/lib/http_stub/server/stub/match/controller.rb +9 -5
  56. data/lib/http_stub/server/stub/payload/response_body_modifier.rb +3 -1
  57. data/lib/http_stub/server/stub/registry.rb +6 -13
  58. data/lib/http_stub/server/stub/triggers.rb +11 -9
  59. data/lib/http_stub/server/views/_home.haml +2 -0
  60. data/lib/http_stub/server/views/_post_link_function.haml +26 -0
  61. data/lib/http_stub/server/views/_session.haml +16 -0
  62. data/lib/http_stub/server/views/_stub.haml +8 -2
  63. data/lib/http_stub/server/views/_stubs.haml +4 -0
  64. data/lib/http_stub/server/views/application.sass +4 -1
  65. data/lib/http_stub/server/views/layout.haml +1 -0
  66. data/lib/http_stub/server/views/scenario.haml +4 -3
  67. data/lib/http_stub/server/views/scenarios.haml +7 -6
  68. data/lib/http_stub/server/views/session.haml +3 -0
  69. data/lib/http_stub/server/views/sessions.haml +6 -0
  70. data/lib/http_stub/server/views/stubs.haml +1 -4
  71. data/lib/http_stub/server/views/transactional_session.haml +2 -0
  72. data/lib/http_stub/version.rb +1 -1
  73. data/spec/acceptance/configurer_initialization_spec.rb +40 -51
  74. data/spec/acceptance/configurer_part_spec.rb +5 -7
  75. data/spec/acceptance/cross_origin_support_spec.rb +7 -13
  76. data/spec/acceptance/endpoint_template_spec.rb +2 -4
  77. data/spec/acceptance/request_reference_spec.rb +1 -3
  78. data/spec/acceptance/scenario_spec.rb +7 -9
  79. data/spec/acceptance/server_defaults_spec.rb +2 -4
  80. data/spec/acceptance/server_memory_spec.rb +20 -0
  81. data/spec/acceptance/session_spec.rb +125 -0
  82. data/spec/acceptance/stub_body_request_matching_spec.rb +2 -4
  83. data/spec/acceptance/stub_control_values_spec.rb +19 -18
  84. data/spec/acceptance/stub_match_last_spec.rb +0 -4
  85. data/spec/acceptance/stub_match_list_spec.rb +0 -4
  86. data/spec/acceptance/stub_miss_list_spec.rb +0 -4
  87. data/spec/acceptance/stub_spec.rb +11 -11
  88. data/spec/acceptance/stub_trigger_spec.rb +60 -13
  89. data/spec/lib/http_stub/configurer/dsl/scenario_builder_spec.rb +83 -30
  90. data/spec/lib/http_stub/configurer/dsl/server_endpoint_template_spec.rb +167 -0
  91. data/spec/lib/http_stub/configurer/dsl/server_spec.rb +306 -176
  92. data/spec/lib/http_stub/configurer/dsl/session_endpoint_template_spec.rb +150 -0
  93. data/spec/lib/http_stub/configurer/dsl/session_factory_spec.rb +97 -0
  94. data/spec/lib/http_stub/configurer/dsl/session_spec.rb +222 -0
  95. data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +209 -48
  96. data/spec/lib/http_stub/configurer/dsl/stub_builder_template_spec.rb +203 -0
  97. data/spec/lib/http_stub/configurer/request/http/basic_spec.rb +80 -4
  98. data/spec/lib/http_stub/configurer/request/http/factory_spec.rb +115 -56
  99. data/spec/lib/http_stub/configurer/request/http/multipart_spec.rb +39 -6
  100. data/spec/lib/http_stub/configurer/request/stub_spec.rb +50 -108
  101. data/spec/lib/http_stub/configurer/request/triggers_spec.rb +101 -0
  102. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +11 -10
  103. data/spec/lib/http_stub/configurer/server/configuration_spec.rb +139 -0
  104. data/spec/lib/http_stub/configurer/server/facade_spec.rb +65 -142
  105. data/spec/lib/http_stub/configurer/server/request_processor_spec.rb +7 -6
  106. data/spec/lib/http_stub/configurer/server/session_facade_spec.rb +336 -0
  107. data/spec/lib/http_stub/configurer_spec.rb +105 -0
  108. data/spec/lib/http_stub/extensions/core/uri_spec.rb +75 -0
  109. data/spec/lib/http_stub/extensions/rack/handler_spec.rb +1 -1
  110. data/spec/lib/http_stub/extensions/sinatra/namespace_spec.rb +36 -0
  111. data/spec/lib/http_stub/rake/server_tasks_smoke_spec.rb +4 -3
  112. data/spec/lib/http_stub/rake/server_tasks_spec.rb +47 -10
  113. data/spec/lib/http_stub/server/application/application_spec.rb +44 -301
  114. data/spec/lib/http_stub/server/application/configuration_spec.rb +59 -0
  115. data/spec/lib/http_stub/server/application/cross_origin_support_spec.rb +23 -20
  116. data/spec/lib/http_stub/server/application/request_support_integration_spec.rb +93 -0
  117. data/spec/lib/http_stub/server/application/request_support_spec.rb +61 -0
  118. data/spec/lib/http_stub/server/application/response_support_spec.rb +30 -0
  119. data/spec/lib/http_stub/server/application/routes/memory_spec.rb +41 -0
  120. data/spec/lib/http_stub/server/application/routes/resource_spec.rb +16 -0
  121. data/spec/lib/http_stub/server/application/routes/scenario_spec.rb +117 -0
  122. data/spec/lib/http_stub/server/application/routes/session_integration_spec.rb +46 -0
  123. data/spec/lib/http_stub/server/application/routes/session_spec.rb +186 -0
  124. data/spec/lib/http_stub/server/application/{application_integration_spec.rb → routes/stub_integration_spec.rb} +16 -38
  125. data/spec/lib/http_stub/server/application/routes/stub_spec.rb +202 -0
  126. data/spec/lib/http_stub/server/application/session_uri_support_spec.rb +48 -0
  127. data/spec/lib/http_stub/server/application/text_formatting_support_spec.rb +3 -3
  128. data/spec/lib/http_stub/server/memory/controller_spec.rb +47 -0
  129. data/spec/lib/http_stub/server/memory/memory_spec.rb +122 -0
  130. data/spec/lib/http_stub/server/registry_spec.rb +153 -34
  131. data/spec/lib/http_stub/server/request/factory_spec.rb +74 -0
  132. data/spec/lib/http_stub/server/request/parameters_spec.rb +5 -8
  133. data/spec/lib/http_stub/server/request/request_spec.rb +52 -52
  134. data/spec/lib/http_stub/server/request/sinatra_request_spec.rb +132 -0
  135. data/spec/lib/http_stub/server/scenario/controller_spec.rb +121 -35
  136. data/spec/lib/http_stub/server/scenario/not_found_error_spec.rb +25 -0
  137. data/spec/lib/http_stub/server/session/configuration_spec.rb +94 -0
  138. data/spec/lib/http_stub/server/session/controller_spec.rb +106 -0
  139. data/spec/lib/http_stub/server/session/empty_spec.rb +13 -0
  140. data/spec/lib/http_stub/server/session/identifier_strategy_spec.rb +126 -0
  141. data/spec/lib/http_stub/server/session/registry_spec.rb +214 -0
  142. data/spec/lib/http_stub/server/session/session_spec.rb +444 -0
  143. data/spec/lib/http_stub/server/stub/controller_spec.rb +89 -37
  144. data/spec/lib/http_stub/server/stub/empty_spec.rb +2 -2
  145. data/spec/lib/http_stub/server/stub/match/controller_spec.rb +59 -16
  146. data/spec/lib/http_stub/server/stub/payload/response_body_modifier_spec.rb +7 -8
  147. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +42 -15
  148. data/spec/lib/http_stub/server/stub/registry_spec.rb +38 -59
  149. data/spec/lib/http_stub/server/stub/triggers_spec.rb +93 -34
  150. data/spec/spec_helper.rb +13 -1
  151. data/spec/support/contain_file.rb +23 -0
  152. data/spec/support/cross_origin_server/application.rb +1 -1
  153. data/spec/support/cross_origin_server/index_page.rb +2 -2
  154. data/spec/support/cross_origin_server/integration.rb +3 -2
  155. data/spec/support/cross_origin_server/public/index.html +2 -1
  156. data/spec/support/http_stub/configurer_integration.rb +12 -14
  157. data/spec/support/http_stub/server/application/http_stub_rack_application_test.rb +47 -0
  158. data/spec/support/http_stub/server/driver.rb +56 -0
  159. data/spec/support/http_stub/server/memory_fixture.rb +17 -0
  160. data/spec/support/http_stub/server/request_fixture.rb +7 -6
  161. data/spec/support/http_stub/server/scenario_fixture.rb +13 -0
  162. data/spec/support/http_stub/server/session_fixture.rb +27 -0
  163. data/spec/support/http_stub/server_integration.rb +12 -17
  164. data/spec/support/http_stub/stub_fixture.rb +19 -6
  165. data/spec/support/include_in_json.rb +4 -4
  166. data/spec/support/rack/rack_application_test.rb +9 -0
  167. data/spec/support/rack/request_fixture.rb +13 -0
  168. data/spec/support/surpressed_output.rb +10 -0
  169. metadata +149 -36
  170. data/lib/http_stub/configurer/dsl/deprecated.rb +0 -60
  171. data/lib/http_stub/configurer/dsl/endpoint_template.rb +0 -38
  172. data/lib/http_stub/configurer/dsl/scenario_activator.rb +0 -15
  173. data/lib/http_stub/configurer/dsl/stub_activator_builder.rb +0 -24
  174. data/lib/http_stub/configurer/dsl/stub_builder_producer.rb +0 -21
  175. data/lib/http_stub/server/request.rb +0 -12
  176. data/lib/http_stub/server/scenario/activator.rb +0 -25
  177. data/lib/http_stub/server/views/_activate_scenario.haml +0 -23
  178. data/lib/http_stub/server/views/index.haml +0 -8
  179. data/spec/acceptance/activator_spec.rb +0 -81
  180. data/spec/lib/http_stub/configurer/dsl/deprecated_spec.rb +0 -240
  181. data/spec/lib/http_stub/configurer/dsl/endpoint_template_spec.rb +0 -255
  182. data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +0 -62
  183. data/spec/lib/http_stub/configurer/dsl/stub_activator_builder_spec.rb +0 -120
  184. data/spec/lib/http_stub/configurer/dsl/stub_builder_producer_spec.rb +0 -85
  185. data/spec/lib/http_stub/server/request_spec.rb +0 -24
  186. data/spec/lib/http_stub/server/scenario/activator_spec.rb +0 -111
  187. data/spec/support/http_stub/server/scenario/scenario_fixture.rb +0 -15
@@ -20,8 +20,8 @@ describe HttpStub::Server::Stub::Empty do
20
20
 
21
21
  describe "#triggers" do
22
22
 
23
- it "is an empty array" do
24
- expect(empty_stub.triggers).to eql([])
23
+ it "is empty" do
24
+ expect(empty_stub.triggers).to eql(HttpStub::Server::Stub::Triggers::EMPTY)
25
25
  end
26
26
 
27
27
  end
@@ -1,33 +1,36 @@
1
1
  describe HttpStub::Server::Stub::Match::Controller do
2
2
 
3
- let(:registry) { instance_double(HttpStub::Server::Registry) }
3
+ let(:request_parameters) { {} }
4
+ let(:session) { instance_double(HttpStub::Server::Session::Session) }
5
+ let(:request) do
6
+ instance_double(HttpStub::Server::Request::Request, parameters: request_parameters, session: session)
7
+ end
4
8
 
5
- let(:controller) { described_class.new(registry) }
9
+ let(:controller) { described_class.new }
6
10
 
7
- describe "#find_last" do
11
+ describe "#last_match" do
8
12
 
9
- let(:uri) { "/some/endpoint/uri" }
10
- let(:method) { "PATCH" }
11
- let(:parameters) { { uri: uri, method: method } }
12
- let(:request) { instance_double(HttpStub::Server::Request::Request, parameters: parameters) }
13
- let(:logger) { instance_double(Logger) }
13
+ let(:uri) { "/some/endpoint/uri" }
14
+ let(:method) { "PATCH" }
15
+ let(:request_parameters) { { uri: uri, method: method } }
16
+ let(:logger) { instance_double(Logger) }
14
17
 
15
- subject { controller.find_last(request, logger) }
18
+ subject { controller.last_match(request, logger) }
16
19
 
17
- it "finds the match in the registry with the uri requested" do
18
- expect(registry).to receive(:find).with(hash_including(uri: uri), anything)
20
+ it "finds the match in the users session having the uri requested" do
21
+ expect(session).to receive(:last_match).with(hash_including(uri: uri), anything)
19
22
 
20
23
  subject
21
24
  end
22
25
 
23
- it "finds the match in the registry with the HTTP method requested" do
24
- expect(registry).to receive(:find).with(hash_including(method: method), anything)
26
+ it "finds the match in the users session having the HTTP method requested" do
27
+ expect(session).to receive(:last_match).with(hash_including(method: method), anything)
25
28
 
26
29
  subject
27
30
  end
28
31
 
29
32
  it "finds the match using the provided logger for diagnostics" do
30
- expect(registry).to receive(:find).with(anything, logger)
33
+ expect(session).to receive(:last_match).with(anything, logger)
31
34
 
32
35
  subject
33
36
  end
@@ -37,7 +40,7 @@ describe HttpStub::Server::Stub::Match::Controller do
37
40
  let(:match_json) { "some match JSON" }
38
41
  let(:match) { instance_double(HttpStub::Server::Stub::Match::Match, to_json: match_json) }
39
42
 
40
- before(:example) { allow(registry).to receive(:find).and_return(match) }
43
+ before(:example) { allow(session).to receive(:last_match).and_return(match) }
41
44
 
42
45
  it "creates an ok response containing the JSON representation of the match" do
43
46
  expect(HttpStub::Server::Response).to receive(:ok).with("body" => match_json)
@@ -56,7 +59,7 @@ describe HttpStub::Server::Stub::Match::Controller do
56
59
 
57
60
  context "when a match is not found" do
58
61
 
59
- before(:example) { allow(registry).to receive(:find).and_return(nil) }
62
+ before(:example) { allow(session).to receive(:last_match).and_return(nil) }
60
63
 
61
64
  it "returns a not found response" do
62
65
  expect(subject).to eql(HttpStub::Server::Response::NOT_FOUND)
@@ -66,4 +69,44 @@ describe HttpStub::Server::Stub::Match::Controller do
66
69
 
67
70
  end
68
71
 
72
+ describe "#matches" do
73
+
74
+ let(:matches) { (1..3).map { instance_double(HttpStub::Server::Stub::Match::Match) } }
75
+
76
+ subject { controller.matches(request) }
77
+
78
+ before(:example) { allow(session).to receive(:matches).and_return(matches) }
79
+
80
+ it "retrieves all matches from the users session" do
81
+ expect(session).to receive(:matches)
82
+
83
+ subject
84
+ end
85
+
86
+ it "returns the matches" do
87
+ expect(subject).to eql(matches)
88
+ end
89
+
90
+ end
91
+
92
+ describe "#matches" do
93
+
94
+ let(:misses) { (1..3).map { instance_double(HttpStub::Server::Stub::Match::Miss) } }
95
+
96
+ subject { controller.misses(request) }
97
+
98
+ before(:example) { allow(session).to receive(:misses).and_return(misses) }
99
+
100
+ it "retrieves all misses from the users session" do
101
+ expect(session).to receive(:misses)
102
+
103
+ subject
104
+ end
105
+
106
+ it "returns the misses" do
107
+ expect(subject).to eql(misses)
108
+ end
109
+
110
+ end
111
+
69
112
  end
@@ -1,7 +1,5 @@
1
1
  describe HttpStub::Server::Stub::Payload::ResponseBodyModifier do
2
2
 
3
- let(:modifier) { described_class }
4
-
5
3
  describe "::modify!" do
6
4
 
7
5
  let(:parameters) { {} }
@@ -9,7 +7,7 @@ describe HttpStub::Server::Stub::Payload::ResponseBodyModifier do
9
7
 
10
8
  let!(:original_payload) { payload.clone }
11
9
 
12
- subject { modifier.modify!(payload, request) }
10
+ subject { described_class.modify!(payload, request) }
13
11
 
14
12
  context "when the payload contains a response file and has a corresponding file parameter" do
15
13
 
@@ -27,9 +25,9 @@ describe HttpStub::Server::Stub::Payload::ResponseBodyModifier do
27
25
 
28
26
  end
29
27
 
30
- context "when the payload contains triggers" do
28
+ context "when the payload contains triggered stubs" do
31
29
 
32
- let(:payload_fixture) { HttpStub::StubFixture.new.with_triggers!(trigger_fixtures) }
30
+ let(:payload_fixture) { HttpStub::StubFixture.new.with_triggered_stubs!(trigger_fixtures) }
33
31
  let(:payload) { payload_fixture.server_payload }
34
32
 
35
33
  context "and the trigger payloads contain response text" do
@@ -47,6 +45,7 @@ describe HttpStub::Server::Stub::Payload::ResponseBodyModifier do
47
45
  context "and the trigger payloads contain a response file with corresponding file parameters" do
48
46
 
49
47
  let(:trigger_fixtures) { (1..3).map { HttpStub::StubFixture.new.with_file_response! } }
48
+ let(:file_parameters) { trigger_fixtures.map(&:file_parameter) }
50
49
  let(:parameters) do
51
50
  trigger_fixtures.reduce({}) do |result, fixture|
52
51
  result.merge("response_file_#{fixture.id}" => fixture.file_parameter)
@@ -55,8 +54,8 @@ describe HttpStub::Server::Stub::Payload::ResponseBodyModifier do
55
54
 
56
55
  it "modifies the payload to have trigger response bodies replaced by the files" do
57
56
  expected_payload = payload.clone
58
- expected_payload["triggers"].zip(trigger_fixtures.map(&:file_parameter)).each do |trigger, file_param|
59
- trigger["response"]["body"] = file_param
57
+ expected_payload["triggers"]["stubs"].zip(file_parameters).each do |trigger, file_parameter|
58
+ trigger["response"]["body"] = file_parameter
60
59
  end
61
60
 
62
61
  subject
@@ -68,7 +67,7 @@ describe HttpStub::Server::Stub::Payload::ResponseBodyModifier do
68
67
 
69
68
  end
70
69
 
71
- context "when the payload does not contain a response file and has no triggers" do
70
+ context "when the payload does not contain a response file and has no triggered stubs" do
72
71
 
73
72
  let(:payload) { HttpStub::StubFixture.new.server_payload }
74
73
 
@@ -1,35 +1,59 @@
1
1
  describe HttpStub::Server::Stub::Registry, "integrating with real stubs" do
2
2
 
3
+ let(:memory_session) { HttpStub::Server::SessionFixture.memory }
4
+
3
5
  let(:logger) { instance_double(Logger, info: nil) }
4
6
 
5
- let(:stub_registry) { HttpStub::Server::Stub::Registry.new }
7
+ let(:stub_registry) { HttpStub::Server::Stub::Registry.new(memory_session) }
8
+
9
+ shared_context "a stub is added" do
10
+
11
+ let(:added_stub) { create_stub(4) }
12
+
13
+ before(:example) { stub_registry.add(added_stub, logger) }
6
14
 
7
- describe "#recall" do
15
+ end
16
+
17
+ describe "#reset" do
8
18
 
9
- subject { stub_registry.recall }
19
+ subject { stub_registry.reset(logger) }
10
20
 
11
- context "when stubs have been added" do
21
+ context "when stubs have been added to the servers memory" do
12
22
 
13
23
  let(:stubs) { (1..3).map { |i| create_stub(i) } }
14
24
 
15
- before(:example) { stubs.each { |stub| stub_registry.add(stub, logger) } }
25
+ before(:example) { stubs.each { |stub| memory_session.add_stub(stub, logger) } }
16
26
 
17
- context "and remembered" do
27
+ context "and a stub is subsequently added" do
18
28
 
19
- before(:example) { stub_registry.remember }
29
+ include_context "a stub is added"
20
30
 
21
- context "and a stub subsequently added" do
31
+ it "retains stubs added to the servers memory" do
32
+ subject
33
+
34
+ expect(stub_registry.all).to eql(stubs)
35
+ end
36
+
37
+ it "rejects stubs not added to the servers memory" do
38
+ subject
39
+
40
+ expect(stub_registry.all).not_to include(added_stub)
41
+ end
42
+
43
+ end
44
+
45
+ end
22
46
 
23
- let(:stub_to_add) { create_stub(4) }
47
+ context "when no stubs have been added to the servers memory" do
24
48
 
25
- before(:example) { stub_registry.add(stub_to_add, logger) }
49
+ context "and a stub is subsequently added" do
26
50
 
27
- it "should restore all known stubs to the remembered state" do
28
- subject
51
+ include_context "a stub is added"
29
52
 
30
- expect(stub_registry.all).not_to include(stub_to_add)
31
- end
53
+ it "clears the stubs" do
54
+ subject
32
55
 
56
+ expect(stub_registry.all).to eql([])
33
57
  end
34
58
 
35
59
  end
@@ -48,7 +72,10 @@ describe HttpStub::Server::Stub::Registry, "integrating with real stubs" do
48
72
  "status" => 200 + number,
49
73
  "body" => "Body #{number}"
50
74
  },
51
- "triggers" => []
75
+ "triggers" => {
76
+ "scenario_names" => [],
77
+ "stubs" => []
78
+ }
52
79
  )
53
80
  end
54
81
 
@@ -1,13 +1,21 @@
1
1
  describe HttpStub::Server::Stub::Registry do
2
2
 
3
- let(:underlying_stub_registry) { instance_double(HttpStub::Server::Registry) }
3
+ let(:stubs_in_memory) { (1..3).map { instance_double(HttpStub::Server::Stub::Stub) } }
4
+ let(:memory_session) { instance_double(HttpStub::Server::Session::Session, stubs: stubs_in_memory) }
4
5
 
5
- let(:logger) { instance_double(Logger) }
6
+ let(:underlying_stub_registry) { instance_double(HttpStub::Server::Registry) }
7
+ let(:logger) { instance_double(Logger) }
6
8
 
7
- let(:stub_registry) { HttpStub::Server::Stub::Registry.new }
9
+ let(:stub_registry) { described_class.new(memory_session) }
8
10
 
9
11
  before(:example) { allow(HttpStub::Server::Registry).to receive(:new).and_return(underlying_stub_registry) }
10
12
 
13
+ it "uses an underlying simple registry that is initialised with the memory sessions stubs" do
14
+ expect(HttpStub::Server::Registry).to receive(:new).with("stub", stubs_in_memory)
15
+
16
+ stub_registry
17
+ end
18
+
11
19
  describe "#add" do
12
20
 
13
21
  let(:stub) { instance_double(HttpStub::Server::Stub::Stub) }
@@ -24,7 +32,7 @@ describe HttpStub::Server::Stub::Registry do
24
32
 
25
33
  describe "#concat" do
26
34
 
27
- let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Stub) } }
35
+ let(:stubs) { (1..3).map { instance_double(HttpStub::Server::Stub::Stub) } }
28
36
 
29
37
  subject { stub_registry.concat(stubs, logger) }
30
38
 
@@ -40,58 +48,42 @@ describe HttpStub::Server::Stub::Registry do
40
48
 
41
49
  let(:id) { SecureRandom.uuid }
42
50
 
51
+ let(:found_stub) { instance_double(HttpStub::Server::Stub::Stub) }
52
+
43
53
  subject { stub_registry.find(id, logger) }
44
54
 
55
+ before(:example) { allow(underlying_stub_registry).to receive(:find).and_return(found_stub) }
56
+
45
57
  it "delegates to an underlying simple registry" do
46
58
  expect(underlying_stub_registry).to receive(:find).with(id, logger)
47
59
 
48
60
  subject
49
61
  end
50
62
 
63
+ it "returns any found stub" do
64
+ expect(subject).to eql(found_stub)
65
+ end
66
+
51
67
  end
52
68
 
53
69
  describe "#match" do
54
70
 
55
- let(:request) { HttpStub::Server::RequestFixture.create }
71
+ let(:request) { instance_double(HttpStub::Server::Request::Request) }
56
72
 
57
- let(:triggers) { instance_double(HttpStub::Server::Stub::Triggers, add_to: nil) }
58
- let(:stub) { instance_double(HttpStub::Server::Stub::Stub, triggers: triggers) }
59
- let(:found_stub) { nil }
73
+ let(:matched_stub) { instance_double(HttpStub::Server::Stub::Stub) }
60
74
 
61
- subject { stub_registry.match(request, logger) }
75
+ subject { stub_registry.find(request, logger) }
62
76
 
63
- before(:example) { allow(underlying_stub_registry).to receive(:find).and_return(found_stub) }
77
+ before(:example) { allow(underlying_stub_registry).to receive(:find).and_return(matched_stub) }
64
78
 
65
- it "finds a matching stub in the underlying simple registry based on the request" do
79
+ it "finds a matching stub in an underlying simple registry" do
66
80
  expect(underlying_stub_registry).to receive(:find).with(request, logger)
67
81
 
68
82
  subject
69
83
  end
70
84
 
71
- context "when a stub is found" do
72
-
73
- let(:found_stub) { stub }
74
-
75
- it "adds the stubs triggers to the underlying stub registry" do
76
- expect(triggers).to receive(:add_to).with(stub_registry, logger)
77
-
78
- subject
79
- end
80
-
81
- it "returns the stub found in the underlying stub registry" do
82
- expect(subject).to eql(stub)
83
- end
84
-
85
- end
86
-
87
- context "when a stub is not found" do
88
-
89
- let(:found_stub) { nil }
90
-
91
- it "returns nil" do
92
- expect(subject).to eql(nil)
93
- end
94
-
85
+ it "returns any found stub" do
86
+ expect(subject).to eql(matched_stub)
95
87
  end
96
88
 
97
89
  end
@@ -102,6 +94,8 @@ describe HttpStub::Server::Stub::Registry do
102
94
 
103
95
  subject { stub_registry.all }
104
96
 
97
+ before(:example) { allow(underlying_stub_registry).to receive(:all).and_return(stubs) }
98
+
105
99
  it "delegates to an underlying simple registry" do
106
100
  expect(underlying_stub_registry).to receive(:all)
107
101
 
@@ -109,42 +103,27 @@ describe HttpStub::Server::Stub::Registry do
109
103
  end
110
104
 
111
105
  it "returns the result from the underlying registry" do
112
- allow(underlying_stub_registry).to receive(:all).and_return(stubs)
113
-
114
106
  expect(subject).to eql(stubs)
115
107
  end
116
108
 
117
109
  end
118
110
 
119
- describe "#recall" do
111
+ describe "#reset" do
120
112
 
121
- subject { stub_registry.recall }
113
+ subject { stub_registry.reset(logger) }
122
114
 
123
- context "when the state of the registry has been remembered" do
115
+ before(:example) { allow(underlying_stub_registry).to receive(:replace) }
124
116
 
125
- let(:last_stub_remembered) { instance_double(HttpStub::Server::Stub::Stub) }
126
-
127
- before(:example) do
128
- allow(underlying_stub_registry).to receive(:last).and_return(last_stub_remembered)
129
- stub_registry.remember
130
- end
131
-
132
- it "causes the underlying registry to rollback to the last stub added before the state was remembered" do
133
- expect(underlying_stub_registry).to receive(:rollback_to).with(last_stub_remembered)
134
-
135
- subject
136
- end
117
+ it "retrieves the stubs in the memory session" do
118
+ expect(memory_session).to receive(:stubs)
137
119
 
120
+ subject
138
121
  end
139
122
 
140
- context "when the state of the registry has not been remembered" do
141
-
142
- it "does not rollback the underlying registry" do
143
- expect(underlying_stub_registry).to_not receive(:rollback_to)
144
-
145
- subject
146
- end
123
+ it "replaces the stubs in the underlying registry with those in the memory session" do
124
+ expect(underlying_stub_registry).to receive(:replace).with(stubs_in_memory, logger)
147
125
 
126
+ subject
148
127
  end
149
128
 
150
129
  end
@@ -1,79 +1,138 @@
1
1
  describe HttpStub::Server::Stub::Triggers do
2
2
 
3
- let(:trigger_bodies) { (1..3).map { |i| "trigger body #{i}" } }
4
- let(:stubs_for_triggers) { (1..trigger_bodies.length).map { instance_double(HttpStub::Server::Stub::Stub) } }
5
- let(:stub_triggers) { HttpStub::Server::Stub::Triggers.new(trigger_bodies) }
3
+ let(:scenario_names) { (1..3).map { |i| "Scenario trigger #{i}" } }
4
+ let(:stub_payloads) { (1..3).map { |i| "Stub trigger payload #{i}" } }
5
+ let(:args) { { "scenario_names" => scenario_names, "stubs" => stub_payloads } }
6
6
 
7
- before(:example) { allow(HttpStub::Server::Stub).to receive(:create).and_return(*stubs_for_triggers) }
7
+ let(:stubs) { (1..stub_payloads.length).map { instance_double(HttpStub::Server::Stub::Stub) } }
8
+
9
+ let(:triggers) { described_class.new(args) }
10
+
11
+ before(:example) { allow(HttpStub::Server::Stub).to receive(:create).and_return(*stubs) }
12
+
13
+ describe "::EMPTY" do
14
+
15
+ subject { HttpStub::Server::Stub::Triggers::EMPTY }
16
+
17
+ it "returns triggers with empty scenario names" do
18
+ expect(subject.scenario_names).to eql([])
19
+ end
20
+
21
+ it "returns triggers with empty stubs" do
22
+ expect(subject.stubs).to eql([])
23
+ end
24
+
25
+ end
8
26
 
9
27
  describe "constructor" do
10
28
 
11
- it "creates a stub for each provided trigger" do
12
- trigger_bodies.zip(stubs_for_triggers).each do |body, stub|
13
- expect(HttpStub::Server::Stub).to receive(:create).with(body).and_return(stub)
29
+ subject { described_class.new(args) }
30
+
31
+ context "when scenario names are provided" do
32
+
33
+ let(:args) { { "scenario_names" => scenario_names } }
34
+
35
+ it "establishes the scenario names" do
36
+ expect(subject.scenario_names).to eql(scenario_names)
14
37
  end
15
38
 
16
- stub_triggers
17
39
  end
18
40
 
19
- end
41
+ context "when scenario names are not provided" do
20
42
 
21
- describe "#add_to" do
43
+ let(:args) { {} }
22
44
 
23
- let(:registry) { instance_double(HttpStub::Server::Stub::Registry) }
24
- let(:logger) { instance_double(Logger) }
45
+ it "establishes an empty scenario name array" do
46
+ expect(subject.scenario_names).to eql([])
47
+ end
48
+
49
+ end
25
50
 
26
- it "adds each stub to the provided registry" do
27
- stubs_for_triggers.each { |stub_for_trigger| expect(registry).to receive(:add).with(stub_for_trigger, logger) }
51
+ context "when stubs are provided" do
52
+
53
+ let(:args) { { "stubs" => stub_payloads } }
54
+
55
+ it "creates a stub for each provided stub payload" do
56
+ stub_payloads.each { |payload| expect(HttpStub::Server::Stub).to receive(:create).with(payload) }
57
+
58
+ subject
59
+ end
60
+
61
+ it "estalishes the created stubs" do
62
+ expect(subject.stubs).to eql(stubs)
63
+ end
28
64
 
29
- stub_triggers.add_to(registry, logger)
30
65
  end
31
66
 
32
- end
67
+ context "when stubs are not provided" do
68
+
69
+ let(:args) { {} }
33
70
 
34
- describe "#each" do
71
+ it "establishes an empty stub array" do
72
+ expect(subject.stubs).to eql([])
73
+ end
74
+
75
+ end
35
76
 
36
- it "yields to the stubs for each trigger" do
37
- yielded_stubs = stub_triggers.each.map.to_a
77
+ context "when nil args are provided" do
78
+
79
+ let(:args) { nil }
80
+
81
+ it "establishes an empty scenario name array" do
82
+ expect(subject.scenario_names).to eql([])
83
+ end
84
+
85
+ it "establishes an empty stub array" do
86
+ expect(subject.stubs).to eql([])
87
+ end
38
88
 
39
- expect(yielded_stubs).to eql(stubs_for_triggers)
40
89
  end
41
90
 
42
91
  end
43
92
 
44
93
  describe "#to_json" do
45
94
 
46
- let(:json_for_stubs) { (1..stubs_for_triggers.length).map { |i| "stub #{i} json" } }
95
+ let(:json_for_stubs) { stubs.each_with_index.map { |i| "stub #{i} JSON" } }
47
96
  let(:quoted_json_for_stubs) { json_for_stubs.map(&:to_json) }
48
97
 
49
- subject { stub_triggers.to_json }
98
+ subject { triggers.to_json }
50
99
 
51
100
  before(:example) do
52
- stubs_for_triggers.zip(quoted_json_for_stubs).each do |stub, json|
53
- allow(stub).to receive(:to_json).and_return(json)
54
- end
101
+ stubs.zip(quoted_json_for_stubs).each { |stub, json| allow(stub).to receive(:to_json).and_return(json) }
102
+ end
103
+
104
+ it "contains the scenario names" do
105
+ expect(subject).to include_in_json(scenario_names: scenario_names)
55
106
  end
56
107
 
57
- it "returns a JSON array containing the JSON representation of each stub" do
58
- expect(subject).to eql(json_for_stubs.to_json)
108
+ it "contains the JSON representation of each stub" do
109
+ expect(subject).to include_in_json(stubs: json_for_stubs)
59
110
  end
60
111
 
61
112
  end
62
113
 
63
114
  describe "#to_s" do
64
115
 
65
- let(:trigger_strings) { (1..stubs_for_triggers.length).map { |i| "trigger string ##{i}" } }
116
+ subject { triggers.to_s }
66
117
 
67
- before(:example) do
68
- stubs_for_triggers.zip(trigger_strings).each do |stub_for_trigger, string|
69
- allow(stub_for_trigger).to receive(:to_s).and_return(string)
118
+ context "when scenario names and stubs are provided" do
119
+
120
+ let(:args) { { "scenario_names" => scenario_names, "stubs" => stubs } }
121
+
122
+ it "returns a string representation of the arguments provided to the triggers" do
123
+ expect(subject).to eql(args.to_s)
70
124
  end
125
+
71
126
  end
72
127
 
73
- it "should contain a string representation of each trigger" do
74
- result = stub_triggers.to_s
128
+ context "when scenario names and stubs are not provided" do
129
+
130
+ let(:args) { {} }
131
+
132
+ it "returns a string indicating the scenario names and stub are empty" do
133
+ expect(subject).to eql({ "scenario_names" => [], "stubs" => [] }.to_s)
134
+ end
75
135
 
76
- trigger_strings.each { |trigger_string| expect(result).to include(trigger_string) }
77
136
  end
78
137
 
79
138
  end