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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf1b8a410f17f0ab1797a5474d9c67567d4fec05
4
- data.tar.gz: ed4cd4e41c8c080c2012ff00de1850a5f80c4ca9
3
+ metadata.gz: 347738836829271815cece8a214373ba88eca832
4
+ data.tar.gz: de16a1c9ac5fc32864414c97b320a9564c681df2
5
5
  SHA512:
6
- metadata.gz: f23b2f2d17db36ebefd1b6564b13b5631ed387c226715bfd71a0f07928cc6cea0aa753b88a1f8171704e20f908c907a3eb4902370b294c4d432815d160d3d31d
7
- data.tar.gz: 472618feeed2b8bcd06177553a484cec111a12c5e24eb53bf18cc363b49b80631ad576b155556f2f9955ad53f1842e2f2e03c802dbf6e4143b02232123106b09
6
+ metadata.gz: 7bb9b5a27d2528112e775a175bc4827365a81d268acafc283ae6b20b19ae4d9ee677e88e194565519e062dbcc39511a4bdac316be86e734587c205b8d8467829
7
+ data.tar.gz: 0554a3a709aa560bb30690d4b9fdc1235cc5e78409068ad97e9a0aa6255bd966bf793401c4f048bce4d76072e735ec78b6b7ba3bbe88a40ce0ab49825500640b
data/lib/http_stub.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'sinatra'
2
+ require_relative 'http_stub/extensions/sinatra/namespace'
2
3
  require 'sinatra/partial'
3
4
  require 'json'
4
5
  require 'net/http'
@@ -21,11 +22,13 @@ require_relative 'http_stub/extensions/core/hash/formatted'
21
22
  require_relative 'http_stub/extensions/core/hash/indifferent_and_insensitive_access'
22
23
  require_relative 'http_stub/extensions/core/hash/with_indifferent_and_insensitive_access'
23
24
  require_relative 'http_stub/extensions/core/hash'
25
+ require_relative 'http_stub/extensions/core/uri'
24
26
  require_relative 'http_stub/extensions/rack/handler'
25
27
  require_relative 'http_stub/server/request/parameters'
26
28
  require_relative 'http_stub/server/request/headers'
29
+ require_relative 'http_stub/server/request/sinatra_request'
27
30
  require_relative 'http_stub/server/request/request'
28
- require_relative 'http_stub/server/request'
31
+ require_relative 'http_stub/server/request/factory'
29
32
  require_relative 'http_stub/server/registry'
30
33
  require_relative 'http_stub/server/stub/match/omitted_value_matcher'
31
34
  require_relative 'http_stub/server/stub/match/regexp_value_matcher'
@@ -67,13 +70,31 @@ require_relative 'http_stub/server/stub'
67
70
  require_relative 'http_stub/server/scenario/links'
68
71
  require_relative 'http_stub/server/scenario/trigger'
69
72
  require_relative 'http_stub/server/scenario/parser'
73
+ require_relative 'http_stub/server/scenario/not_found_error'
70
74
  require_relative 'http_stub/server/scenario/scenario'
71
- require_relative 'http_stub/server/scenario/activator'
72
75
  require_relative 'http_stub/server/scenario/controller'
73
76
  require_relative 'http_stub/server/scenario'
74
- require_relative 'http_stub/server/application/text_formatting_support'
77
+ require_relative 'http_stub/server/session'
78
+ require_relative 'http_stub/server/session/configuration'
79
+ require_relative 'http_stub/server/session/identifier_strategy'
80
+ require_relative 'http_stub/server/session/empty'
81
+ require_relative 'http_stub/server/session/session'
82
+ require_relative 'http_stub/server/session/registry'
83
+ require_relative 'http_stub/server/session/controller'
84
+ require_relative 'http_stub/server/memory/memory'
85
+ require_relative 'http_stub/server/memory/controller'
86
+ require_relative 'http_stub/server/application/configuration'
87
+ require_relative 'http_stub/server/application/request_support'
88
+ require_relative 'http_stub/server/application/response_support'
75
89
  require_relative 'http_stub/server/application/cross_origin_support'
90
+ require_relative 'http_stub/server/application/session_uri_support'
91
+ require_relative 'http_stub/server/application/text_formatting_support'
76
92
  require_relative 'http_stub/server/application/response_pipeline'
93
+ require_relative 'http_stub/server/application/routes/memory'
94
+ require_relative 'http_stub/server/application/routes/resource'
95
+ require_relative 'http_stub/server/application/routes/session'
96
+ require_relative 'http_stub/server/application/routes/scenario'
97
+ require_relative 'http_stub/server/application/routes/stub'
77
98
  require_relative 'http_stub/server/application/application'
78
99
  require_relative 'http_stub/server/daemon'
79
100
  require_relative 'http_stub/configurer/request/http/basic'
@@ -84,23 +105,25 @@ require_relative 'http_stub/configurer/request/regexpable'
84
105
  require_relative 'http_stub/configurer/request/controllable_value'
85
106
  require_relative 'http_stub/configurer/request/stub_response_file'
86
107
  require_relative 'http_stub/configurer/request/stub_response'
108
+ require_relative 'http_stub/configurer/request/triggers'
87
109
  require_relative 'http_stub/configurer/request/stub'
88
110
  require_relative 'http_stub/configurer/request/scenario'
111
+ require_relative 'http_stub/configurer/server/configuration'
89
112
  require_relative 'http_stub/configurer/server/command'
90
113
  require_relative 'http_stub/configurer/server/command_processor'
91
114
  require_relative 'http_stub/configurer/server/buffered_command_processor'
92
115
  require_relative 'http_stub/configurer/server/request_processor'
116
+ require_relative 'http_stub/configurer/server/session_facade'
93
117
  require_relative 'http_stub/configurer/server/facade'
94
118
  require_relative 'http_stub/configurer/dsl/request_attribute_referencer'
95
119
  require_relative 'http_stub/configurer/dsl/request_referencer'
96
120
  require_relative 'http_stub/configurer/dsl/stub_builder'
97
- require_relative 'http_stub/configurer/dsl/stub_builder_producer'
98
- require_relative 'http_stub/configurer/dsl/scenario_activator'
121
+ require_relative 'http_stub/configurer/dsl/stub_builder_template'
122
+ require_relative 'http_stub/configurer/dsl/session_endpoint_template'
123
+ require_relative 'http_stub/configurer/dsl/session'
124
+ require_relative 'http_stub/configurer/dsl/session_factory'
99
125
  require_relative 'http_stub/configurer/dsl/scenario_builder'
100
- require_relative 'http_stub/configurer/dsl/endpoint_template'
101
- require_relative 'http_stub/configurer/dsl/stub_activator_builder'
126
+ require_relative 'http_stub/configurer/dsl/server_endpoint_template'
102
127
  require_relative 'http_stub/configurer/dsl/server'
103
- require_relative 'http_stub/configurer/dsl/deprecated'
104
- require_relative 'http_stub/configurer/dsl/deprecated'
105
128
  require_relative 'http_stub/configurer/part'
106
129
  require_relative 'http_stub/configurer'
@@ -5,19 +5,17 @@ module HttpStub
5
5
  def self.included(mod)
6
6
  mod.extend(HttpStub::Configurer::ClassMethods)
7
7
  mod.send(:include, HttpStub::Configurer::InstanceMethods)
8
- mod.send(:include, HttpStub::Configurer::DSL::Deprecated)
9
8
  end
10
9
 
11
10
  module ClassMethods
12
11
 
13
12
  def stub_server
14
- @stub_server ||= HttpStub::Configurer::DSL::Server.new(server_facade)
13
+ @stub_server ||= HttpStub::Configurer::DSL::Server.new
15
14
  end
16
15
 
17
16
  def initialize!
18
17
  on_initialize if self.respond_to?(:on_initialize) && !@initialized
19
- server_facade.flush_requests
20
- server_facade.remember_stubs
18
+ stub_server.initialize!
21
19
  @initialized = true
22
20
  end
23
21
 
@@ -29,12 +27,6 @@ module HttpStub
29
27
  end
30
28
  end
31
29
 
32
- private
33
-
34
- def server_facade
35
- @server_facade ||= HttpStub::Configurer::Server::Facade.new(self)
36
- end
37
-
38
30
  end
39
31
 
40
32
  module InstanceMethods
@@ -3,14 +3,14 @@ module HttpStub
3
3
  module DSL
4
4
 
5
5
  class ScenarioBuilder
6
- include HttpStub::Configurer::DSL::StubBuilderProducer
7
- include HttpStub::Configurer::DSL::ScenarioActivator
8
6
 
9
- def initialize(default_stub_builder, name)
10
- @default_stub_builder = default_stub_builder
7
+ delegate :build_stub, to: :@default_stub_template
8
+
9
+ def initialize(name, default_stub_template)
11
10
  @name = name
12
- @stub_builders = []
11
+ @default_stub_template = default_stub_template
13
12
  @triggered_scenario_names = []
13
+ @stub_builders = []
14
14
  end
15
15
 
16
16
  def add_stub!(builder=nil, &block)
@@ -18,18 +18,20 @@ module HttpStub
18
18
  @stub_builders << resolved_builder
19
19
  end
20
20
 
21
+ def add_stubs!(builders)
22
+ builders.each { |builder| add_stub!(builder) }
23
+ end
24
+
25
+ def activate!(*names)
26
+ @triggered_scenario_names.concat(names.flatten)
27
+ end
28
+
21
29
  def build
22
30
  HttpStub::Configurer::Request::Scenario.new(
23
31
  name: @name, stubs: @stub_builders.map(&:build), triggered_scenario_names: @triggered_scenario_names
24
32
  )
25
33
  end
26
34
 
27
- private
28
-
29
- def activate_all!(names)
30
- @triggered_scenario_names.concat(names)
31
- end
32
-
33
35
  end
34
36
 
35
37
  end
@@ -3,52 +3,46 @@ module HttpStub
3
3
  module DSL
4
4
 
5
5
  class Server
6
- include HttpStub::Configurer::DSL::StubBuilderProducer
7
- include HttpStub::Configurer::DSL::ScenarioActivator
8
6
 
9
- attr_accessor :host, :port
7
+ delegate :host, :host=, :port, :port=, :base_uri, :external_base_uri, to: :@configuration
8
+ delegate :session_identifier, :session_identifier=, to: :@configuration
9
+ delegate :enable, :enabled?, to: :@configuration
10
10
 
11
- def initialize(server_facade)
12
- @server_facade = server_facade
13
- @default_stub_builder = HttpStub::Configurer::DSL::StubBuilder.new
14
- @enabled_features = []
15
- end
16
-
17
- def base_uri
18
- "http://#{host}:#{port}"
19
- end
20
-
21
- def external_base_uri
22
- ENV["STUB_EXTERNAL_BASE_URI"] || base_uri
23
- end
24
-
25
- def enable(*features)
26
- @enabled_features = features
27
- end
11
+ delegate :activate!, :build_stub, :add_stub!, :add_stubs!, to: :@default_session
28
12
 
29
- def enabled?(feature)
30
- @enabled_features.include?(feature)
13
+ def initialize
14
+ @configuration = HttpStub::Configurer::Server::Configuration.new
15
+ @server_facade = HttpStub::Configurer::Server::Facade.new(@configuration)
16
+ @default_stub_template = HttpStub::Configurer::DSL::StubBuilderTemplate.new
17
+ @session_factory = HttpStub::Configurer::DSL::SessionFactory.new(@server_facade, @default_stub_template)
18
+ @default_session = @session_factory.memory
19
+ @enabled_features = []
31
20
  end
32
21
 
33
22
  def request_defaults=(args)
34
- @default_stub_builder.match_requests(args)
23
+ @default_stub_template.match_requests(args)
35
24
  end
36
25
 
37
26
  def response_defaults=(args)
38
- @default_stub_builder.respond_with(args)
27
+ @default_stub_template.respond_with(args)
28
+ end
29
+
30
+ def initialize!
31
+ @server_facade.flush_requests
32
+ switch_to_transactional_session
39
33
  end
40
34
 
41
35
  def has_started!
42
36
  @server_facade.server_has_started
37
+ switch_to_transactional_session
43
38
  end
44
39
 
45
- def add_stub!(builder=nil, &block)
46
- resolved_builder = builder || self.build_stub(&block)
47
- @server_facade.stub_response(resolved_builder.build)
40
+ def reset!
41
+ @server_facade.reset
48
42
  end
49
43
 
50
44
  def add_scenario!(name, &_block)
51
- builder = HttpStub::Configurer::DSL::ScenarioBuilder.new(@default_stub_builder, name)
45
+ builder = HttpStub::Configurer::DSL::ScenarioBuilder.new(name, @default_stub_template)
52
46
  yield builder
53
47
  @server_facade.define_scenario(builder.build)
54
48
  end
@@ -60,35 +54,30 @@ module HttpStub
60
54
  end
61
55
 
62
56
  def endpoint_template(&block)
63
- HttpStub::Configurer::DSL::EndpointTemplate.new(self).tap { |template| template.invoke(&block) }
57
+ HttpStub::Configurer::DSL::ServerEndpointTemplate.new(self, @default_session, &block)
64
58
  end
65
59
 
66
- def add_activator!(&_block)
67
- builder = HttpStub::Configurer::DSL::StubActivatorBuilder.new(@default_stub_builder)
68
- yield builder
69
- @server_facade.define_scenario(builder.build)
60
+ def session(id)
61
+ @session_factory.create(id)
70
62
  end
71
63
 
72
- def remember_stubs
73
- @server_facade.remember_stubs
64
+ def clear_sessions!
65
+ @server_facade.clear_sessions
74
66
  end
75
67
 
76
68
  def recall_stubs!
77
- @server_facade.recall_stubs
69
+ @default_session.reset!
78
70
  end
79
71
 
80
72
  def clear_stubs!
81
- @server_facade.clear_stubs
82
- end
83
-
84
- def clear_scenarios!
85
- @server_facade.clear_scenarios
73
+ @default_session.clear!
86
74
  end
87
75
 
88
76
  private
89
77
 
90
- def activate_all!(scenario_names)
91
- scenario_names.each { |scenario_name| @server_facade.activate(scenario_name) }
78
+ def switch_to_transactional_session
79
+ @session_factory.transactional.mark_as_default!
80
+ @default_session = @session_factory.transactional
92
81
  end
93
82
 
94
83
  end
@@ -0,0 +1,23 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module DSL
4
+
5
+ class ServerEndpointTemplate
6
+
7
+ delegate :match_requests, :schema, :respond_with, :trigger, :invoke, to: :@session_endpoint_template
8
+ delegate :build_stub, :add_stub!, to: :@session_endpoint_template
9
+
10
+ def initialize(server, default_session, &block)
11
+ @server = server
12
+ @session_endpoint_template = default_session.endpoint_template(&block)
13
+ end
14
+
15
+ def add_scenario!(name, response_overrides={}, &block)
16
+ @server.add_scenario_with_one_stub!(name, self.build_stub(response_overrides, &block))
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,55 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module DSL
4
+
5
+ class Session
6
+
7
+ MEMORY_SESSION_ID = "http_stub_memory".freeze
8
+ TRANSACTIONAL_SESSION_ID = "http_stub_transactional".freeze
9
+
10
+ delegate :build_stub, to: :@default_stub_template
11
+
12
+ def initialize(id, server_facade, default_stub_template)
13
+ @id = id
14
+ @session_facade = server_facade.create_session_facade(id)
15
+ @default_stub_template = default_stub_template
16
+ end
17
+
18
+ def mark_as_default!
19
+ @session_facade.mark_as_default
20
+ end
21
+
22
+ def endpoint_template(&block)
23
+ HttpStub::Configurer::DSL::SessionEndpointTemplate.new(self, @default_stub_template, &block)
24
+ end
25
+
26
+ def activate!(*names)
27
+ @session_facade.activate(names)
28
+ end
29
+
30
+ def add_stub!(builder=nil, &block)
31
+ resolved_builder = builder || self.build_stub(&block)
32
+ @session_facade.stub_response(resolved_builder.build)
33
+ end
34
+
35
+ def add_stubs!(builders)
36
+ builders.each { |builder| add_stub!(builder) }
37
+ end
38
+
39
+ def reset!
40
+ @session_facade.reset_stubs
41
+ end
42
+
43
+ def clear!
44
+ @session_facade.clear_stubs
45
+ end
46
+
47
+ def delete!
48
+ @session_facade.delete
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module DSL
4
+
5
+ class SessionEndpointTemplate
6
+
7
+ delegate :match_requests, :schema, :respond_with, :trigger, :invoke, :build_stub, to: :@stub_builder_template
8
+
9
+ def initialize(session, default_stub_template, &block)
10
+ @session = session
11
+ @stub_builder_template = HttpStub::Configurer::DSL::StubBuilderTemplate.new(default_stub_template, &block)
12
+ end
13
+
14
+ def add_stub!(response_overrides={}, &block)
15
+ @session.add_stub!(self.build_stub(response_overrides, &block))
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ module HttpStub
2
+ module Configurer
3
+ module DSL
4
+
5
+ class SessionFactory
6
+
7
+ MEMORY_SESSION_ID = "http_stub_memory".freeze
8
+ TRANSACTIONAL_SESSION_ID = "http_stub_transactional".freeze
9
+
10
+ private_constant :MEMORY_SESSION_ID, :TRANSACTIONAL_SESSION_ID
11
+
12
+ def initialize(server_facade, default_stub_template)
13
+ @server_facade = server_facade
14
+ @default_stub_template = default_stub_template
15
+ @sessions = {}
16
+ end
17
+
18
+ def create(id)
19
+ @sessions[id] ||= HttpStub::Configurer::DSL::Session.new(id, @server_facade, @default_stub_template)
20
+ end
21
+
22
+ def memory
23
+ self.create(MEMORY_SESSION_ID)
24
+ end
25
+
26
+ def transactional
27
+ self.create(TRANSACTIONAL_SESSION_ID)
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -6,11 +6,12 @@ module HttpStub
6
6
 
7
7
  attr_reader :request, :response, :triggers
8
8
 
9
- def initialize(default_builder=nil)
9
+ def initialize(parent_builder=nil, &block)
10
10
  @request = {}
11
11
  @response = {}
12
- @triggers = []
13
- self.merge!(default_builder) if default_builder
12
+ @triggers = { scenarios: [], stubs: [] }
13
+ self.merge!(parent_builder) if parent_builder
14
+ self.invoke(&block) if block_given?
14
15
  end
15
16
 
16
17
  def match_requests(args)
@@ -27,10 +28,12 @@ module HttpStub
27
28
  self.tap { @response.deep_merge!(resolved_args) }
28
29
  end
29
30
 
30
- def trigger(stub_builder_or_builders)
31
- resolved_builders =
32
- stub_builder_or_builders.is_a?(Array) ? stub_builder_or_builders : [ stub_builder_or_builders ]
33
- @triggers.concat(resolved_builders)
31
+ def trigger(args)
32
+ resolved_args = args.clone
33
+ resolved_args[:scenarios] ||= resolved_args[:scenario] ? [ resolved_args[:scenario] ] : []
34
+ resolved_args[:stubs] ||= resolved_args[:stub] ? [ resolved_args[:stub] ] : []
35
+ @triggers[:scenarios].concat(resolved_args[:scenarios])
36
+ @triggers[:stubs].concat(resolved_args[:stubs])
34
37
  self
35
38
  end
36
39
 
@@ -45,7 +48,14 @@ module HttpStub
45
48
  end
46
49
 
47
50
  def build
48
- HttpStub::Configurer::Request::Stub.new(request: @request, response: @response, triggers: @triggers)
51
+ HttpStub::Configurer::Request::Stub.new(
52
+ request: @request,
53
+ response: @response,
54
+ triggers: {
55
+ scenario_names: @triggers[:scenarios],
56
+ stubs: @triggers[:stubs].map(&:build)
57
+ }
58
+ )
49
59
  end
50
60
 
51
61
  end