http_stub 0.19.1 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +13 -5
  2. data/lib/http_stub.rb +26 -21
  3. data/lib/http_stub/configurer/server/command_processor.rb +1 -0
  4. data/lib/http_stub/server/application.rb +24 -46
  5. data/lib/http_stub/server/formatted_hash.rb +18 -0
  6. data/lib/http_stub/server/header_parser.rb +17 -0
  7. data/lib/http_stub/server/registry.rb +9 -9
  8. data/lib/http_stub/server/request.rb +19 -0
  9. data/lib/http_stub/server/request_pipeline.rb +27 -0
  10. data/lib/http_stub/server/response.rb +7 -3
  11. data/lib/http_stub/server/scenario.rb +1 -1
  12. data/lib/http_stub/server/scenario/activator.rb +4 -4
  13. data/lib/http_stub/server/scenario/controller.rb +8 -8
  14. data/lib/http_stub/server/scenario/{request_parser.rb → parser.rb} +2 -2
  15. data/lib/http_stub/server/scenario/{instance.rb → scenario.rb} +2 -2
  16. data/lib/http_stub/server/stub.rb +1 -1
  17. data/lib/http_stub/server/stub/controller.rb +8 -8
  18. data/lib/http_stub/server/stub/empty.rb +34 -0
  19. data/lib/http_stub/server/stub/match/exact_value_matcher.rb +17 -0
  20. data/lib/http_stub/server/stub/match/hash_with_string_value_matchers.rb +26 -0
  21. data/lib/http_stub/server/stub/match/match.rb +20 -0
  22. data/lib/http_stub/server/stub/match/omitted_value_matcher.rb +23 -0
  23. data/lib/http_stub/server/stub/match/regexp_value_matcher.rb +18 -0
  24. data/lib/http_stub/server/stub/match/rule/body.rb +49 -0
  25. data/lib/http_stub/server/stub/match/rule/headers.rb +31 -0
  26. data/lib/http_stub/server/stub/match/rule/json_body.rb +39 -0
  27. data/lib/http_stub/server/stub/match/rule/method.rb +27 -0
  28. data/lib/http_stub/server/stub/match/rule/parameters.rb +27 -0
  29. data/lib/http_stub/server/stub/match/rule/simple_body.rb +27 -0
  30. data/lib/http_stub/server/stub/match/rule/uri.rb +27 -0
  31. data/lib/http_stub/server/stub/match/string_value_matcher.rb +33 -0
  32. data/lib/http_stub/server/stub/match/truthy_matcher.rb +25 -0
  33. data/lib/http_stub/server/stub/{request_parser.rb → parser.rb} +2 -2
  34. data/lib/http_stub/server/stub/payload_file_consolidator.rb +1 -1
  35. data/lib/http_stub/server/stub/registry.rb +16 -8
  36. data/lib/http_stub/server/stub/response/base.rb +2 -2
  37. data/lib/http_stub/server/stub/stub.rb +44 -0
  38. data/lib/http_stub/server/stub/triggers.rb +2 -2
  39. data/lib/http_stub/server/views/_match.haml +20 -0
  40. data/lib/http_stub/server/views/matches.haml +3 -0
  41. data/lib/http_stub/server/views/stub.haml +1 -0
  42. data/lib/http_stub/version.rb +1 -1
  43. data/spec/acceptance/stub_control_values_spec.rb +6 -3
  44. data/spec/acceptance/stub_match_spec.rb +142 -0
  45. data/spec/lib/http_stub/configurer/server/command_processor_integration_spec.rb +4 -0
  46. data/spec/lib/http_stub/server/application_integration_spec.rb +17 -18
  47. data/spec/lib/http_stub/server/application_spec.rb +81 -53
  48. data/spec/lib/http_stub/server/formatted_hash_spec.rb +54 -0
  49. data/spec/lib/http_stub/server/{stub/request_header_parser_spec.rb → header_parser_spec.rb} +9 -9
  50. data/spec/lib/http_stub/server/{request_file_consolidator_spec.rb → payload_file_consolidator_spec.rb} +4 -4
  51. data/spec/lib/http_stub/server/registry_spec.rb +13 -14
  52. data/spec/lib/http_stub/server/request_pipeline_spec.rb +80 -0
  53. data/spec/lib/http_stub/server/request_spec.rb +113 -0
  54. data/spec/lib/http_stub/server/response_spec.rb +46 -12
  55. data/spec/lib/http_stub/server/scenario/activator_spec.rb +18 -18
  56. data/spec/lib/http_stub/server/scenario/controller_spec.rb +18 -15
  57. data/spec/lib/http_stub/server/scenario/{request_parser_spec.rb → parser_spec.rb} +8 -8
  58. data/spec/lib/http_stub/server/scenario/{instance_spec.rb → scenario_spec.rb} +7 -5
  59. data/spec/lib/http_stub/server/scenario_spec.rb +3 -3
  60. data/spec/lib/http_stub/server/stub/controller_spec.rb +29 -14
  61. data/spec/lib/http_stub/server/stub/empty_spec.rb +68 -0
  62. data/spec/lib/http_stub/server/stub/{exact_value_matcher_spec.rb → match/exact_value_matcher_spec.rb} +2 -2
  63. data/spec/lib/http_stub/server/stub/{hash_with_string_value_matchers_spec.rb → match/hash_with_string_value_matchers_spec.rb} +27 -19
  64. data/spec/lib/http_stub/server/stub/match/match_spec.rb +40 -0
  65. data/spec/lib/http_stub/server/stub/{omitted_value_matcher_spec.rb → match/omitted_value_matcher_spec.rb} +2 -2
  66. data/spec/lib/http_stub/server/stub/{regexp_value_matcher_spec.rb → match/regexp_value_matcher_spec.rb} +2 -2
  67. data/spec/lib/http_stub/server/stub/{request_body_spec.rb → match/rule/body_spec.rb} +12 -12
  68. data/spec/lib/http_stub/server/stub/match/rule/headers_spec.rb +90 -0
  69. data/spec/lib/http_stub/server/stub/{json_request_body_spec.rb → match/rule/json_body_spec.rb} +9 -10
  70. data/spec/lib/http_stub/server/stub/{method_spec.rb → match/rule/method_spec.rb} +6 -5
  71. data/spec/lib/http_stub/server/stub/{request_parameters_spec.rb → match/rule/parameters_spec.rb} +16 -10
  72. data/spec/lib/http_stub/server/stub/match/rule/simple_body_spec.rb +46 -0
  73. data/spec/lib/http_stub/server/stub/match/rule/uri_spec.rb +46 -0
  74. data/spec/lib/http_stub/server/stub/{string_value_matcher_spec.rb → match/string_value_matcher_spec.rb} +30 -26
  75. data/spec/lib/http_stub/server/stub/match/truthy_matcher_spec.rb +24 -0
  76. data/spec/lib/http_stub/server/stub/{request_parser_spec.rb → parser_spec.rb} +7 -7
  77. data/spec/lib/http_stub/server/stub/registry_integration_spec.rb +5 -5
  78. data/spec/lib/http_stub/server/stub/registry_spec.rb +114 -43
  79. data/spec/lib/http_stub/server/stub/response/base_spec.rb +8 -2
  80. data/spec/lib/http_stub/server/stub/stub_spec.rb +294 -0
  81. data/spec/lib/http_stub/server/stub/triggers_spec.rb +4 -4
  82. data/spec/lib/http_stub/server/stub_spec.rb +3 -3
  83. data/spec/spec_helper.rb +7 -3
  84. data/spec/support/configurer_integration.rb +1 -0
  85. data/spec/support/http_stub/empty_configurer.rb +7 -0
  86. data/spec/support/{scenario_fixture.rb → http_stub/scenario_fixture.rb} +0 -0
  87. data/spec/support/http_stub/server/request_fixture.rb +25 -0
  88. data/spec/support/http_stub/server/scenario/scenario_fixture.rb +15 -0
  89. data/spec/support/http_stub/server/stub/match/match_fixture.rb +17 -0
  90. data/spec/support/{stub_fixture.rb → http_stub/stub_fixture.rb} +0 -0
  91. data/spec/support/server_integration.rb +1 -0
  92. metadata +315 -290
  93. data/lib/http_stub/server/stub/exact_value_matcher.rb +0 -15
  94. data/lib/http_stub/server/stub/hash_with_string_value_matchers.rb +0 -22
  95. data/lib/http_stub/server/stub/headers.rb +0 -19
  96. data/lib/http_stub/server/stub/instance.rb +0 -32
  97. data/lib/http_stub/server/stub/json_request_body.rb +0 -35
  98. data/lib/http_stub/server/stub/method.rb +0 -23
  99. data/lib/http_stub/server/stub/omitted_value_matcher.rb +0 -21
  100. data/lib/http_stub/server/stub/regexp_value_matcher.rb +0 -16
  101. data/lib/http_stub/server/stub/request_body.rb +0 -44
  102. data/lib/http_stub/server/stub/request_header_parser.rb +0 -19
  103. data/lib/http_stub/server/stub/request_headers.rb +0 -28
  104. data/lib/http_stub/server/stub/request_parameters.rb +0 -23
  105. data/lib/http_stub/server/stub/simple_request_body.rb +0 -23
  106. data/lib/http_stub/server/stub/string_value_matcher.rb +0 -31
  107. data/lib/http_stub/server/stub/truthy_request_matcher.rb +0 -23
  108. data/lib/http_stub/server/stub/uri.rb +0 -23
  109. data/spec/lib/http_stub/server/stub/headers_spec.rb +0 -51
  110. data/spec/lib/http_stub/server/stub/instance_spec.rb +0 -235
  111. data/spec/lib/http_stub/server/stub/request_headers_spec.rb +0 -68
  112. data/spec/lib/http_stub/server/stub/simple_request_body_spec.rb +0 -43
  113. data/spec/lib/http_stub/server/stub/truthy_request_matcher_spec.rb +0 -23
  114. data/spec/lib/http_stub/server/stub/uri_spec.rb +0 -43
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: db776c7504fe1de45fec47a235aabd80355a1f10
4
- data.tar.gz: 9900db1809f2a8404f17bd8c53b1d161d87c00ff
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzEzZTE3NjYzODQyZTFjMTU1NzdjNDk1YmE3MDBmYWVlYjQzNTRlMQ==
5
+ data.tar.gz: !binary |-
6
+ YzI4MjA5ZjI5NzI1NmYyNDEzMGI4OWM0MDhmYjg5NTcyNGJlNTk3NQ==
5
7
  SHA512:
6
- metadata.gz: b7e98fe17e5a5af10bf737bc77d6766f53f4c3557a8a3130cf851fffe63089632433844a24471ec4c3e60b2976faa03fe963982a6cb5e6f5f623d047b6cf4087
7
- data.tar.gz: f0a8e46fa7721be673816eb64e8311b92c1a6b02dd67455769cf2e676d02b7b8c38b94ff7476a4034658777942d8987b26eb91d5ff0a96692366a4e4164dc470
8
+ metadata.gz: !binary |-
9
+ ODkwODg5NzU4Yzk0MjdhZjJlMmE1Nzg5ZjFmMmRlZGM2MmI4ZmZlZWJmZTE2
10
+ MzViMTBmMmY1NWE4MGMxOTBiOWMwYmExMTVmNDQ5ZWM5NzQyMWU1MjA1YzAw
11
+ MjlkMDBjYTJjNDI0NDRmNTgzMDhkYzU4ODI3OTEwMDYyOGY4MjA=
12
+ data.tar.gz: !binary |-
13
+ ODk0MTQ5YmNjN2I0YjZkYmRjZDc0ZDU2NWNlMWUzZTU3NGRhMmRjOTZkMDUy
14
+ NjEwYzIzZGNiMDg4MTMxMDc0N2RjOTIwNDY4OTcwMjRmMThhMjk0NzNlYmY4
15
+ ZTUxOWIyMTAyODY5NDIxMWIxNTdhZThhNzM0MWM5NzQyNzFjNGY=
data/lib/http_stub.rb CHANGED
@@ -20,39 +20,43 @@ require 'active_support/core_ext/hash/indifferent_access'
20
20
  require_relative 'http_stub/hash_with_indifferent_and_insensitive_access'
21
21
  require_relative 'http_stub/extensions/core/hash'
22
22
  require_relative 'http_stub/extensions/rack/handler'
23
+ require_relative 'http_stub/server/formatted_hash'
24
+ require_relative 'http_stub/server/header_parser'
25
+ require_relative 'http_stub/server/request'
23
26
  require_relative 'http_stub/server/registry'
24
- require_relative 'http_stub/server/stub/omitted_value_matcher'
25
- require_relative 'http_stub/server/stub/regexp_value_matcher'
26
- require_relative 'http_stub/server/stub/exact_value_matcher'
27
- require_relative 'http_stub/server/stub/string_value_matcher'
28
- require_relative 'http_stub/server/stub/hash_with_string_value_matchers'
29
- require_relative 'http_stub/server/stub/headers'
30
- require_relative 'http_stub/server/stub/request_header_parser'
31
- require_relative 'http_stub/server/stub/payload_file_consolidator'
32
- require_relative 'http_stub/server/stub/request_parser'
33
- require_relative 'http_stub/server/stub/truthy_request_matcher'
34
- require_relative 'http_stub/server/stub/uri'
35
- require_relative 'http_stub/server/stub/method'
36
- require_relative 'http_stub/server/stub/request_headers'
37
- require_relative 'http_stub/server/stub/request_parameters'
38
- require_relative 'http_stub/server/stub/simple_request_body'
39
- require_relative 'http_stub/server/stub/json_request_body'
40
- require_relative 'http_stub/server/stub/request_body'
27
+ require_relative 'http_stub/server/stub/match/omitted_value_matcher'
28
+ require_relative 'http_stub/server/stub/match/regexp_value_matcher'
29
+ require_relative 'http_stub/server/stub/match/exact_value_matcher'
30
+ require_relative 'http_stub/server/stub/match/string_value_matcher'
31
+ require_relative 'http_stub/server/stub/match/hash_with_string_value_matchers'
32
+ require_relative 'http_stub/server/stub/match/truthy_matcher'
33
+ require_relative 'http_stub/server/stub/match/rule/uri'
34
+ require_relative 'http_stub/server/stub/match/rule/method'
35
+ require_relative 'http_stub/server/stub/match/rule/headers'
36
+ require_relative 'http_stub/server/stub/match/rule/parameters'
37
+ require_relative 'http_stub/server/stub/match/rule/simple_body'
38
+ require_relative 'http_stub/server/stub/match/rule/json_body'
39
+ require_relative 'http_stub/server/stub/match/rule/body'
41
40
  require_relative 'http_stub/server/stub/response/base'
42
41
  require_relative 'http_stub/server/stub/response/text'
43
42
  require_relative 'http_stub/server/stub/response/file'
44
43
  require_relative 'http_stub/server/stub/response'
44
+ require_relative 'http_stub/server/response'
45
45
  require_relative 'http_stub/server/stub/triggers'
46
- require_relative 'http_stub/server/stub/instance'
46
+ require_relative 'http_stub/server/stub/payload_file_consolidator'
47
+ require_relative 'http_stub/server/stub/parser'
48
+ require_relative 'http_stub/server/stub/stub'
49
+ require_relative 'http_stub/server/stub/empty'
50
+ require_relative 'http_stub/server/stub/match/match'
47
51
  require_relative 'http_stub/server/stub/registry'
48
52
  require_relative 'http_stub/server/stub/controller'
49
53
  require_relative 'http_stub/server/stub'
50
- require_relative 'http_stub/server/scenario/request_parser'
51
- require_relative 'http_stub/server/scenario/instance'
54
+ require_relative 'http_stub/server/scenario/parser'
55
+ require_relative 'http_stub/server/scenario/scenario'
52
56
  require_relative 'http_stub/server/scenario/activator'
53
57
  require_relative 'http_stub/server/scenario/controller'
54
58
  require_relative 'http_stub/server/scenario'
55
- require_relative 'http_stub/server/response'
59
+ require_relative 'http_stub/server/request_pipeline'
56
60
  require_relative 'http_stub/server/response_pipeline'
57
61
  require_relative 'http_stub/server/application'
58
62
  require_relative 'http_stub/server/daemon'
@@ -79,5 +83,6 @@ require_relative 'http_stub/configurer/dsl/endpoint_template'
79
83
  require_relative 'http_stub/configurer/dsl/stub_activator_builder'
80
84
  require_relative 'http_stub/configurer/dsl/server'
81
85
  require_relative 'http_stub/configurer/dsl/deprecated'
86
+ require_relative 'http_stub/configurer/dsl/deprecated'
82
87
  require_relative 'http_stub/configurer/part'
83
88
  require_relative 'http_stub/configurer'
@@ -12,6 +12,7 @@ module HttpStub
12
12
  begin
13
13
  response = Net::HTTP.start(host, port) { |http| http.request(command.http_request) }
14
14
  raise "#{error_message_prefix(command)}: #{response.code} #{response.message}" unless response.code == "200"
15
+ response
15
16
  rescue Exception => exc
16
17
  raise "#{error_message_prefix(command)}: #{exc}"
17
18
  end
@@ -9,10 +9,13 @@ module HttpStub
9
9
 
10
10
  def initialize
11
11
  super()
12
- @stub_registry = HttpStub::Server::Stub::Registry.new
12
+ @match_registry = HttpStub::Server::Registry.new("match")
13
+ @stub_registry = HttpStub::Server::Stub::Registry.new(@match_registry)
13
14
  @scenario_registry = HttpStub::Server::Registry.new("scenario")
14
15
  @stub_controller = HttpStub::Server::Stub::Controller.new(@stub_registry)
15
16
  @scenario_controller = HttpStub::Server::Scenario::Controller.new(@scenario_registry, @stub_registry)
17
+ @request_pipeline =
18
+ HttpStub::Server::RequestPipeline.new(@stub_controller, @scenario_controller, @match_registry)
16
19
  end
17
20
 
18
21
  private
@@ -23,29 +26,15 @@ module HttpStub
23
26
  SUPPORTED_REQUEST_TYPES.each { |type| self.send(type, path, opts, &block) }
24
27
  end
25
28
 
26
- before { @response_pipeline = HttpStub::Server::ResponsePipeline.new(self) }
29
+ before do
30
+ @response_pipeline = HttpStub::Server::ResponsePipeline.new(self)
31
+ @http_stub_request = HttpStub::Server::Request.new(request)
32
+ end
27
33
 
28
34
  public
29
35
 
30
- # Sample request body:
31
- # {
32
- # "uri": "/some/path",
33
- # "method": "get",
34
- # "headers": {
35
- # "key": "value",
36
- # ...
37
- # },
38
- # "parameters": {
39
- # "key": "value",
40
- # ...
41
- # },
42
- # "response": {
43
- # "status": "200",
44
- # "body": "Hello World"
45
- # }
46
- # }
47
36
  post "/stubs" do
48
- response = @stub_controller.register(request)
37
+ response = @stub_controller.register(@http_stub_request, logger)
49
38
  @response_pipeline.process(response)
50
39
  end
51
40
 
@@ -54,7 +43,7 @@ module HttpStub
54
43
  end
55
44
 
56
45
  delete "/stubs" do
57
- @stub_controller.clear(request)
46
+ @stub_controller.clear(logger)
58
47
  halt 200, "OK"
59
48
  end
60
49
 
@@ -68,21 +57,12 @@ module HttpStub
68
57
  halt 200, "OK"
69
58
  end
70
59
 
71
- # Sample request body:
72
- # {
73
- # "name": "some_scenario_name",
74
- # "stubs": [
75
- # {
76
- # ... see /stub ...
77
- # },
78
- # ],
79
- # "triggered_scenario_names": [
80
- # "some_other_scenario_name",
81
- # ...
82
- # ]
83
- # }
60
+ get "/stubs/matches" do
61
+ haml :matches, {}, matches: @match_registry.all
62
+ end
63
+
84
64
  post "/stubs/scenarios" do
85
- response = @scenario_controller.register(request)
65
+ response = @scenario_controller.register(@http_stub_request, logger)
86
66
  @response_pipeline.process(response)
87
67
  end
88
68
 
@@ -91,15 +71,22 @@ module HttpStub
91
71
  end
92
72
 
93
73
  delete "/stubs/scenarios" do
94
- @scenario_controller.clear(request)
74
+ @scenario_controller.clear(logger)
95
75
  halt 200, "OK"
96
76
  end
97
77
 
78
+ get "/stubs/:id" do
79
+ haml :stub, {}, the_stub: @stub_registry.find(params[:id], logger)
80
+ end
81
+
98
82
  get "/application.css" do
99
83
  sass :application
100
84
  end
101
85
 
102
- any_request_type(//) { handle_request }
86
+ any_request_type(//) do
87
+ response = @request_pipeline.process(@http_stub_request, logger)
88
+ @response_pipeline.process(response)
89
+ end
103
90
 
104
91
  helpers do
105
92
 
@@ -109,15 +96,6 @@ module HttpStub
109
96
 
110
97
  end
111
98
 
112
- private
113
-
114
- def handle_request
115
- response = @stub_controller.replay(request)
116
- response = @scenario_controller.activate(request) if response.empty?
117
- response = HttpStub::Server::Response::ERROR if response.empty?
118
- @response_pipeline.process(response)
119
- end
120
-
121
99
  end
122
100
 
123
101
  end
@@ -0,0 +1,18 @@
1
+ module HttpStub
2
+ module Server
3
+
4
+ class FormattedHash < Hash
5
+
6
+ def initialize(hash, key_value_delimiter)
7
+ self.merge!(hash || {})
8
+ @key_value_delimiter = key_value_delimiter
9
+ end
10
+
11
+ def to_s
12
+ self.map { |key_and_value| key_and_value.map(&:to_s).join(@key_value_delimiter) }.join(", ")
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module HttpStub
2
+ module Server
3
+
4
+ class HeaderParser
5
+
6
+ def self.parse(request)
7
+ request.env.reduce(HttpStub::Server::FormattedHash.new({}, ":")) do |result, element|
8
+ match = element[0].match(/^(?:HTTP_)?([A-Z0-9_]+)$/)
9
+ result[match[1]] = element[1] if match
10
+ result
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -8,18 +8,18 @@ module HttpStub
8
8
  @models = []
9
9
  end
10
10
 
11
- def add(model, request)
11
+ def add(model, logger)
12
12
  @models.unshift(model)
13
- request.logger.info "Registered #{@model_name}: #{model}"
13
+ logger.info "Registered #{@model_name}: #{model}"
14
14
  end
15
15
 
16
- def concat(models, request)
17
- models.each { |model| self.add(model, request) }
16
+ def concat(models, logger)
17
+ models.each { |model| self.add(model, logger) }
18
18
  end
19
19
 
20
- def find(args)
21
- args[:request].logger.info "Finding #{@model_name} satisfying: #{args[:criteria]}"
22
- @models.find { |model| model.satisfies?(args[:criteria]) }
20
+ def find(criteria, logger)
21
+ logger.info "Finding #{@model_name} matching: #{criteria}"
22
+ @models.find { |model| model.matches?(criteria, logger) }
23
23
  end
24
24
 
25
25
  def last
@@ -35,8 +35,8 @@ module HttpStub
35
35
  @models = @models.slice(starting_index..-1) if starting_index
36
36
  end
37
37
 
38
- def clear(request)
39
- request.logger.info "Clearing #{@model_name} registry"
38
+ def clear(logger)
39
+ logger.info "Clearing #{@model_name} registry"
40
40
  @models.clear
41
41
  end
42
42
 
@@ -0,0 +1,19 @@
1
+ module HttpStub
2
+ module Server
3
+
4
+ class Request
5
+
6
+ attr_reader :uri, :method, :headers, :parameters, :body
7
+
8
+ def initialize(request)
9
+ @uri = request.path_info
10
+ @method = request.request_method.downcase
11
+ @headers = HttpStub::Server::FormattedHash.new(HttpStub::Server::HeaderParser.parse(request), ":")
12
+ @parameters = HttpStub::Server::FormattedHash.new(request.params, "=")
13
+ @body = request.body.read
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module HttpStub
2
+ module Server
3
+
4
+ class RequestPipeline
5
+
6
+ def initialize(stub_controller, scenario_controller, match_registry)
7
+ @stub_controller = stub_controller
8
+ @scenario_controller = scenario_controller
9
+ @match_registry = match_registry
10
+ end
11
+
12
+ def process(request, logger)
13
+ response = @stub_controller.replay(request, logger)
14
+ response = @scenario_controller.activate(request, logger) if response.empty?
15
+ response.empty? ? process_unmatched(request, logger) : response
16
+ end
17
+
18
+ private
19
+
20
+ def process_unmatched(request, logger)
21
+ @match_registry.add(HttpStub::Server::Stub::Match::Match.new(nil, request), logger)
22
+ HttpStub::Server::Response::NOT_FOUND
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -3,9 +3,13 @@ module HttpStub
3
3
 
4
4
  class Response
5
5
 
6
- SUCCESS = HttpStub::Server::Stub::Response::Text.new("status" => 200, "body" => "OK").freeze
7
- ERROR = HttpStub::Server::Stub::Response::Text.new("status" => 404, "body" => "ERROR").freeze
8
- EMPTY = HttpStub::Server::Stub::Response::Text.new.freeze
6
+ def self.success(headers={})
7
+ HttpStub::Server::Stub::Response::Text.new("status" => 200, "headers" => headers, "body" => "OK")
8
+ end
9
+
10
+ SUCCESS = success.freeze
11
+ NOT_FOUND = HttpStub::Server::Stub::Response::Text.new("status" => 404, "body" => "NOT FOUND").freeze
12
+ EMPTY = HttpStub::Server::Stub::Response::Text.new.freeze
9
13
 
10
14
  end
11
15
 
@@ -4,7 +4,7 @@ module HttpStub
4
4
  module Scenario
5
5
 
6
6
  def self.create(args)
7
- HttpStub::Server::Scenario::Instance.new(args)
7
+ HttpStub::Server::Scenario::Scenario.new(args)
8
8
  end
9
9
 
10
10
  end
@@ -9,12 +9,12 @@ module HttpStub
9
9
  @stub_registry = stub_registry
10
10
  end
11
11
 
12
- def activate(scenario, request)
13
- @stub_registry.concat(scenario.stubs, request)
12
+ def activate(scenario, logger)
13
+ @stub_registry.concat(scenario.stubs, logger)
14
14
  scenario.triggered_scenario_names.each do |triggered_scenario_name|
15
- scenario_to_activate = @scenario_registry.find(criteria: triggered_scenario_name, request: request)
15
+ scenario_to_activate = @scenario_registry.find(triggered_scenario_name, logger)
16
16
  raise "Scenario not found with name '#{triggered_scenario_name}'" unless scenario_to_activate
17
- activate(scenario_to_activate, request)
17
+ activate(scenario_to_activate, logger)
18
18
  end
19
19
  end
20
20
 
@@ -9,24 +9,24 @@ module HttpStub
9
9
  @scenario_activator = HttpStub::Server::Scenario::Activator.new(scenario_registry, stub_registry)
10
10
  end
11
11
 
12
- def register(request)
13
- scenario = HttpStub::Server::Scenario.create(HttpStub::Server::Scenario::RequestParser.parse(request))
14
- @scenario_registry.add(scenario, request)
12
+ def register(request, logger)
13
+ scenario = HttpStub::Server::Scenario.create(HttpStub::Server::Scenario::Parser.parse(request))
14
+ @scenario_registry.add(scenario, logger)
15
15
  HttpStub::Server::Response::SUCCESS
16
16
  end
17
17
 
18
- def activate(request)
19
- scenario = @scenario_registry.find(criteria: request.path_info.gsub(/^\//, ""), request: request)
18
+ def activate(request, logger)
19
+ scenario = @scenario_registry.find(request.uri.gsub(/^\//, ""), logger)
20
20
  if scenario
21
- @scenario_activator.activate(scenario, request)
21
+ @scenario_activator.activate(scenario, logger)
22
22
  HttpStub::Server::Response::SUCCESS
23
23
  else
24
24
  HttpStub::Server::Response::EMPTY
25
25
  end
26
26
  end
27
27
 
28
- def clear(request)
29
- @scenario_registry.clear(request)
28
+ def clear(logger)
29
+ @scenario_registry.clear(logger)
30
30
  end
31
31
 
32
32
  end
@@ -2,10 +2,10 @@ module HttpStub
2
2
  module Server
3
3
  module Scenario
4
4
 
5
- class RequestParser
5
+ class Parser
6
6
 
7
7
  def self.parse(request)
8
- JSON.parse(request.params["payload"]).tap do |payload|
8
+ JSON.parse(request.parameters["payload"]).tap do |payload|
9
9
  payload["stubs"].each do |stub_payload|
10
10
  HttpStub::Server::Stub::PayloadFileConsolidator.consolidate!(stub_payload, request)
11
11
  end