http_stub 0.22.4 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/lib/http_stub.rb +15 -5
  3. data/lib/http_stub/configurer/dsl/request_attribute_referencer.rb +19 -0
  4. data/lib/http_stub/configurer/dsl/request_referencer.rb +24 -0
  5. data/lib/http_stub/configurer/dsl/scenario_activator.rb +2 -2
  6. data/lib/http_stub/configurer/dsl/scenario_builder.rb +2 -2
  7. data/lib/http_stub/configurer/dsl/server.rb +4 -4
  8. data/lib/http_stub/configurer/dsl/stub_builder.rb +5 -3
  9. data/lib/http_stub/configurer/request/controllable_value.rb +1 -3
  10. data/lib/http_stub/configurer/request/http/factory.rb +2 -2
  11. data/lib/http_stub/configurer/request/omittable.rb +2 -4
  12. data/lib/http_stub/configurer/request/regexpable.rb +3 -8
  13. data/lib/http_stub/configurer/server/command_processor.rb +5 -7
  14. data/lib/http_stub/configurer/server/facade.rb +2 -4
  15. data/lib/http_stub/extensions/core/hash.rb +4 -11
  16. data/lib/http_stub/extensions/core/hash/formatted.rb +23 -0
  17. data/lib/http_stub/extensions/core/hash/indifferent_and_insensitive_access.rb +24 -0
  18. data/lib/http_stub/extensions/core/hash/with_indifferent_and_insensitive_access.rb +18 -0
  19. data/lib/http_stub/extensions/rack/handler.rb +1 -1
  20. data/lib/http_stub/rake/task_generators.rb +1 -1
  21. data/lib/http_stub/server/application.rb +10 -11
  22. data/lib/http_stub/server/application_helpers.rb +3 -5
  23. data/lib/http_stub/server/daemon.rb +5 -6
  24. data/lib/http_stub/server/request.rb +3 -10
  25. data/lib/http_stub/server/request/headers.rb +25 -0
  26. data/lib/http_stub/server/request/parameters.rb +21 -0
  27. data/lib/http_stub/server/request/request.rb +21 -0
  28. data/lib/http_stub/server/scenario/links.rb +1 -1
  29. data/lib/http_stub/server/stub/controller.rb +1 -1
  30. data/lib/http_stub/server/stub/empty.rb +4 -0
  31. data/lib/http_stub/server/stub/match/hash_matcher.rb +19 -0
  32. data/lib/http_stub/server/stub/match/omitted_value_matcher.rb +1 -3
  33. data/lib/http_stub/server/stub/match/rule/body.rb +2 -4
  34. data/lib/http_stub/server/stub/match/rule/headers.rb +5 -11
  35. data/lib/http_stub/server/stub/match/rule/json_body.rb +3 -5
  36. data/lib/http_stub/server/stub/match/rule/method.rb +1 -1
  37. data/lib/http_stub/server/stub/match/rule/parameters.rb +4 -7
  38. data/lib/http_stub/server/stub/match/rule/simple_body.rb +3 -3
  39. data/lib/http_stub/server/stub/match/rule/truthy.rb +27 -0
  40. data/lib/http_stub/server/stub/match/rule/uri.rb +3 -3
  41. data/lib/http_stub/server/stub/match/string_value_matcher.rb +4 -13
  42. data/lib/http_stub/server/stub/response.rb +5 -2
  43. data/lib/http_stub/server/stub/response/attribute/body.rb +32 -0
  44. data/lib/http_stub/server/stub/response/attribute/headers.rb +26 -0
  45. data/lib/http_stub/server/stub/response/attribute/interpolator.rb +26 -0
  46. data/lib/http_stub/server/stub/response/attribute/interpolator/headers.rb +27 -0
  47. data/lib/http_stub/server/stub/response/attribute/interpolator/parameters.rb +27 -0
  48. data/lib/http_stub/server/stub/response/base.rb +5 -16
  49. data/lib/http_stub/server/stub/response/file.rb +6 -2
  50. data/lib/http_stub/server/stub/response/text.rb +12 -0
  51. data/lib/http_stub/server/stub/stub.rb +4 -0
  52. data/lib/http_stub/version.rb +1 -1
  53. data/spec/acceptance/request_reference_spec.rb +44 -0
  54. data/spec/lib/http_stub/configurer/dsl/request_attribute_referencer_spec.rb +37 -0
  55. data/spec/lib/http_stub/configurer/dsl/request_referencer_spec.rb +37 -0
  56. data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +14 -14
  57. data/spec/lib/http_stub/configurer/dsl/server_spec.rb +2 -6
  58. data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +34 -8
  59. data/spec/lib/http_stub/{server/formatted_hash_spec.rb → extensions/core/hash/formatted_spec.rb} +4 -6
  60. data/spec/lib/http_stub/{hash_with_indifferent_and_insensitive_access_spec.rb → extensions/core/hash/indifferent_and_insensitive_access_spec.rb} +8 -4
  61. data/spec/lib/http_stub/extensions/core/hash/with_indifferent_and_insensitive_access_spec.rb +41 -0
  62. data/spec/lib/http_stub/extensions/core/hash_spec.rb +11 -129
  63. data/spec/lib/http_stub/server/application_spec.rb +1 -1
  64. data/spec/lib/http_stub/server/payload_file_consolidator_spec.rb +1 -1
  65. data/spec/lib/http_stub/server/request/headers_spec.rb +89 -0
  66. data/spec/lib/http_stub/server/request/parameters_spec.rb +60 -0
  67. data/spec/lib/http_stub/server/request/request_spec.rb +84 -0
  68. data/spec/lib/http_stub/server/request_spec.rb +9 -98
  69. data/spec/lib/http_stub/server/response_spec.rb +2 -6
  70. data/spec/lib/http_stub/server/scenario/controller_spec.rb +1 -1
  71. data/spec/lib/http_stub/server/scenario/parser_spec.rb +3 -1
  72. data/spec/lib/http_stub/server/stub/controller_spec.rb +11 -4
  73. data/spec/lib/http_stub/server/stub/empty_spec.rb +10 -0
  74. data/spec/lib/http_stub/server/stub/match/hash_matcher_spec.rb +122 -0
  75. data/spec/lib/http_stub/server/stub/match/rule/body_spec.rb +4 -4
  76. data/spec/lib/http_stub/server/stub/match/rule/headers_spec.rb +54 -45
  77. data/spec/lib/http_stub/server/stub/match/rule/json_body_spec.rb +5 -5
  78. data/spec/lib/http_stub/server/stub/match/rule/method_spec.rb +19 -17
  79. data/spec/lib/http_stub/server/stub/match/rule/parameters_spec.rb +26 -33
  80. data/spec/lib/http_stub/server/stub/match/rule/simple_body_spec.rb +16 -25
  81. data/spec/lib/http_stub/server/stub/match/{truthy_matcher_spec.rb → rule/truthy_spec.rb} +3 -5
  82. data/spec/lib/http_stub/server/stub/match/rule/uri_spec.rb +16 -25
  83. data/spec/lib/http_stub/server/stub/match/string_value_matcher_spec.rb +2 -12
  84. data/spec/lib/http_stub/server/stub/parser_spec.rb +5 -3
  85. data/spec/lib/http_stub/server/stub/response/attribute/body_spec.rb +98 -0
  86. data/spec/lib/http_stub/server/stub/response/attribute/headers_spec.rb +57 -0
  87. data/spec/lib/http_stub/server/stub/response/attribute/interpolator/headers_spec.rb +53 -0
  88. data/spec/lib/http_stub/server/stub/response/attribute/interpolator/parameters_spec.rb +53 -0
  89. data/spec/lib/http_stub/server/stub/response/attribute/interpolator_spec.rb +41 -0
  90. data/spec/lib/http_stub/server/stub/response/base_spec.rb +6 -53
  91. data/spec/lib/http_stub/server/stub/response/file_spec.rb +48 -7
  92. data/spec/lib/http_stub/server/stub/response/text_spec.rb +66 -1
  93. data/spec/lib/http_stub/server/stub/stub_spec.rb +25 -4
  94. data/spec/spec_helper.rb +1 -0
  95. data/spec/support/http_stub/server/request_fixture.rb +1 -1
  96. metadata +86 -26
  97. data/lib/http_stub/hash_with_indifferent_and_insensitive_access.rb +0 -18
  98. data/lib/http_stub/server/formatted_hash.rb +0 -18
  99. data/lib/http_stub/server/header_parser.rb +0 -17
  100. data/lib/http_stub/server/stub/match/hash_with_string_value_matchers.rb +0 -26
  101. data/lib/http_stub/server/stub/match/truthy_matcher.rb +0 -25
  102. data/spec/lib/http_stub/server/header_parser_spec.rb +0 -51
  103. data/spec/lib/http_stub/server/stub/match/hash_with_string_value_matchers_spec.rb +0 -166
@@ -8,11 +8,9 @@ module HttpStub
8
8
  end
9
9
 
10
10
  def pp(text)
11
- begin
12
- text ? JSON.pretty_generate(JSON.parse(text)) : ""
13
- rescue JSON::ParserError
14
- text
15
- end
11
+ text ? JSON.pretty_generate(JSON.parse(text)) : ""
12
+ rescue JSON::ParserError
13
+ text
16
14
  end
17
15
 
18
16
  end
@@ -17,17 +17,16 @@ module HttpStub
17
17
 
18
18
  def initialize(args)
19
19
  @configurer = args[:configurer]
20
- default_args = @configurer ?
21
- { host: @configurer.stub_server.host, port: @configurer.stub_server.port } : { host: "localhost" }
20
+ default_args = { host: "localhost" }
21
+ default_args = { host: @configurer.stub_server.host, port: @configurer.stub_server.port } if @configurer
22
22
  super(default_args.merge(args))
23
23
  end
24
24
 
25
25
  def start!
26
26
  super
27
- if @configurer
28
- @configurer.initialize!
29
- logger.info "#{@name} initialized"
30
- end
27
+ return unless @configurer
28
+ @configurer.initialize!
29
+ logger.info "#{@name} initialized"
31
30
  end
32
31
 
33
32
  def start_command
@@ -1,19 +1,12 @@
1
1
  module HttpStub
2
2
  module Server
3
3
 
4
- class Request
4
+ module Request
5
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
6
+ def self.create(rack_request)
7
+ HttpStub::Server::Request::Request.new(rack_request)
14
8
  end
15
9
 
16
10
  end
17
-
18
11
  end
19
12
  end
@@ -0,0 +1,25 @@
1
+ module HttpStub
2
+ module Server
3
+ module Request
4
+
5
+ class Headers < ::Hash
6
+ include HttpStub::Extensions::Core::Hash::IndifferentAndInsensitiveAccess
7
+ include HttpStub::Extensions::Core::Hash::Formatted
8
+
9
+ def self.create(rack_request)
10
+ rack_request.env.each_with_object(self.new) do |(name, value), result|
11
+ match = name.match(/^(?:HTTP_)?([A-Z0-9_]+)$/)
12
+ result[match[1]] = value if match
13
+ end
14
+ end
15
+
16
+ def initialize(header_hash={})
17
+ super(header_hash, ":")
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module HttpStub
2
+ module Server
3
+ module Request
4
+
5
+ class Parameters < ::HashWithIndifferentAccess
6
+ include HttpStub::Extensions::Core::Hash::Formatted
7
+
8
+ def self.create(rack_request)
9
+ self.new(rack_request.params)
10
+ end
11
+
12
+ def initialize(parameter_hash={})
13
+ super(parameter_hash, "=", "&")
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module HttpStub
2
+ module Server
3
+ module Request
4
+
5
+ class Request
6
+
7
+ attr_reader :uri, :method, :headers, :parameters, :body
8
+
9
+ def initialize(rack_request)
10
+ @uri = rack_request.path_info
11
+ @method = rack_request.request_method.downcase
12
+ @headers = HttpStub::Server::Request::Headers.create(rack_request)
13
+ @parameters = HttpStub::Server::Request::Parameters.create(rack_request)
14
+ @body = rack_request.body.read
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -9,7 +9,7 @@ module HttpStub
9
9
  end
10
10
 
11
11
  def detail
12
- "/http_stub/scenarios?#{URI.encode_www_form(:name => @name)}"
12
+ "/http_stub/scenarios?#{URI.encode_www_form(name: @name)}"
13
13
  end
14
14
 
15
15
  def activate
@@ -16,7 +16,7 @@ module HttpStub
16
16
 
17
17
  def match(request, logger)
18
18
  stub = @registry.match(request, logger)
19
- stub ? stub.response : HttpStub::Server::Response::NOT_FOUND
19
+ stub ? stub.response_for(request) : HttpStub::Server::Response::NOT_FOUND
20
20
  end
21
21
 
22
22
  def clear(logger)
@@ -21,6 +21,10 @@ module HttpStub
21
21
  false
22
22
  end
23
23
 
24
+ def response_for(_request)
25
+ self
26
+ end
27
+
24
28
  def to_s
25
29
  ""
26
30
  end
@@ -0,0 +1,19 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+
6
+ class HashMatcher
7
+
8
+ def self.match?(stub_hash, actual_hash)
9
+ !stub_hash.find do |(key, value)|
10
+ !HttpStub::Server::Stub::Match::StringValueMatcher.match?(value, actual_hash[key])
11
+ end
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
@@ -5,11 +5,9 @@ module HttpStub
5
5
 
6
6
  class OmittedValueMatcher
7
7
 
8
- private
9
-
10
8
  OMITTED_CONTROL_VALUE = "control:omitted".freeze
11
9
 
12
- public
10
+ private_constant :OMITTED_CONTROL_VALUE
13
11
 
14
12
  def self.match?(stub_value, actual_value)
15
13
  stub_value == OMITTED_CONTROL_VALUE && actual_value.nil?
@@ -6,18 +6,16 @@ module HttpStub
6
6
 
7
7
  class Body
8
8
 
9
- private
10
-
11
9
  SCHEMA_PROPERTIES = %w{ type definition }.freeze
12
10
 
13
- public
11
+ private_constant :SCHEMA_PROPERTIES
14
12
 
15
13
  class << self
16
14
 
17
15
  def create(body)
18
16
  matcher = create_schema_body(body["schema"]) if body.is_a?(Hash) && body["schema"]
19
17
  matcher ||= HttpStub::Server::Stub::Match::Rule::SimpleBody.new(body) if body.present?
20
- matcher || HttpStub::Server::Stub::Match::TruthyMatcher
18
+ matcher || HttpStub::Server::Stub::Match::Rule::Truthy
21
19
  end
22
20
 
23
21
  private
@@ -4,22 +4,16 @@ module HttpStub
4
4
  module Match
5
5
  module Rule
6
6
 
7
- class Headers
7
+ class Headers < ::Hash
8
+ include HttpStub::Extensions::Core::Hash::IndifferentAndInsensitiveAccess
9
+ include HttpStub::Extensions::Core::Hash::Formatted
8
10
 
9
11
  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
- )
12
+ super((headers || {}).underscore_keys, ":")
15
13
  end
16
14
 
17
15
  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
16
+ HttpStub::Server::Stub::Match::HashMatcher.match?(self, request.headers)
23
17
  end
24
18
 
25
19
  end
@@ -23,11 +23,9 @@ module HttpStub
23
23
  private
24
24
 
25
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
26
+ JSON::Validator.fully_validate(@schema_definition, request.body, validate_schema: true, json: true)
27
+ rescue StandardError => err
28
+ [ err.message ]
31
29
  end
32
30
 
33
31
  end
@@ -11,7 +11,7 @@ module HttpStub
11
11
  end
12
12
 
13
13
  def matches?(request, _logger)
14
- @method.blank? || @method.downcase == request.method.downcase
14
+ @method.blank? || @method.casecmp(request.method) == 0
15
15
  end
16
16
 
17
17
  def to_s
@@ -4,18 +4,15 @@ module HttpStub
4
4
  module Match
5
5
  module Rule
6
6
 
7
- class Parameters
7
+ class Parameters < ::HashWithIndifferentAccess
8
+ include HttpStub::Extensions::Core::Hash::Formatted
8
9
 
9
10
  def initialize(parameters)
10
- @parameters = HttpStub::Server::Stub::Match::HashWithStringValueMatchers.new(parameters || {})
11
+ super(parameters || {}, "=", "&")
11
12
  end
12
13
 
13
14
  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("&") : ""
15
+ HttpStub::Server::Stub::Match::HashMatcher.match?(self, request.parameters)
19
16
  end
20
17
 
21
18
  end
@@ -7,15 +7,15 @@ module HttpStub
7
7
  class SimpleBody
8
8
 
9
9
  def initialize(body)
10
- @body = HttpStub::Server::Stub::Match::StringValueMatcher.new(body)
10
+ @body = body
11
11
  end
12
12
 
13
13
  def matches?(request, _logger)
14
- @body.matches?(request.body)
14
+ HttpStub::Server::Stub::Match::StringValueMatcher.match?(@body, request.body)
15
15
  end
16
16
 
17
17
  def to_s
18
- @body.to_s
18
+ @body
19
19
  end
20
20
 
21
21
  end
@@ -0,0 +1,27 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Match
5
+ module Rule
6
+
7
+ class Truthy
8
+
9
+ class << self
10
+
11
+ def matches?(_request, _logger)
12
+ true
13
+ end
14
+
15
+ def to_s
16
+ ""
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -7,15 +7,15 @@ module HttpStub
7
7
  class Uri
8
8
 
9
9
  def initialize(uri)
10
- @uri = HttpStub::Server::Stub::Match::StringValueMatcher.new(uri)
10
+ @uri = uri
11
11
  end
12
12
 
13
13
  def matches?(request, _logger)
14
- @uri.matches?(request.uri)
14
+ HttpStub::Server::Stub::Match::StringValueMatcher.match?(@uri, request.uri)
15
15
  end
16
16
 
17
17
  def to_s
18
- @uri.to_s
18
+ @uri
19
19
  end
20
20
 
21
21
  end
@@ -5,24 +5,15 @@ module HttpStub
5
5
 
6
6
  class StringValueMatcher
7
7
 
8
- private
9
-
10
8
  MATCHERS = [ HttpStub::Server::Stub::Match::OmittedValueMatcher,
11
9
  HttpStub::Server::Stub::Match::RegexpValueMatcher,
12
10
  HttpStub::Server::Stub::Match::ExactValueMatcher ].freeze
13
11
 
14
- public
15
-
16
- def initialize(stub_value)
17
- @stub_match_value = stub_value ? stub_value.to_s : stub_value
18
- end
19
-
20
- def matches?(actual_value)
21
- !!MATCHERS.find { |matcher| matcher.match?(@stub_match_value, actual_value) }
22
- end
12
+ private_constant :MATCHERS
23
13
 
24
- def to_s
25
- @stub_match_value
14
+ def self.match?(stub_value, actual_value)
15
+ stub_match_value = stub_value ? stub_value.to_s : stub_value
16
+ !!MATCHERS.find { |matcher| matcher.match?(stub_match_value, actual_value) }
26
17
  end
27
18
 
28
19
  end
@@ -5,8 +5,11 @@ module HttpStub
5
5
  module Response
6
6
 
7
7
  def self.create(args)
8
- args["body"].is_a?(Hash) ?
9
- HttpStub::Server::Stub::Response::File.new(args) : HttpStub::Server::Stub::Response::Text.new(args)
8
+ if args["body"].is_a?(Hash)
9
+ HttpStub::Server::Stub::Response::File.new(args)
10
+ else
11
+ HttpStub::Server::Stub::Response::Text.new(args)
12
+ end
10
13
  end
11
14
 
12
15
  end
@@ -0,0 +1,32 @@
1
+ module HttpStub
2
+ module Server
3
+ module Stub
4
+ module Response
5
+ module Attribute
6
+
7
+ class Body < ::String
8
+
9
+ def initialize(stub_body)
10
+ @stub_body = stub_body
11
+ super stub_body.nil? ? "" : stub_body
12
+ end
13
+
14
+ def with_values_from(request)
15
+ if self.provided?
16
+ HttpStub::Server::Stub::Response::Attribute::Interpolator.interpolate(@stub_body, request)
17
+ else
18
+ nil
19
+ end
20
+ end
21
+
22
+ def provided?
23
+ !@stub_body.nil?
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end