http_stub 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -1,15 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class ExactValueMatcher
6
-
7
- def self.match?(stub_value, actual_value)
8
- stub_value == actual_value
9
- end
10
-
11
- end
12
-
13
- end
14
- end
15
- end
@@ -1,22 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class HashWithStringValueMatchers < Hash
6
-
7
- def initialize(stub_hash)
8
- stub_hash.each_pair { |key, value| self[key] = HttpStub::Server::Stub::StringValueMatcher.new(value) }
9
- end
10
-
11
- def match?(actual_hash)
12
- !(self.find do |key_and_value_matcher|
13
- key, value_matcher = key_and_value_matcher
14
- !value_matcher.match?(actual_hash[key])
15
- end)
16
- end
17
-
18
- end
19
-
20
- end
21
- end
22
- end
@@ -1,19 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class Headers < Hash
6
-
7
- def initialize(headers)
8
- self.merge!(headers || {})
9
- end
10
-
11
- def to_s
12
- self.map { |key_and_value| key_and_value.map(&:to_s).join(":") }.join(", ")
13
- end
14
-
15
- end
16
-
17
- end
18
- end
19
- end
@@ -1,32 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class Instance
6
-
7
- attr_reader :method, :uri, :headers, :parameters, :body, :response, :triggers
8
-
9
- def initialize(args)
10
- @method = HttpStub::Server::Stub::Method.new(args["method"])
11
- @uri = HttpStub::Server::Stub::Uri.new(args["uri"])
12
- @headers = HttpStub::Server::Stub::RequestHeaders.new(args["headers"])
13
- @parameters = HttpStub::Server::Stub::RequestParameters.new(args["parameters"])
14
- @body = HttpStub::Server::Stub::RequestBody.create(args["body"])
15
- @response = HttpStub::Server::Stub::Response.create(args["response"])
16
- @triggers = HttpStub::Server::Stub::Triggers.new(args["triggers"])
17
- @description = args.to_s
18
- end
19
-
20
- def satisfies?(request)
21
- [ @uri, @method, @headers, @parameters, @body ].all? { |matcher| matcher.match?(request) }
22
- end
23
-
24
- def to_s
25
- @description
26
- end
27
-
28
- end
29
-
30
- end
31
- end
32
- end
@@ -1,35 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class JsonRequestBody
6
-
7
- def initialize(schema_definition)
8
- @schema_definition = schema_definition
9
- end
10
-
11
- def match?(request)
12
- validate_against_schema(request).tap do |errors|
13
- errors.each { |error| request.logger.info(error) }
14
- end.empty?
15
- end
16
-
17
- def to_s
18
- @schema_definition.to_json
19
- end
20
-
21
- private
22
-
23
- def validate_against_schema(request)
24
- begin
25
- JSON::Validator.fully_validate(@schema_definition, request.body.read, validate_schema: true, json: true)
26
- rescue Exception => exc
27
- [ exc.message ]
28
- end
29
- end
30
-
31
- end
32
-
33
- end
34
- end
35
- end
@@ -1,23 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class Method
6
-
7
- def initialize(method)
8
- @method = method
9
- end
10
-
11
- def match?(request)
12
- @method.blank? || @method.downcase == request.request_method.downcase
13
- end
14
-
15
- def to_s
16
- @method || ""
17
- end
18
-
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,21 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class OmittedValueMatcher
6
-
7
- private
8
-
9
- OMITTED_CONTROL_VALUE = "control:omitted".freeze
10
-
11
- public
12
-
13
- def self.match?(stub_value, actual_value)
14
- stub_value == OMITTED_CONTROL_VALUE && actual_value.nil?
15
- end
16
-
17
- end
18
-
19
- end
20
- end
21
- end
@@ -1,16 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class RegexpValueMatcher
6
-
7
- def self.match?(stub_value, actual_value)
8
- match_data = stub_value.match(/^regexp:(.*)/)
9
- match_data && !!Regexp.new(match_data[1]).match(actual_value)
10
- end
11
-
12
- end
13
-
14
- end
15
- end
16
- end
@@ -1,44 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class RequestBody
6
-
7
- private
8
-
9
- SCHEMA_PROPERTIES = %w{ type definition }.freeze
10
-
11
- public
12
-
13
- class << self
14
-
15
- def create(body)
16
- matcher = create_schema_request_body(body["schema"]) if body.is_a?(Hash) && body["schema"]
17
- matcher ||= HttpStub::Server::Stub::SimpleRequestBody.new(body) if !body.blank?
18
- matcher || HttpStub::Server::Stub::TruthyRequestMatcher
19
- end
20
-
21
- private
22
-
23
- def create_schema_request_body(schema)
24
- validate_schema_properties(schema)
25
- begin
26
- HttpStub::Server::Stub.const_get("#{schema["type"].capitalize}RequestBody").new(schema["definition"])
27
- rescue NameError
28
- raise "Stub request body schema #{schema} is invalid: #{schema["type"]} schema is not supported"
29
- end
30
- end
31
-
32
- def validate_schema_properties(schema)
33
- SCHEMA_PROPERTIES.each do |property|
34
- raise "Stub request body schema #{schema} is invalid: #{property} expected" unless schema[property]
35
- end
36
- end
37
-
38
- end
39
-
40
- end
41
-
42
- end
43
- end
44
- end
@@ -1,19 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class RequestHeaderParser
6
-
7
- def self.parse(request)
8
- request.env.reduce({}) do |result, element|
9
- match = element[0].match(/^(?:HTTP_)?([A-Z0-9_]+)$/)
10
- result[match[1]] = element[1] if match
11
- result
12
- end
13
- end
14
-
15
- end
16
-
17
- end
18
- end
19
- end
@@ -1,28 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class RequestHeaders < HttpStub::Server::Stub::Headers
6
-
7
- def initialize(headers)
8
- super(headers)
9
- @headers = HttpStub::Server::Stub::HashWithStringValueMatchers.new(
10
- (headers || {}).downcase_and_underscore_keys
11
- )
12
- end
13
-
14
- def match?(request)
15
- @headers.match?(headers_in(request).downcase_and_underscore_keys)
16
- end
17
-
18
- private
19
-
20
- def headers_in(request)
21
- HttpStub::Server::Stub::RequestHeaderParser.parse(request)
22
- end
23
-
24
- end
25
-
26
- end
27
- end
28
- end
@@ -1,23 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class RequestParameters
6
-
7
- def initialize(parameters)
8
- @parameters = HttpStub::Server::Stub::HashWithStringValueMatchers.new(parameters || {})
9
- end
10
-
11
- def match?(request)
12
- @parameters.match?(request.params)
13
- end
14
-
15
- def to_s
16
- @parameters ? @parameters.map { |key_and_value| key_and_value.map(&:to_s).join("=") }.join("&") : ""
17
- end
18
-
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class SimpleRequestBody
6
-
7
- def initialize(body)
8
- @body = HttpStub::Server::Stub::StringValueMatcher.new(body)
9
- end
10
-
11
- def match?(request)
12
- @body.match?(request.body.read)
13
- end
14
-
15
- def to_s
16
- @body.to_s
17
- end
18
-
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,31 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class StringValueMatcher
6
-
7
- private
8
-
9
- MATCHERS = [ HttpStub::Server::Stub::OmittedValueMatcher,
10
- HttpStub::Server::Stub::RegexpValueMatcher,
11
- HttpStub::Server::Stub::ExactValueMatcher ].freeze
12
-
13
- public
14
-
15
- def initialize(stub_value)
16
- @stub_match_value = stub_value ? stub_value.to_s : stub_value
17
- end
18
-
19
- def match?(actual_value)
20
- !!MATCHERS.find { |matcher| matcher.match?(@stub_match_value, actual_value) }
21
- end
22
-
23
- def to_s
24
- @stub_match_value
25
- end
26
-
27
- end
28
-
29
- end
30
- end
31
- end
@@ -1,23 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class TruthyRequestMatcher
6
-
7
- class << self
8
-
9
- def match?(_request)
10
- true
11
- end
12
-
13
- def to_s
14
- ""
15
- end
16
-
17
- end
18
-
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- module HttpStub
2
- module Server
3
- module Stub
4
-
5
- class Uri
6
-
7
- def initialize(uri)
8
- @uri = HttpStub::Server::Stub::StringValueMatcher.new(uri)
9
- end
10
-
11
- def match?(request)
12
- @uri.match?(request.path_info)
13
- end
14
-
15
- def to_s
16
- @uri.to_s
17
- end
18
-
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,51 +0,0 @@
1
- describe HttpStub::Server::Stub::Headers do
2
-
3
- let(:header_hash) { {} }
4
-
5
- let(:headers) { HttpStub::Server::Stub::Headers.new(header_hash) }
6
-
7
- it "is a Hash" do
8
- expect(headers).to be_a(Hash)
9
- end
10
-
11
- describe "#to_s" do
12
-
13
- describe "when multiple headers are provided" do
14
-
15
- let(:header_hash) { { "key1" => "value1", "key2" => "value2", "key3" => "value3" } }
16
-
17
- it "returns a string containing each header formatted as a conventional request header" do
18
- result = headers.to_s
19
-
20
- header_hash.each { |key, value| expect(result).to match(/#{key}:#{value}/) }
21
- end
22
-
23
- it "comma delimits the headers" do
24
- expect(headers.to_s).to match(/key\d.value\d\, key\d.value\d\, key\d.value\d/)
25
- end
26
-
27
- end
28
-
29
- describe "when empty headers are provided" do
30
-
31
- let(:header_hash) { {} }
32
-
33
- it "returns an empty string" do
34
- expect(headers.to_s).to eql("")
35
- end
36
-
37
- end
38
-
39
- describe "when nil headers are provided" do
40
-
41
- let(:header_hash) { nil }
42
-
43
- it "returns an empty string" do
44
- expect(headers.to_s).to eql("")
45
- end
46
-
47
- end
48
-
49
- end
50
-
51
- end