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
@@ -2,7 +2,7 @@ module HttpStub
2
2
  module Server
3
3
  module Scenario
4
4
 
5
- class Instance
5
+ class Scenario
6
6
 
7
7
  attr_reader :name, :stubs, :triggered_scenario_names
8
8
 
@@ -13,7 +13,7 @@ module HttpStub
13
13
  @triggered_scenario_names = @args["triggered_scenario_names"]
14
14
  end
15
15
 
16
- def satisfies?(name)
16
+ def matches?(name, _logger)
17
17
  @name == name
18
18
  end
19
19
 
@@ -4,7 +4,7 @@ module HttpStub
4
4
  module Stub
5
5
 
6
6
  def self.create(args)
7
- HttpStub::Server::Stub::Instance.new(args)
7
+ HttpStub::Server::Stub::Stub.new(args)
8
8
  end
9
9
 
10
10
  end
@@ -8,19 +8,19 @@ module HttpStub
8
8
  @registry = registry
9
9
  end
10
10
 
11
- def register(request)
12
- stub = HttpStub::Server::Stub.create(HttpStub::Server::Stub::RequestParser.parse(request))
13
- @registry.add(stub, request)
14
- HttpStub::Server::Response::SUCCESS
11
+ def register(request, logger)
12
+ stub = HttpStub::Server::Stub.create(HttpStub::Server::Stub::Parser.parse(request))
13
+ @registry.add(stub, logger)
14
+ HttpStub::Server::Response.success("location" => stub.stub_uri)
15
15
  end
16
16
 
17
- def replay(request)
18
- stub = @registry.find_for(request)
17
+ def replay(request, logger)
18
+ stub = @registry.find(request, logger)
19
19
  stub ? stub.response : HttpStub::Server::Response::EMPTY
20
20
  end
21
21
 
22
- def clear(request)
23
- @registry.clear(request)
22
+ def clear(logger)
23
+ @registry.clear(logger)
24
24
  end
25
25
 
26
26
  end
@@ -0,0 +1,34 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+
5
+ class Empty
6
+
7
+ private
8
+
9
+ def initialize
10
+ @uri = @method = @body = @stub_uri = ""
11
+ @headers = @parameters = {}
12
+ @triggers = []
13
+ @response = HttpStub::Server::Response::EMPTY
14
+ end
15
+
16
+ public
17
+
18
+ attr_reader :uri, :method, :headers, :parameters, :body, :response, :triggers, :stub_uri
19
+
20
+ def matches?(_criteria, _logger)
21
+ false
22
+ end
23
+
24
+ def to_s
25
+ ""
26
+ end
27
+
28
+ INSTANCE = self.new.freeze
29
+
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,17 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+
6
+ class ExactValueMatcher
7
+
8
+ def self.match?(stub_value, actual_value)
9
+ stub_value == actual_value
10
+ end
11
+
12
+ end
13
+
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+
6
+ class HashWithStringValueMatchers < Hash
7
+
8
+ def initialize(stub_hash)
9
+ stub_hash.each_pair do |key, value|
10
+ self[key] = HttpStub::Server::Stub::Match::StringValueMatcher.new(value)
11
+ end
12
+ end
13
+
14
+ def matches?(actual_hash)
15
+ !(self.find do |key_and_value_matcher|
16
+ key, value_matcher = key_and_value_matcher
17
+ !value_matcher.matches?(actual_hash[key])
18
+ end)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+
6
+ class Match
7
+
8
+ attr_reader :stub, :request
9
+
10
+ def initialize(stub, request)
11
+ @stub = stub || HttpStub::Server::Stub::Empty::INSTANCE
12
+ @request = request
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+
6
+ class OmittedValueMatcher
7
+
8
+ private
9
+
10
+ OMITTED_CONTROL_VALUE = "control:omitted".freeze
11
+
12
+ public
13
+
14
+ def self.match?(stub_value, actual_value)
15
+ stub_value == OMITTED_CONTROL_VALUE && actual_value.nil?
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+
6
+ class RegexpValueMatcher
7
+
8
+ def self.match?(stub_value, actual_value)
9
+ match_data = stub_value.match(/^regexp:(.*)/)
10
+ match_data && !!Regexp.new(match_data[1]).match(actual_value)
11
+ end
12
+
13
+ end
14
+
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,49 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class Body
8
+
9
+ private
10
+
11
+ SCHEMA_PROPERTIES = %w{ type definition }.freeze
12
+
13
+ public
14
+
15
+ class << self
16
+
17
+ def create(body)
18
+ matcher = create_schema_body(body["schema"]) if body.is_a?(Hash) && body["schema"]
19
+ matcher ||= HttpStub::Server::Stub::Match::Rule::SimpleBody.new(body) if body.present?
20
+ matcher || HttpStub::Server::Stub::Match::TruthyMatcher
21
+ end
22
+
23
+ private
24
+
25
+ def create_schema_body(schema)
26
+ validate_schema_properties(schema)
27
+ begin
28
+ body_class = HttpStub::Server::Stub::Match::Rule.const_get("#{schema["type"].capitalize}Body")
29
+ body_class.new(schema["definition"])
30
+ rescue NameError
31
+ raise "Stub request body schema #{schema} is invalid: #{schema["type"]} schema is not supported"
32
+ end
33
+ end
34
+
35
+ def validate_schema_properties(schema)
36
+ SCHEMA_PROPERTIES.each do |property|
37
+ raise "Stub request body schema #{schema} is invalid: #{property} expected" unless schema[property]
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,31 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class Headers
8
+
9
+ def initialize(headers)
10
+ original_headers = headers || {}
11
+ @formatted_headers = HttpStub::Server::FormattedHash.new(original_headers, ":")
12
+ @matchable_headers = HttpStub::Server::Stub::Match::HashWithStringValueMatchers.new(
13
+ original_headers.downcase_and_underscore_keys
14
+ )
15
+ end
16
+
17
+ def matches?(request, _logger)
18
+ @matchable_headers.matches?(request.headers.downcase_and_underscore_keys)
19
+ end
20
+
21
+ def to_s
22
+ @formatted_headers.to_s
23
+ end
24
+
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,39 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class JsonBody
8
+
9
+ def initialize(schema_definition)
10
+ @schema_definition = schema_definition
11
+ end
12
+
13
+ def matches?(request, logger)
14
+ validate_against_schema(request).tap do |errors|
15
+ errors.each { |error| logger.info(error) }
16
+ end.empty?
17
+ end
18
+
19
+ def to_s
20
+ @schema_definition.to_json
21
+ end
22
+
23
+ private
24
+
25
+ def validate_against_schema(request)
26
+ begin
27
+ JSON::Validator.fully_validate(@schema_definition, request.body, validate_schema: true, json: true)
28
+ rescue Exception => exc
29
+ [ exc.message ]
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class Method
8
+
9
+ def initialize(method)
10
+ @method = method
11
+ end
12
+
13
+ def matches?(request, _logger)
14
+ @method.blank? || @method.downcase == request.method.downcase
15
+ end
16
+
17
+ def to_s
18
+ @method || ""
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class Parameters
8
+
9
+ def initialize(parameters)
10
+ @parameters = HttpStub::Server::Stub::Match::HashWithStringValueMatchers.new(parameters || {})
11
+ end
12
+
13
+ def matches?(request, _logger)
14
+ @parameters.matches?(request.parameters)
15
+ end
16
+
17
+ def to_s
18
+ @parameters ? @parameters.map { |key_and_value| key_and_value.map(&:to_s).join("=") }.join("&") : ""
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class SimpleBody
8
+
9
+ def initialize(body)
10
+ @body = HttpStub::Server::Stub::Match::StringValueMatcher.new(body)
11
+ end
12
+
13
+ def matches?(request, _logger)
14
+ @body.matches?(request.body)
15
+ end
16
+
17
+ def to_s
18
+ @body.to_s
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class Uri
8
+
9
+ def initialize(uri)
10
+ @uri = HttpStub::Server::Stub::Match::StringValueMatcher.new(uri)
11
+ end
12
+
13
+ def matches?(request, _logger)
14
+ @uri.matches?(request.uri)
15
+ end
16
+
17
+ def to_s
18
+ @uri.to_s
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end