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,26 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module DSL
4
+
5
+ class StubBuilderTemplate
6
+
7
+ attr_reader :template
8
+
9
+ delegate :match_requests, :schema, :respond_with, :trigger, :invoke, to: :@template
10
+
11
+ def initialize(parent_template=nil, &block)
12
+ @template = HttpStub::Configurer::DSL::StubBuilder.new(parent_template.try(:template), &block)
13
+ end
14
+
15
+ def build_stub(response_overrides={}, &block)
16
+ HttpStub::Configurer::DSL::StubBuilder.new(@template) do |stub|
17
+ stub.respond_with(response_overrides)
18
+ stub.invoke(&block) if block_given?
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -5,12 +5,18 @@ module HttpStub
5
5
 
6
6
  class Basic
7
7
 
8
- def initialize(http_request)
9
- @http_request = http_request
8
+ def initialize(args)
9
+ @request_method = args[:method]
10
+ @path = args[:path]
11
+ @headers = args[:headers] || {}
12
+ @parameters = args[:parameters]
10
13
  end
11
14
 
12
15
  def to_http_request
13
- @http_request
16
+ http_request_class = Net::HTTP.const_get(@request_method.to_s.capitalize)
17
+ http_request_class.new("/http_stub/#{@path}", @headers.stringify_keys).tap do |http_request|
18
+ http_request.set_form_data(@parameters) if @parameters
19
+ end
14
20
  end
15
21
 
16
22
  end
@@ -7,29 +7,20 @@ module HttpStub
7
7
 
8
8
  class << self
9
9
 
10
- def multipart(model)
11
- HttpStub::Configurer::Request::Http::Multipart.new(model)
10
+ def multipart(path, model, headers={})
11
+ HttpStub::Configurer::Request::Http::Multipart.new(path: path, headers: headers, model: model)
12
12
  end
13
13
 
14
- def get(path)
15
- create_basic_request(:get, path.start_with?("/") ? path : "/#{path}")
14
+ def get(path, headers={})
15
+ HttpStub::Configurer::Request::Http::Basic.new(method: :get, path: path, headers: headers)
16
16
  end
17
17
 
18
18
  def post(path, parameters={})
19
- create_basic_request(:post, path) { |http_request| http_request.set_form_data(parameters) }
19
+ HttpStub::Configurer::Request::Http::Basic.new(method: :post, path: path, parameters: parameters)
20
20
  end
21
21
 
22
- def delete(path)
23
- create_basic_request(:delete, path)
24
- end
25
-
26
- private
27
-
28
- def create_basic_request(request_method, path, &_block)
29
- http_request_class = Net::HTTP.const_get(request_method.to_s.capitalize)
30
- http_request = http_request_class.new(path)
31
- yield http_request if block_given?
32
- HttpStub::Configurer::Request::Http::Basic.new(http_request)
22
+ def delete(path, headers={})
23
+ HttpStub::Configurer::Request::Http::Basic.new(method: :delete, path: path, headers: headers)
33
24
  end
34
25
 
35
26
  end
@@ -5,20 +5,18 @@ module HttpStub
5
5
 
6
6
  class Multipart
7
7
 
8
- def initialize(model)
9
- @model = model
8
+ def initialize(args)
9
+ @model = args[:model]
10
+ @path = args[:path]
11
+ @headers = args[:headers] || {}
10
12
  end
11
13
 
12
14
  def to_http_request
13
- Net::HTTP::Post::Multipart.new(path, parameters)
15
+ Net::HTTP::Post::Multipart.new("/http_stub/#{@path}", parameters, @headers.stringify_keys)
14
16
  end
15
17
 
16
18
  private
17
19
 
18
- def path
19
- "/http_stub/#{@model.class.name.demodulize.pluralize.underscore}"
20
- end
21
-
22
20
  def parameters
23
21
  { payload: @model.payload.to_json }.tap do |parameters|
24
22
  @model.response_files.each do |response_file|
@@ -8,7 +8,7 @@ module HttpStub
8
8
  @id = SecureRandom.uuid
9
9
  @request = args[:request]
10
10
  @response = HttpStub::Configurer::Request::StubResponse.new(@id, args[:response])
11
- @triggers = args[:triggers].map(&:build)
11
+ @triggers = HttpStub::Configurer::Request::Triggers.new(args[:triggers])
12
12
  end
13
13
 
14
14
  def payload
@@ -20,12 +20,12 @@ module HttpStub
20
20
  parameters: HttpStub::Configurer::Request::ControllableValue.format(@request[:parameters] || {}),
21
21
  body: HttpStub::Configurer::Request::ControllableValue.format(@request[:body] || {}),
22
22
  response: @response.payload,
23
- triggers: @triggers.map(&:payload)
23
+ triggers: @triggers.payload
24
24
  }
25
25
  end
26
26
 
27
27
  def response_files
28
- ([ @response.file ] + @triggers.map(&:response_files)).flatten.compact
28
+ ([ @response.file ] + @triggers.response_files).compact
29
29
  end
30
30
 
31
31
  def to_s
@@ -0,0 +1,25 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Request
4
+
5
+ class Triggers
6
+
7
+ def initialize(args)
8
+ @scenario_names = args[:scenario_names]
9
+ @stubs = args[:stubs]
10
+ end
11
+
12
+ def response_files
13
+ @stubs.map(&:response_files).flatten
14
+ end
15
+
16
+ def payload
17
+ { scenario_names: @scenario_names, stubs: @stubs.map(&:payload) }
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+ end
@@ -4,12 +4,12 @@ module HttpStub
4
4
 
5
5
  class CommandProcessor
6
6
 
7
- def initialize(configurer)
8
- @configurer = configurer
7
+ def initialize(configuration)
8
+ @configuration = configuration
9
9
  end
10
10
 
11
11
  def process(command, http_options={})
12
- response = Net::HTTP.start(host, port, http_options) { |http| http.request(command.http_request) }
12
+ response = send_command(command, http_options)
13
13
  raise "#{error_message_prefix(command)}: #{response.code} #{response.message}" unless response.code == "200"
14
14
  response
15
15
  rescue StandardError => err
@@ -18,16 +18,14 @@ module HttpStub
18
18
 
19
19
  private
20
20
 
21
- def host
22
- @configurer.stub_server.host
23
- end
24
-
25
- def port
26
- @configurer.stub_server.port
21
+ def send_command(command, http_options)
22
+ Net::HTTP.start(@configuration.host, @configuration.port, http_options) do |http|
23
+ http.request(command.http_request)
24
+ end
27
25
  end
28
26
 
29
27
  def error_message_prefix(command)
30
- "Error occurred #{command.description} whilst configuring #{@configurer.stub_server.base_uri}: "
28
+ "Error occurred #{command.description} whilst configuring #{@configuration.base_uri}: "
31
29
  end
32
30
 
33
31
  end
@@ -0,0 +1,33 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class Configuration
6
+
7
+ attr_accessor :host, :port, :session_identifier
8
+
9
+ def initialize
10
+ @enabled_features = []
11
+ end
12
+
13
+ def base_uri
14
+ "http://#{host}:#{port}"
15
+ end
16
+
17
+ def external_base_uri
18
+ ENV["STUB_EXTERNAL_BASE_URI"] || base_uri
19
+ end
20
+
21
+ def enable(*features)
22
+ @enabled_features = features
23
+ end
24
+
25
+ def enabled?(feature)
26
+ @enabled_features.include?(feature)
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end
33
+ end
@@ -4,72 +4,50 @@ module HttpStub
4
4
 
5
5
  class Facade
6
6
 
7
- STUBS_BASE_URI = "/http_stub/stubs".freeze
8
- STUB_MEMORY_URI = "#{STUBS_BASE_URI}/memory".freeze
9
- SCENARIOS_BASE_URI = "/http_stub/scenarios".freeze
10
- SCENARIO_ACTIVATION_URI = "#{SCENARIOS_BASE_URI}/activate".freeze
7
+ attr_reader :default_session
11
8
 
12
- private_constant :STUBS_BASE_URI, :STUB_MEMORY_URI, :SCENARIOS_BASE_URI, :SCENARIO_ACTIVATION_URI
13
-
14
- def initialize(configurer)
15
- @request_processor = HttpStub::Configurer::Server::RequestProcessor.new(configurer)
16
- end
17
-
18
- def stub_response(model)
19
- @request_processor.submit(
20
- request: HttpStub::Configurer::Request::Http::Factory.multipart(model),
21
- description: "stubbing '#{model}'"
22
- )
9
+ def initialize(configuration)
10
+ @request_processor = HttpStub::Configurer::Server::RequestProcessor.new(configuration)
23
11
  end
24
12
 
25
- def define_scenario(model)
26
- @request_processor.submit(
27
- request: HttpStub::Configurer::Request::Http::Factory.multipart(model),
28
- description: "registering scenario '#{model}'"
29
- )
13
+ def server_has_started
14
+ @request_processor.disable_buffering!
30
15
  end
31
16
 
32
- def activate(scenario_name)
33
- request = HttpStub::Configurer::Request::Http::Factory.post(SCENARIO_ACTIVATION_URI, name: scenario_name)
34
- @request_processor.submit(
35
- request: request,
36
- description: "activating '#{scenario_name}'"
37
- )
17
+ def flush_requests
18
+ @request_processor.flush!
38
19
  end
39
20
 
40
- def remember_stubs
21
+ def reset
41
22
  @request_processor.submit(
42
- request: HttpStub::Configurer::Request::Http::Factory.post(STUB_MEMORY_URI),
43
- description: "committing stubs to memory"
23
+ request: HttpStub::Configurer::Request::Http::Factory.delete("memory"),
24
+ description: "resetting server"
44
25
  )
45
26
  end
46
27
 
47
- def recall_stubs
28
+ def define_scenario(scenario)
48
29
  @request_processor.submit(
49
- request: HttpStub::Configurer::Request::Http::Factory.get(STUB_MEMORY_URI),
50
- description: "recalling stubs in memory"
30
+ request: HttpStub::Configurer::Request::Http::Factory.multipart("scenarios", scenario),
31
+ description: "registering scenario '#{scenario}'"
51
32
  )
52
33
  end
53
34
 
54
- def clear_stubs
55
- @request_processor.submit(
56
- request: HttpStub::Configurer::Request::Http::Factory.delete(STUBS_BASE_URI),
57
- description: "clearing stubs")
58
- end
59
-
60
35
  def clear_scenarios
61
36
  @request_processor.submit(
62
- request: HttpStub::Configurer::Request::Http::Factory.delete(SCENARIOS_BASE_URI),
37
+ request: HttpStub::Configurer::Request::Http::Factory.delete("scenarios"),
63
38
  description: "clearing scenarios"
64
39
  )
65
40
  end
66
41
 
67
- def server_has_started
68
- @request_processor.disable_buffering!
42
+ def create_session_facade(id)
43
+ HttpStub::Configurer::Server::SessionFacade.new(id, @request_processor)
69
44
  end
70
45
 
71
- def flush_requests
72
- @request_processor.flush!
46
+ def clear_sessions
47
+ @request_processor.submit(
48
+ request: HttpStub::Configurer::Request::Http::Factory.delete("sessions"),
49
+ description: "clearing sessions"
50
+ )
73
51
  end
74
52
 
75
53
  end
@@ -4,8 +4,8 @@ module HttpStub
4
4
 
5
5
  class RequestProcessor
6
6
 
7
- def initialize(configurer)
8
- @command_processor = HttpStub::Configurer::Server::CommandProcessor.new(configurer)
7
+ def initialize(configuration)
8
+ @command_processor = HttpStub::Configurer::Server::CommandProcessor.new(configuration)
9
9
  @buffered_command_processor = @active_processor =
10
10
  HttpStub::Configurer::Server::BufferedCommandProcessor.new(@command_processor)
11
11
  end
@@ -0,0 +1,64 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module Server
4
+
5
+ class SessionFacade
6
+
7
+ REQUEST_FACTORY = HttpStub::Configurer::Request::Http::Factory
8
+
9
+ private_constant :REQUEST_FACTORY
10
+
11
+ def initialize(session_id, request_processor)
12
+ @session_id = session_id
13
+ @request_processor = request_processor
14
+ @session_description = "session '#{session_id}'"
15
+ end
16
+
17
+ def mark_as_default
18
+ @request_processor.submit(
19
+ request: REQUEST_FACTORY.post("sessions/default", http_stub_session_id: @session_id),
20
+ description: "marking #{@session_description} as the default"
21
+ )
22
+ end
23
+
24
+ def stub_response(stub)
25
+ @request_processor.submit(
26
+ request: REQUEST_FACTORY.multipart("stubs", stub, http_stub_session_id: @session_id),
27
+ description: "stubbing '#{stub}' in #{@session_description}"
28
+ )
29
+ end
30
+
31
+ def activate(scenario_names)
32
+ parameters = { http_stub_session_id: @session_id, "names[]" => scenario_names }
33
+ @request_processor.submit(
34
+ request: REQUEST_FACTORY.post("scenarios/activate", parameters),
35
+ description: "activating #{scenario_names.map { |name| "'#{name}'" }.join(", ")} in #{@session_description}"
36
+ )
37
+ end
38
+
39
+ def reset_stubs
40
+ @request_processor.submit(
41
+ request: REQUEST_FACTORY.post("stubs/reset", http_stub_session_id: @session_id),
42
+ description: "resetting stubs in #{@session_description}"
43
+ )
44
+ end
45
+
46
+ def clear_stubs
47
+ @request_processor.submit(
48
+ request: REQUEST_FACTORY.delete("stubs", http_stub_session_id: @session_id),
49
+ description: "clearing stubs in #{@session_description}"
50
+ )
51
+ end
52
+
53
+ def delete
54
+ @request_processor.submit(
55
+ request: REQUEST_FACTORY.delete("sessions", http_stub_session_id: @session_id),
56
+ description: "deleting #{@session_description}"
57
+ )
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ module HttpStub
2
+ module Extensions
3
+ module Core
4
+
5
+ module URI
6
+
7
+ def self.included(mod)
8
+ mod.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def add_parameters(uri, parameters)
14
+ parsed_uri = ::URI.parse(uri)
15
+ existing_parameters = ::URI.decode_www_form(parsed_uri.query || "").to_h
16
+ parsed_uri.query = ::URI.encode_www_form(existing_parameters.merge(parameters))
17
+ parsed_uri.to_s
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
26
+ end
27
+
28
+ ::URI.send(:include, HttpStub::Extensions::Core::URI)
@@ -1,6 +1,7 @@
1
1
  module HttpStub
2
2
  module Extensions
3
3
  module Rack
4
+
4
5
  module Handler
5
6
 
6
7
  def self.included(mod)