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,15 @@
1
+ module HttpStub
2
+ module Server
3
+ module Session
4
+
5
+ class Empty
6
+
7
+ def self.stubs
8
+ []
9
+ end
10
+
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module HttpStub
2
+ module Server
3
+ module Session
4
+
5
+ class IdentifierStrategy
6
+
7
+ def initialize(session_configuration)
8
+ @identifier_configuration = session_configuration.identifier_configuration
9
+ end
10
+
11
+ def identifier_for(sinatra_request)
12
+ @identifier_configuration.map do |configuration_entry|
13
+ configuration_entry.map do |attribute_type, attribute_name|
14
+ identifier_attributes = attribute_type == :header ? sinatra_request.headers : sinatra_request.parameters
15
+ identifier_attributes[attribute_name]
16
+ end
17
+ end.flatten.compact.first
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ module HttpStub
2
+ module Server
3
+ module Session
4
+
5
+ class Registry
6
+
7
+ delegate :find, :all, :delete, to: :@session_registry
8
+
9
+ def initialize(session_configuration, scenario_registry, memory_session)
10
+ @session_configuration = session_configuration
11
+ @scenario_registry = scenario_registry
12
+ @memory_session = memory_session
13
+ @session_registry = HttpStub::Server::Registry.new("session", [ memory_session ])
14
+ end
15
+
16
+ def find_or_create(session_id, logger)
17
+ effective_session_id = session_id || @session_configuration.default_identifier
18
+ @session_registry.find(effective_session_id, logger) || create(effective_session_id, logger)
19
+ end
20
+
21
+ def clear(logger)
22
+ @memory_session.clear(logger)
23
+ @session_registry.replace([ @memory_session ], logger)
24
+ end
25
+
26
+ private
27
+
28
+ def create(session_id, logger)
29
+ HttpStub::Server::Session::Session.new(session_id, @scenario_registry, @memory_session).tap do |session|
30
+ @session_registry.add(session, logger)
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,80 @@
1
+ module HttpStub
2
+ module Server
3
+ module Session
4
+
5
+ class Session
6
+
7
+ attr_reader :id
8
+
9
+ def initialize(id, scenario_registry, memory_session)
10
+ @id = id
11
+ @scenario_registry = scenario_registry
12
+ @stub_registry = HttpStub::Server::Stub::Registry.new(memory_session)
13
+ @stub_match_registry = HttpStub::Server::Registry.new("stub match")
14
+ @stub_miss_registry = HttpStub::Server::Registry.new("stub miss")
15
+ end
16
+
17
+ def matches?(id, _logger)
18
+ id == @id
19
+ end
20
+
21
+ def activate_scenario!(name, logger)
22
+ found_scenario = @scenario_registry.find(name, logger)
23
+ raise HttpStub::Server::Scenario::NotFoundError, name unless found_scenario
24
+ @stub_registry.concat(found_scenario.stubs, logger)
25
+ found_scenario.triggered_scenarios.each { |scenario| activate_scenario!(scenario.name, logger) }
26
+ end
27
+
28
+ def add_stub(stub, logger)
29
+ @stub_registry.add(stub, logger)
30
+ end
31
+
32
+ def find_stub(id, logger)
33
+ @stub_registry.find(id, logger)
34
+ end
35
+
36
+ def stubs
37
+ @stub_registry.all
38
+ end
39
+
40
+ def match(request, logger)
41
+ @stub_registry.match(request, logger)
42
+ end
43
+
44
+ def add_match(match, logger)
45
+ @stub_match_registry.add(match, logger)
46
+ match.stub.triggers.scenario_names.each { |scenario_name| activate_scenario!(scenario_name, logger) }
47
+ match.stub.triggers.stubs.each { |stub| add_stub(stub, logger) }
48
+ end
49
+
50
+ def matches
51
+ @stub_match_registry.all
52
+ end
53
+
54
+ def last_match(args, logger)
55
+ @stub_match_registry.find(args, logger)
56
+ end
57
+
58
+ def add_miss(miss, logger)
59
+ @stub_miss_registry.add(miss, logger)
60
+ end
61
+
62
+ def misses
63
+ @stub_miss_registry.all
64
+ end
65
+
66
+ def reset(logger)
67
+ @stub_miss_registry.clear(logger)
68
+ @stub_match_registry.clear(logger)
69
+ @stub_registry.reset(logger)
70
+ end
71
+
72
+ def clear(logger)
73
+ [ @stub_miss_registry, @stub_match_registry, @stub_registry ].each { |registry| registry.clear(logger) }
74
+ end
75
+
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -4,28 +4,34 @@ module HttpStub
4
4
 
5
5
  class Controller
6
6
 
7
- def initialize(stub_registry, match_registry, miss_registry)
8
- @stub_registry = stub_registry
9
- @match_registry = match_registry
10
- @miss_registry = miss_registry
11
- end
12
-
13
7
  def register(request, logger)
14
8
  stub = HttpStub::Server::Stub.create(HttpStub::Server::Stub::Parser.parse(request))
15
- @stub_registry.add(stub, logger)
9
+ request.session.add_stub(stub, logger)
16
10
  HttpStub::Server::Response.ok("headers" => { "location" => stub.uri })
17
11
  end
18
12
 
19
13
  def match(request, logger)
20
- stub = @stub_registry.match(request, logger)
14
+ stub = request.session.match(request, logger)
21
15
  response = stub ? stub.response_for(request) : HttpStub::Server::Response::NOT_FOUND
22
- @match_registry.add(HttpStub::Server::Stub::Match::Match.new(request, response, stub), logger) if stub
23
- @miss_registry.add(HttpStub::Server::Stub::Match::Miss.new(request), logger) unless stub
16
+ request.session.add_match(HttpStub::Server::Stub::Match::Match.new(request, response, stub), logger) if stub
17
+ request.session.add_miss(HttpStub::Server::Stub::Match::Miss.new(request), logger) unless stub
24
18
  response
25
19
  end
26
20
 
27
- def clear(logger)
28
- [ @stub_registry, @match_registry, @miss_registry ].each { |registry| registry.clear(logger) }
21
+ def find(request, logger)
22
+ request.session.find_stub(request.parameters[:stub_id], logger)
23
+ end
24
+
25
+ def find_all(request)
26
+ request.session.stubs
27
+ end
28
+
29
+ def reset(request, logger)
30
+ request.session.reset(logger)
31
+ end
32
+
33
+ def clear(request, logger)
34
+ request.session.clear(logger)
29
35
  end
30
36
 
31
37
  end
@@ -10,7 +10,7 @@ module HttpStub
10
10
  @uri = ""
11
11
  @match_rules = HttpStub::Server::Stub::Match::Rules::EMPTY
12
12
  @response = HttpStub::Server::Response::EMPTY
13
- @triggers = []
13
+ @triggers = HttpStub::Server::Stub::Triggers::EMPTY
14
14
  end
15
15
 
16
16
  public
@@ -5,13 +5,17 @@ module HttpStub
5
5
 
6
6
  class Controller
7
7
 
8
- def initialize(result_registry)
9
- @result_registry = result_registry
8
+ def last_match(request, logger)
9
+ result = request.session.last_match(request.parameters.slice(:method, :uri), logger)
10
+ result ? HttpStub::Server::Response.ok("body" => result.to_json) : HttpStub::Server::Response::NOT_FOUND
10
11
  end
11
12
 
12
- def find_last(request, logger)
13
- result = @result_registry.find(request.parameters.slice(:method, :uri), logger)
14
- result ? HttpStub::Server::Response.ok("body" => result.to_json) : HttpStub::Server::Response::NOT_FOUND
13
+ def matches(request)
14
+ request.session.matches
15
+ end
16
+
17
+ def misses(request)
18
+ request.session.misses
15
19
  end
16
20
 
17
21
  end
@@ -8,7 +8,9 @@ module HttpStub
8
8
  def self.modify!(payload, request)
9
9
  response_file = request.parameters["response_file_#{payload["id"]}"]
10
10
  payload["response"]["body"] = response_file if response_file
11
- payload["triggers"].each { |trigger_payload| modify!(trigger_payload, request) } if payload["triggers"]
11
+ payload["triggers"]["stubs"].each do |trigger_payload|
12
+ modify!(trigger_payload, request)
13
+ end if payload["triggers"] && payload["triggers"]["stubs"]
12
14
  end
13
15
 
14
16
  end
@@ -6,22 +6,15 @@ module HttpStub
6
6
 
7
7
  delegate :add, :concat, :find, :all, :clear, to: :@stub_registry
8
8
 
9
- def initialize
10
- @stub_registry = HttpStub::Server::Registry.new("stub")
11
- end
12
-
13
- def match(request, logger)
14
- @stub_registry.find(request, logger).tap do |matched_stub|
15
- matched_stub.triggers.add_to(self, logger) if matched_stub
16
- end
17
- end
9
+ alias_method :match, :find
18
10
 
19
- def remember
20
- @remembered_stub = @stub_registry.last
11
+ def initialize(memory_session)
12
+ @memory_session = memory_session
13
+ @stub_registry = HttpStub::Server::Registry.new("stub", memory_session.stubs)
21
14
  end
22
15
 
23
- def recall
24
- @stub_registry.rollback_to(@remembered_stub) if @remembered_stub
16
+ def reset(logger)
17
+ @stub_registry.replace(@memory_session.stubs, logger)
25
18
  end
26
19
 
27
20
  end
@@ -4,22 +4,24 @@ module HttpStub
4
4
 
5
5
  class Triggers
6
6
 
7
- delegate :each, to: :@triggers
8
-
9
- def initialize(triggers)
10
- @triggers = (triggers || []).map { |trigger| HttpStub::Server::Stub.create(trigger) }
7
+ attr_reader :scenario_names
8
+ attr_reader :stubs
9
+
10
+ def initialize(args={})
11
+ resolved_args = { "scenario_names" => [], "stubs" => [] }.merge(args || {})
12
+ @scenario_names = resolved_args["scenario_names"]
13
+ @stubs = resolved_args["stubs"].map { |stub_args| HttpStub::Server::Stub.create(stub_args) }
14
+ @description = resolved_args.to_s
11
15
  end
12
16
 
13
- def add_to(registry, logger)
14
- @triggers.each { |trigger| registry.add(trigger, logger) }
15
- end
17
+ EMPTY = HttpStub::Server::Stub::Triggers.new.freeze
16
18
 
17
19
  def to_json(*args)
18
- @triggers.to_json(*args)
20
+ { scenario_names: @scenario_names, stubs: @stubs }.to_json(*args)
19
21
  end
20
22
 
21
23
  def to_s
22
- @triggers.reduce("") { |result, trigger| "#{result}\n#{trigger}" }
24
+ @description
23
25
  end
24
26
 
25
27
  end
@@ -0,0 +1,2 @@
1
+ %header
2
+ %h1 http_stub administration
@@ -0,0 +1,26 @@
1
+ :javascript
2
+ function postLink(link_selector, successText) {
3
+ $(window).load(function() {
4
+ $(link_selector).on("click", function(event) {
5
+ event.preventDefault();
6
+ var link = $(event.target)
7
+ var originalLinkText = link.text();
8
+ link.prop("disabled", true);
9
+ $.post(
10
+ link.attr("href"), link.data(), null, "html"
11
+ ).done(
12
+ function() {
13
+ link.html(successText).css("color", "green");
14
+ setTimeout(function() { link.html(originalLinkText).css("color", "inherit"); }, 3000);
15
+ }
16
+ ).fail(
17
+ function() {
18
+ link.html("Failed").css("color", "red");
19
+ setTimeout(function() { link.html(originalLinkText).css("color", "inherit"); }, 3000);
20
+ }
21
+ ).always(
22
+ function() { link.prop("disabled", false); }
23
+ );
24
+ });
25
+ });
26
+ }
@@ -0,0 +1,16 @@
1
+ %section
2
+ %a{ href: session_uri("/http_stub/scenarios"), id: :scenarios, class: :link } List Scenarios
3
+ %br
4
+ %a{ href: session_uri("/http_stub/stubs"), id: :stubs, class: :link } List Stubs
5
+ %br
6
+ %a{ href: session_uri("/http_stub/stubs/matches"), id: :stub_matches, class: :link } List Stub Matches
7
+ %br
8
+ %a{ href: session_uri("/http_stub/stubs/misses"), id: :stub_misses, class: :link } List Stub Misses
9
+ %br
10
+ %section
11
+ %a{ href: session_uri("/http_stub/stubs/reset"), id: :reset_session, class: :link } Reset To Initial State
12
+ %br
13
+ %a{ href: "/http_stub/memory", id: :memory, class: :link } View Initial State
14
+ %br
15
+ :javascript
16
+ postLink("a#reset_session", "Session Reset");
@@ -4,5 +4,11 @@
4
4
  %h3 Response
5
5
  = partial :response, locals: { response: the_stub.response }
6
6
  %h3 Triggers
7
- - the_stub.triggers.each do |trigger|
8
- = partial :stub, locals: { the_stub: trigger }
7
+ - if the_stub.triggers.scenario_names.present?
8
+ %h4 Scenarios
9
+ - the_stub.triggers.scenario_names.each do |triggered_scenario_name|
10
+ %span= triggered_scenario_name
11
+ - if the_stub.triggers.stubs.present?
12
+ %h4 Stubs
13
+ - the_stub.triggers.stubs.each do |triggered_stub|
14
+ = partial :stub, locals: { the_stub: triggered_stub }
@@ -0,0 +1,4 @@
1
+ %section
2
+ - stubs.each do |the_stub|
3
+ = partial :stub, locals: { the_stub: the_stub }
4
+ .header-rule
@@ -15,13 +15,16 @@ body
15
15
 
16
16
  header
17
17
  :padding-left 1rem
18
+ :text-align center
18
19
 
19
20
  section
21
+ :display flex
22
+ :align-items center
23
+ :flex-direction column
20
24
  :max-width 800px
21
25
  :border-radius 10px
22
26
  :background white
23
27
  :padding 1rem
24
- :display inline-block
25
28
 
26
29
  table
27
30
  :padding 1rem
@@ -5,5 +5,6 @@
5
5
  %link{ rel: "icon", href: "/favicon.ico", type: "image/x-icon" }
6
6
  %script{ src: "/jquery-2.2.4.min.js", type: "text/javascript" }
7
7
  %title http_stub server diagnostics
8
+ = partial :post_link_function
8
9
  %body
9
10
  =yield
@@ -1,12 +1,13 @@
1
1
  %header
2
2
  %h1= scenario.name
3
3
  %section
4
- %a{ href: scenario.links.activate, class: [:scenario, :link, :activate_scenario], data: { name: scenario.name } } Activate
4
+ %a{ href: session_uri(scenario.links.activate), class: [:scenario, :link, :activate_scenario], data: { name: scenario.name } } Activate
5
5
  %h2 Stubs
6
6
  - scenario.stubs.each do |the_stub|
7
7
  = partial :stub, locals: { the_stub: the_stub }
8
8
  - if scenario.triggered_scenarios.present?
9
9
  %h2 Triggered Scenarios
10
10
  - scenario.triggered_scenarios.each do |triggered_scenario|
11
- %a{ href: triggered_scenario.links.detail, class: :triggered_scenario_detail }= triggered_scenario.name
12
- = partial :activate_scenario
11
+ %a{ href: session_uri(triggered_scenario.links.detail), class: :triggered_scenario_detail }= triggered_scenario.name
12
+ :javascript
13
+ postLink("a.activate_scenario", "Activated");
@@ -4,9 +4,10 @@
4
4
  %table{ class: :scenarios }
5
5
  - scenarios.each do |scenario|
6
6
  %tr
7
- %td{ class: [:scenario_name, :"header-rule"] }= scenario.name
8
- %td{ class: :"header-rule" }
9
- %a{ href: scenario.links.activate, class: [:scenario, :link, :activate_scenario], data: { name: scenario.name } } Activate
10
- %td{ class: :"header-rule" }
11
- %a{ href: scenario.links.detail, class: [:scenario, :link, :view_scenario] } View Detail
12
- = partial :activate_scenario
7
+ %td{ class: ["scenario_name", "header-rule"] }= scenario.name
8
+ %td{ class: "header-rule" }
9
+ %a{ href: session_uri(scenario.links.activate), class: [:scenario, :link, :activate_scenario], data: { name: scenario.name } } Activate
10
+ %td{ class: "header-rule" }
11
+ %a{ href: session_uri(scenario.links.detail), class: [:scenario, :link, :view_scenario] } View Detail
12
+ :javascript
13
+ postLink("a.activate_scenario", "Activated");