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
@@ -0,0 +1,167 @@
1
+ describe HttpStub::Configurer::DSL::ServerEndpointTemplate do
2
+
3
+ let(:server) { instance_double(HttpStub::Configurer::DSL::Server) }
4
+ let(:session_endpoint_template) { instance_double(HttpStub::Configurer::DSL::SessionEndpointTemplate) }
5
+ let(:active_session) do
6
+ instance_double(HttpStub::Configurer::DSL::Session, endpoint_template: session_endpoint_template)
7
+ end
8
+ let(:initial_block_verifier) { double("BlockVerifier") }
9
+ let(:block) { lambda { initial_block_verifier.verify } }
10
+
11
+ let(:server_endpoint_template) { described_class.new(server, active_session, &block) }
12
+
13
+ before(:each) { }
14
+
15
+ it "creates a session template in the active session that is initialized with the provided block" do
16
+ expect(active_session).to receive(:endpoint_template).and_yield
17
+ expect(initial_block_verifier).to receive(:verify)
18
+
19
+ server_endpoint_template
20
+ end
21
+
22
+ describe "#match_requests" do
23
+
24
+ let(:request_matching_rules) { { uri: "/some/stub/uri", method: :get } }
25
+
26
+ subject { server_endpoint_template.match_requests(request_matching_rules) }
27
+
28
+ it "delegates to the session template" do
29
+ expect(session_endpoint_template).to receive(:match_requests).with(request_matching_rules)
30
+
31
+ subject
32
+ end
33
+
34
+ end
35
+
36
+ describe "#schema" do
37
+
38
+ let(:type) { :some_schema_type }
39
+ let(:definition) { "some schema definition" }
40
+
41
+ subject { server_endpoint_template.schema(type, definition) }
42
+
43
+ it "delegates to the session template" do
44
+ expect(session_endpoint_template).to receive(:schema).with(type, definition)
45
+
46
+ subject
47
+ end
48
+
49
+ it "returns the created schema" do
50
+ schema = { schema: :some_schema }
51
+ expect(session_endpoint_template).to receive(:schema).and_return(schema)
52
+
53
+ expect(subject).to eql(schema)
54
+ end
55
+
56
+ end
57
+
58
+ describe "#respond_with" do
59
+
60
+ let(:response_settings) { { status: 204 } }
61
+
62
+ subject { server_endpoint_template.respond_with(response_settings) }
63
+
64
+ it "delegates to the session templates" do
65
+ expect(session_endpoint_template).to receive(:respond_with).with(response_settings)
66
+
67
+ subject
68
+ end
69
+
70
+ end
71
+
72
+ describe "#trigger" do
73
+
74
+ let(:trigger_settings) { { scenario: "some scenario name" } }
75
+
76
+ subject { server_endpoint_template.trigger(trigger_settings) }
77
+
78
+ it "delegates to the session template" do
79
+ expect(session_endpoint_template).to receive(:trigger).with(trigger_settings)
80
+
81
+ subject
82
+ end
83
+
84
+ end
85
+
86
+ describe "#invoke" do
87
+
88
+ let(:block_verifier) { double("BlockVerifier") }
89
+ let(:block) { lambda { block_verifier.verify } }
90
+
91
+ subject { server_endpoint_template.invoke(&block) }
92
+
93
+ it "delegates to the session template" do
94
+ expect(session_endpoint_template).to receive(:invoke).and_yield
95
+ expect(block_verifier).to receive(:verify)
96
+
97
+ subject
98
+ end
99
+
100
+ end
101
+
102
+ describe "#build_stub" do
103
+
104
+ let(:response_overrides) { { status: 201 } }
105
+ let(:block_verifier) { double("BlockVerifier") }
106
+ let(:block) { lambda { block_verifier.verify } }
107
+
108
+ subject { server_endpoint_template.build_stub(response_overrides, &block) }
109
+
110
+ it "delegates to the session template" do
111
+ expect(session_endpoint_template).to receive(:build_stub).with(response_overrides).and_yield
112
+ expect(block_verifier).to receive(:verify)
113
+
114
+ subject
115
+ end
116
+
117
+ end
118
+
119
+ describe "#add_stub!" do
120
+
121
+ let(:response_overrides) { { status: 201 } }
122
+ let(:block_verifier) { double("BlockVerifier") }
123
+ let(:block) { lambda { block_verifier.verify } }
124
+
125
+ subject { server_endpoint_template.add_stub!(response_overrides, &block) }
126
+
127
+ it "delegates to the session template" do
128
+ expect(session_endpoint_template).to receive(:add_stub!).with(response_overrides).and_yield
129
+ expect(block_verifier).to receive(:verify)
130
+
131
+ subject
132
+ end
133
+
134
+ end
135
+
136
+ describe "#add_scenario!" do
137
+
138
+ let(:name) { "Some scenario name" }
139
+ let(:response_overrides) { { status: 201 } }
140
+ let(:block_verifier) { double("BlockVerifier") }
141
+ let(:block) { lambda { block_verifier.verify } }
142
+
143
+ let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
144
+
145
+ subject { server_endpoint_template.add_scenario!(name, response_overrides, &block) }
146
+
147
+ before(:example) do
148
+ allow(session_endpoint_template).to receive(:build_stub).and_return(stub_builder)
149
+ allow(server).to receive(:add_scenario_with_one_stub!)
150
+ end
151
+
152
+ it "builds a stub using any provided response overrides and block" do
153
+ expect(session_endpoint_template).to receive(:build_stub).with(response_overrides).and_yield
154
+ expect(block_verifier).to receive(:verify)
155
+
156
+ subject
157
+ end
158
+
159
+ it "adds a one stub scenario to the server with the provided name and created stub builder" do
160
+ expect(server).to receive(:add_scenario_with_one_stub!).with(name, stub_builder)
161
+
162
+ subject
163
+ end
164
+
165
+ end
166
+
167
+ end
@@ -1,12 +1,49 @@
1
1
  describe HttpStub::Configurer::DSL::Server do
2
2
 
3
- let(:server_facade) { instance_double(HttpStub::Configurer::Server::Facade) }
4
- let(:default_stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
3
+ let(:configuration) { instance_double(HttpStub::Configurer::Server::Configuration) }
4
+ let(:server_facade) { instance_double(HttpStub::Configurer::Server::Facade) }
5
+ let(:default_stub_template) { instance_double(HttpStub::Configurer::DSL::StubBuilderTemplate) }
6
+ let(:memory_session) { instance_double(HttpStub::Configurer::DSL::Session) }
7
+ let(:transactional_session) { instance_double(HttpStub::Configurer::DSL::Session) }
8
+ let(:session_factory) do
9
+ instance_double(HttpStub::Configurer::DSL::SessionFactory, memory: memory_session,
10
+ transactional: transactional_session)
11
+ end
12
+
13
+ let(:default_session) { memory_session }
14
+ let(:block_verifier) { double("BlockVerifier") }
5
15
 
6
- let(:server) { HttpStub::Configurer::DSL::Server.new(server_facade) }
16
+ let(:server) { described_class.new }
7
17
 
8
18
  before(:example) do
9
- allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).with(no_args).and_return(default_stub_builder)
19
+ allow(HttpStub::Configurer::Server::Configuration).to receive(:new).and_return(configuration)
20
+ allow(HttpStub::Configurer::Server::Facade).to receive(:new).and_return(server_facade)
21
+ allow(HttpStub::Configurer::DSL::StubBuilderTemplate).to receive(:new).and_return(default_stub_template)
22
+ allow(HttpStub::Configurer::DSL::SessionFactory).to receive(:new).and_return(session_factory)
23
+ end
24
+
25
+ it "creates a server configuration" do
26
+ expect(HttpStub::Configurer::Server::Configuration).to receive(:new)
27
+
28
+ server
29
+ end
30
+
31
+ it "creates a facade to interact with the stub server based on the configuration" do
32
+ expect(HttpStub::Configurer::Server::Facade).to receive(:new).with(configuration)
33
+
34
+ server
35
+ end
36
+
37
+ it "creates a stub builder template to hold the default stub settings for the server" do
38
+ expect(HttpStub::Configurer::DSL::StubBuilderTemplate).to receive(:new).with(no_args)
39
+
40
+ server
41
+ end
42
+
43
+ it "creates a session factory that creates sessions interacting with the servers facade and stub template" do
44
+ expect(HttpStub::Configurer::DSL::SessionFactory).to receive(:new).with(server_facade, default_stub_template)
45
+
46
+ server
10
47
  end
11
48
 
12
49
  shared_context "a server with host and port configured" do
@@ -18,121 +55,142 @@ describe HttpStub::Configurer::DSL::Server do
18
55
 
19
56
  end
20
57
 
21
- it "produces stub builders" do
22
- expect(server).to be_a(HttpStub::Configurer::DSL::StubBuilderProducer)
23
- end
58
+ describe "#host" do
59
+
60
+ let(:host) { "some_host" }
61
+
62
+ subject { server.host }
63
+
64
+ it "delegates to the servers configuration" do
65
+ allow(configuration).to receive(:host).and_return(host)
66
+
67
+ expect(subject).to eql(host)
68
+ end
24
69
 
25
- it "activates scenarios" do
26
- expect(server).to be_a(HttpStub::Configurer::DSL::ScenarioActivator)
27
70
  end
28
71
 
29
- describe "#base_uri" do
72
+ describe "#host=" do
30
73
 
31
- subject { server.base_uri }
74
+ let(:host) { "some_host" }
32
75
 
33
- include_context "a server with host and port configured"
76
+ subject { server.host = host }
34
77
 
35
- it "returns a uri that combines any established host and port" do
36
- expect(subject).to include("some_host:8888")
37
- end
78
+ it "delegates to the servers configuration" do
79
+ allow(configuration).to receive(:host=).with(host)
38
80
 
39
- it "returns a uri accessed via http" do
40
- expect(subject).to match(/^http:\/\//)
81
+ subject
41
82
  end
42
83
 
43
84
  end
44
85
 
45
- describe "#external_base_uri" do
86
+ describe "#port" do
46
87
 
47
- subject { server.external_base_uri }
88
+ let(:port) { "8888" }
48
89
 
49
- include_context "a server with host and port configured"
90
+ subject { server.port }
50
91
 
51
- context "when an external base URI environment variable is established" do
92
+ it "delegates to the servers configuration" do
93
+ allow(configuration).to receive(:port).and_return(port)
52
94
 
53
- let(:external_base_uri) { "http://some/external/base/uri" }
95
+ expect(subject).to eql(port)
96
+ end
54
97
 
55
- before(:example) { ENV["STUB_EXTERNAL_BASE_URI"] = external_base_uri }
56
- after(:example) { ENV["STUB_EXTERNAL_BASE_URI"] = nil }
98
+ end
57
99
 
58
- it "returns the environment variable" do
59
- expect(subject).to eql(external_base_uri)
60
- end
100
+ describe "#port=" do
61
101
 
62
- end
102
+ let(:port) { "8888" }
63
103
 
64
- context "when an external base URI environment variable is not established" do
104
+ subject { server.port = port }
65
105
 
66
- it "returns the base URI" do
67
- expect(subject).to eql(server.base_uri)
68
- end
106
+ it "delegates to the servers configuration" do
107
+ allow(configuration).to receive(:port=).with(port)
69
108
 
109
+ subject
70
110
  end
71
111
 
72
- it "returns a uri that combines any established host and port" do
73
- expect(subject).to include("some_host:8888")
74
- end
112
+ end
113
+
114
+ describe "#base_uri" do
115
+
116
+ let(:base_uri) { "some_host:8888" }
75
117
 
76
- it "returns a uri accessed via http" do
77
- expect(subject).to match(/^http:\/\//)
118
+ subject { server.base_uri }
119
+
120
+ it "delegates to the servers configuration" do
121
+ allow(configuration).to receive(:base_uri).and_return(base_uri)
122
+
123
+ expect(subject).to eql(base_uri)
78
124
  end
79
125
 
80
126
  end
81
127
 
82
- describe "#enable" do
128
+ describe "#external_base_uri" do
83
129
 
84
- context "when a feature is provided" do
130
+ let(:external_base_uri) { "http://some/external/base/uri" }
85
131
 
86
- let(:feature) { :some_feature }
132
+ subject { server.external_base_uri }
87
133
 
88
- subject { server.enable(feature) }
134
+ it "delegates to the servers configuration" do
135
+ allow(configuration).to receive(:external_base_uri).and_return(external_base_uri)
89
136
 
90
- it "enables the feature" do
91
- subject
137
+ expect(subject).to eql(external_base_uri)
138
+ end
92
139
 
93
- expect(server.enabled?(feature)).to be(true)
94
- end
140
+ end
141
+
142
+ describe "#session_identifier" do
143
+
144
+ let(:session_identifier) { { headers: :some_session_identifier } }
145
+
146
+ subject { server.session_identifier }
95
147
 
148
+ it "delegates to the servers configuration" do
149
+ allow(configuration).to receive(:session_identifier).and_return(session_identifier)
150
+
151
+ expect(subject).to eql(session_identifier)
96
152
  end
97
153
 
98
- context "when features are provided" do
154
+ end
99
155
 
100
- let(:features) { (1..3).map { |i| "feature_#{i}".to_sym } }
156
+ describe "#session_identifer=" do
101
157
 
102
- subject { server.enable(*features) }
158
+ let(:session_identifier) { { headers: :some_session_identifier } }
103
159
 
104
- it "enables the features" do
105
- subject
160
+ subject { server.session_identifier = session_identifier }
106
161
 
107
- features.each { |feature| expect(server.enabled?(feature)).to be(true) }
108
- end
162
+ it "delegates to the servers configuration" do
163
+ expect(configuration).to receive(:session_identifier=).with(session_identifier)
109
164
 
165
+ subject
110
166
  end
111
167
 
112
168
  end
113
169
 
114
- describe "#enabled?" do
170
+ describe "#enable" do
115
171
 
116
- let(:feature) { :some_feature }
172
+ let(:features) { (1..3).map { |i| "feature_#{i}".to_sym } }
117
173
 
118
- subject { server.enabled?(feature) }
174
+ subject { server.enable(*features) }
119
175
 
120
- context "when a feature is enabled" do
176
+ it "delegates to the sessions configuration" do
177
+ expect(configuration).to receive(:enable).with(*features)
121
178
 
122
- before(:example) { server.enable(feature) }
179
+ subject
180
+ end
123
181
 
124
- it "returns false" do
125
- expect(subject).to be(true)
126
- end
182
+ end
127
183
 
128
- end
184
+ describe "#enabled?" do
185
+
186
+ let(:feature) { :some_feature }
129
187
 
130
- context "when a feature is not enabled" do
188
+ subject { server.enabled?(feature) }
131
189
 
132
- it "returns false" do
133
- expect(subject).to be(false)
134
- end
190
+ it "delegates to the servers configuration" do
191
+ expect(configuration).to receive(:enabled?).and_return(true)
135
192
 
193
+ expect(subject).to be(true)
136
194
  end
137
195
 
138
196
  end
@@ -141,10 +199,12 @@ describe HttpStub::Configurer::DSL::Server do
141
199
 
142
200
  let(:args) { { request_rule_key: "request rule value" } }
143
201
 
144
- it "establishes request matcher rules on the default stub builder" do
145
- expect(default_stub_builder).to receive(:match_requests).with(args)
202
+ subject { server.request_defaults = args }
146
203
 
147
- server.request_defaults = args
204
+ it "establishes request matching rules on the default stub template" do
205
+ expect(default_stub_template).to receive(:match_requests).with(args)
206
+
207
+ subject
148
208
  end
149
209
 
150
210
  end
@@ -153,82 +213,90 @@ describe HttpStub::Configurer::DSL::Server do
153
213
 
154
214
  let(:args) { { response_key: "response value" } }
155
215
 
156
- it "establishes response values on the default stub builder" do
157
- expect(default_stub_builder).to receive(:respond_with).with(args)
216
+ subject { server.response_defaults = args }
217
+
218
+ it "establishes response values on the default stub template" do
219
+ expect(default_stub_template).to receive(:respond_with).with(args)
158
220
 
159
- server.response_defaults = args
221
+ subject
160
222
  end
161
223
 
162
224
  end
163
225
 
164
- describe "#has_started!" do
165
-
166
- it "informs the facade that the server has started" do
167
- expect(server_facade).to receive(:server_has_started)
168
-
169
- server.has_started!
170
- end
226
+ it "causes session operations to be performed against the memory session prior to initialization" do
227
+ expect(memory_session).to receive(:activate!)
171
228
 
229
+ server.activate!("some scenario name")
172
230
  end
173
231
 
174
- describe "#add_stub!" do
232
+ describe "#initialize!" do
175
233
 
176
- let(:stub) { instance_double(HttpStub::Configurer::Request::Stub) }
177
- let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder, build: stub) }
234
+ subject { server.initialize! }
178
235
 
179
- before(:example) { allow(server_facade).to receive(:stub_response) }
236
+ before(:example) do
237
+ allow(server_facade).to receive(:flush_requests)
238
+ allow(transactional_session).to receive(:mark_as_default!)
239
+ end
180
240
 
181
- shared_examples_for "adding a stub request" do
241
+ it "flushes requests held by the server facade" do
242
+ expect(server_facade).to receive(:flush_requests)
182
243
 
183
- it "builds the stub request" do
184
- expect(stub_builder).to receive(:build).and_return(stub)
244
+ subject
245
+ end
185
246
 
186
- subject
187
- end
247
+ it "marks the transactional session as the servers default" do
248
+ expect(transactional_session).to receive(:mark_as_default!)
188
249
 
189
- it "informs the server facade of the stub request" do
190
- expect(server_facade).to receive(:stub_response).with(stub)
250
+ subject
251
+ end
191
252
 
192
- subject
193
- end
253
+ it "causes subsequent session operations to be performed against the transactional session" do
254
+ subject
255
+ expect(transactional_session).to receive(:activate!)
194
256
 
257
+ server.activate!("some scenario name")
195
258
  end
196
259
 
197
- context "when a stub builder is provided" do
260
+ end
198
261
 
199
- subject { server.add_stub!(stub_builder) }
262
+ describe "#has_started!" do
200
263
 
201
- it_behaves_like "adding a stub request"
264
+ subject { server.has_started! }
202
265
 
266
+ before(:example) do
267
+ allow(server_facade).to receive(:server_has_started)
268
+ allow(transactional_session).to receive(:mark_as_default!)
203
269
  end
204
270
 
205
- context "when a stub builder is not provided" do
271
+ it "informs the facade that the server has started" do
272
+ expect(server_facade).to receive(:server_has_started)
206
273
 
207
- let(:block_verifier) { double("BlockVerifier") }
208
- let(:block) { lambda { block_verifier.verify } }
274
+ subject
275
+ end
209
276
 
210
- before(:example) do
211
- allow(HttpStub::Configurer::DSL::StubBuilder).to receive(:new).and_return(stub_builder)
212
- allow(stub_builder).to receive(:invoke)
213
- end
277
+ it "marks the transactional session as the servers default" do
278
+ expect(transactional_session).to receive(:mark_as_default!)
214
279
 
215
- subject { server.add_stub!(&block) }
280
+ subject
281
+ end
216
282
 
217
- it "creates a stub builder" do
218
- expect(HttpStub::Configurer::DSL::StubBuilder).to receive(:new)
283
+ it "causes subsequent session operations to be performed against the transactional session" do
284
+ subject
285
+ expect(transactional_session).to receive(:activate!)
219
286
 
220
- subject
221
- end
287
+ server.activate!("some scenario name")
288
+ end
222
289
 
223
- it "requests the created builder invoke the provided block" do
224
- expect(stub_builder).to receive(:invoke).and_yield
225
- expect(block_verifier).to receive(:verify)
290
+ end
226
291
 
227
- subject
228
- end
292
+ describe "#reset!" do
293
+
294
+ subject { server.reset! }
229
295
 
230
- it_behaves_like "adding a stub request"
296
+ it "delegates to the server facade" do
297
+ expect(server_facade).to receive(:reset)
231
298
 
299
+ subject
232
300
  end
233
301
 
234
302
  end
@@ -236,27 +304,26 @@ describe HttpStub::Configurer::DSL::Server do
236
304
  describe "#add_scenario!" do
237
305
 
238
306
  let(:scenario_name) { "some/scenario/name" }
307
+ let(:block) { lambda { |_scenario| "some block" } }
308
+
239
309
  let(:scenario) { instance_double(HttpStub::Configurer::Request::Scenario) }
240
310
  let(:scenario_builder) { instance_double(HttpStub::Configurer::DSL::ScenarioBuilder, build: scenario) }
241
311
 
242
- before(:example) { allow(server_facade).to receive(:define_scenario) }
243
-
244
- let(:block) { lambda { |_scenario| "some block" } }
312
+ subject { server.add_scenario!(scenario_name, &block) }
245
313
 
246
314
  before(:example) do
247
315
  allow(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).and_return(scenario_builder)
316
+ allow(server_facade).to receive(:define_scenario)
248
317
  end
249
318
 
250
- subject { server.add_scenario!(scenario_name, &block) }
251
-
252
- it "creates a scenario builder containing the servers default stub builder" do
253
- expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(default_stub_builder, anything)
319
+ it "creates a scenario builder containing the provided scenario name" do
320
+ expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(scenario_name, anything)
254
321
 
255
322
  subject
256
323
  end
257
324
 
258
- it "creates a scenario builder containing the provided scenario name" do
259
- expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(anything, scenario_name)
325
+ it "creates a scenario builder extending the servers default stub template" do
326
+ expect(HttpStub::Configurer::DSL::ScenarioBuilder).to receive(:new).with(anything, default_stub_template)
260
327
 
261
328
  subject
262
329
  end
@@ -281,9 +348,8 @@ describe HttpStub::Configurer::DSL::Server do
281
348
 
282
349
  describe "#add_scenario_with_one_stub!" do
283
350
 
284
- let(:scenario_name) { "Some scenario name" }
285
- let(:block_verifier) { double("BlockVerifier") }
286
- let(:block) { lambda { block_verifier.verify } }
351
+ let(:scenario_name) { "Some scenario name" }
352
+ let(:block) { lambda { block_verifier.verify } }
287
353
 
288
354
  let(:scenario_builder) { instance_double(HttpStub::Configurer::DSL::ScenarioBuilder, add_stub!: nil) }
289
355
  let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder, invoke: nil) }
@@ -318,8 +384,7 @@ describe HttpStub::Configurer::DSL::Server do
318
384
 
319
385
  context "when a block is provided" do
320
386
 
321
- let(:block_verifier) { double("BlockVerifier") }
322
- let(:block) { lambda { block_verifier.verify } }
387
+ let(:block) { lambda { block_verifier.verify } }
323
388
 
324
389
  subject { server.add_scenario_with_one_stub!(scenario_name, &block) }
325
390
 
@@ -338,124 +403,189 @@ describe HttpStub::Configurer::DSL::Server do
338
403
 
339
404
  describe "#endpoint_template" do
340
405
 
341
- let(:block_verifier) { double("BlockVerifier") }
342
- let(:block) { lambda { block_verifier.verify } }
343
- let(:endpoint_template) { instance_double(HttpStub::Configurer::DSL::EndpointTemplate, invoke: nil) }
406
+ let(:block) { lambda { block_verifier.verify } }
407
+ let(:server_endpoint_template) { instance_double(HttpStub::Configurer::DSL::ServerEndpointTemplate) }
344
408
 
345
409
  subject { server.endpoint_template(&block) }
346
410
 
347
411
  before(:example) do
348
- allow(HttpStub::Configurer::DSL::EndpointTemplate).to receive(:new).and_return(endpoint_template)
412
+ allow(HttpStub::Configurer::DSL::ServerEndpointTemplate).to receive(:new).and_return(server_endpoint_template)
349
413
  end
350
414
 
351
- it "requests the endpoint template invoke the provided block" do
352
- expect(endpoint_template).to receive(:invoke).and_yield
353
- expect(block_verifier).to receive(:verify)
415
+ it "creates an endpoint template for the server" do
416
+ expect(HttpStub::Configurer::DSL::ServerEndpointTemplate).to receive(:new).with(server, anything)
354
417
 
355
418
  subject
356
419
  end
357
420
 
358
- it "creates a template for the server" do
359
- expect(HttpStub::Configurer::DSL::EndpointTemplate).to receive(:new).with(server).and_return(endpoint_template)
421
+ it "creates an endpoint template using the curently default session" do
422
+ expect(HttpStub::Configurer::DSL::ServerEndpointTemplate).to receive(:new).with(anything, default_session)
423
+
424
+ subject
425
+ end
426
+
427
+ it "creates an endpoint template using any provided block to initialize the template" do
428
+ allow(HttpStub::Configurer::DSL::ServerEndpointTemplate).to receive(:new).and_yield
429
+ expect(block_verifier).to receive(:verify)
360
430
 
361
431
  subject
362
432
  end
363
433
 
364
434
  it "returns the created endpoint template" do
365
- expect(subject).to eql(endpoint_template)
435
+ expect(subject).to eql(server_endpoint_template)
366
436
  end
367
437
 
368
438
  end
369
439
 
370
- describe "#add_activator!" do
440
+ describe "#session" do
371
441
 
372
- let(:scenario) { instance_double(HttpStub::Configurer::Request::Scenario) }
373
- let(:stub_activator_builder) do
374
- instance_double(HttpStub::Configurer::DSL::StubActivatorBuilder, build: scenario)
375
- end
442
+ let(:session_id) { "some session id" }
376
443
 
377
- before(:example) { allow(server_facade).to receive(:define_scenario) }
444
+ let(:session) { instance_double(HttpStub::Configurer::DSL::Session) }
378
445
 
379
- let(:block) { lambda { |_activator| "some block" } }
446
+ subject { server.session(session_id) }
380
447
 
381
- before(:example) do
382
- allow(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).and_return(stub_activator_builder)
448
+ before(:example) { allow(session_factory).to receive(:create).and_return(session) }
449
+
450
+ it "creates a DSL for interacting with a session for the provided ID via the session factory" do
451
+ expect(session_factory).to receive(:create).with(session_id)
452
+
453
+ subject
454
+ end
455
+
456
+ it "returns the created session" do
457
+ expect(subject).to eql(session)
383
458
  end
384
459
 
385
- subject { server.add_activator!(&block) }
460
+ end
461
+
462
+ describe "#activate!" do
386
463
 
387
- it "creates a stub activator builder containing the servers default stub builder" do
388
- expect(HttpStub::Configurer::DSL::StubActivatorBuilder).to receive(:new).with(default_stub_builder)
464
+ let(:scenario_name) { "Some scenario name" }
465
+
466
+ subject { server.activate!(scenario_name) }
467
+
468
+ it "delegates to the default session" do
469
+ expect(default_session).to receive(:activate!).with(scenario_name)
389
470
 
390
471
  subject
391
472
  end
392
473
 
393
- it "yields the created builder to the provided block" do
394
- expect { |block| server.add_activator!(&block) }.to yield_with_args(stub_activator_builder)
474
+ end
475
+
476
+ describe "#clear_sessions!" do
477
+
478
+ subject { server.clear_sessions! }
479
+
480
+ it "delegates to the server facade" do
481
+ expect(server_facade).to receive(:clear_sessions)
482
+
483
+ subject
395
484
  end
396
485
 
397
- it "builds a scenario request" do
398
- expect(stub_activator_builder).to receive(:build).and_return(scenario)
486
+ end
487
+
488
+ describe "#build_stub" do
489
+
490
+ let(:block) { lambda { block_verifier.verify } }
491
+
492
+ subject { server.build_stub(&block) }
493
+
494
+ it "delegates to the default session" do
495
+ expect(default_session).to receive(:build_stub)
399
496
 
400
497
  subject
401
498
  end
402
499
 
403
- it "informs the server facade to submit the scenario request" do
404
- expect(server_facade).to receive(:define_scenario).with(scenario)
500
+ it "initializes the built stub using the provided block" do
501
+ allow(default_session).to receive(:build_stub).and_yield
502
+ expect(block_verifier).to receive(:verify)
405
503
 
406
504
  subject
407
505
  end
408
506
 
507
+ it "returns the built stub" do
508
+ built_stub = instance_double(HttpStub::Configurer::DSL::StubBuilder)
509
+ allow(default_session).to receive(:build_stub).and_return(built_stub)
510
+
511
+ expect(subject).to eql(built_stub)
512
+ end
513
+
409
514
  end
410
515
 
411
- describe "#activate!" do
516
+ describe "#add_stub!" do
412
517
 
413
- let(:scenario_name) { "/some/scenario/name" }
518
+ context "when a stub builder is provided" do
414
519
 
415
- it "delegates to the server facade" do
416
- expect(server_facade).to receive(:activate).with(scenario_name)
520
+ let(:stub_builder) { instance_double(HttpStub::Configurer::DSL::StubBuilder) }
521
+
522
+ subject { server.add_stub!(stub_builder) }
523
+
524
+ it "delegates to the default session" do
525
+ expect(default_session).to receive(:add_stub!).with(stub_builder)
526
+
527
+ subject
528
+ end
417
529
 
418
- server.activate!(scenario_name)
419
530
  end
420
531
 
421
- end
532
+ context "when a block is provided" do
422
533
 
423
- describe "#remember_stubs" do
534
+ let(:block) { lambda { block_verifier.verify } }
424
535
 
425
- it "delegates to the server facade" do
426
- expect(server_facade).to receive(:remember_stubs)
536
+ subject { server.add_stub!(&block) }
537
+
538
+ it "delegates to the default session" do
539
+ expect(default_session).to receive(:add_stub!)
540
+
541
+ subject
542
+ end
543
+
544
+ it "supplies the provided block to the session" do
545
+ allow(default_session).to receive(:add_stub!).and_yield
546
+ expect(block_verifier).to receive(:verify)
547
+
548
+ subject
549
+ end
427
550
 
428
- server.remember_stubs
429
551
  end
430
552
 
431
553
  end
432
554
 
433
- describe "#recall_stubs!" do
555
+ describe "#add_stubs!" do
434
556
 
435
- it "delegates to the server facade" do
436
- expect(server_facade).to receive(:recall_stubs)
557
+ let(:stub_builders) { (1..3).map { instance_double(HttpStub::Configurer::DSL::StubBuilder) } }
437
558
 
438
- server.recall_stubs!
559
+ subject { server.add_stubs!(stub_builders) }
560
+
561
+ it "delegates to the default session" do
562
+ expect(default_session).to receive(:add_stubs!).with(stub_builders)
563
+
564
+ subject
439
565
  end
440
566
 
441
567
  end
442
568
 
443
- describe "#clear_stubs!" do
569
+ describe "#recall_stubs!" do
444
570
 
445
- it "delegates to the server facade" do
446
- expect(server_facade).to receive(:clear_stubs)
571
+ subject { server.recall_stubs! }
447
572
 
448
- server.clear_stubs!
573
+ it "resets the default session" do
574
+ expect(default_session).to receive(:reset!)
575
+
576
+ subject
449
577
  end
450
578
 
451
579
  end
452
580
 
453
- describe "#clear_scenarios!" do
581
+ describe "#clear_stubs!" do
582
+
583
+ subject { server.clear_stubs! }
454
584
 
455
- it "delegates to the server facade" do
456
- expect(server_facade).to receive(:clear_scenarios)
585
+ it "delegates to the default session" do
586
+ expect(default_session).to receive(:clear!)
457
587
 
458
- server.clear_scenarios!
588
+ subject
459
589
  end
460
590
 
461
591
  end