http_stub 0.22.4 → 0.23.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.
- checksums.yaml +4 -4
- data/lib/http_stub.rb +15 -5
- data/lib/http_stub/configurer/dsl/request_attribute_referencer.rb +19 -0
- data/lib/http_stub/configurer/dsl/request_referencer.rb +24 -0
- data/lib/http_stub/configurer/dsl/scenario_activator.rb +2 -2
- data/lib/http_stub/configurer/dsl/scenario_builder.rb +2 -2
- data/lib/http_stub/configurer/dsl/server.rb +4 -4
- data/lib/http_stub/configurer/dsl/stub_builder.rb +5 -3
- data/lib/http_stub/configurer/request/controllable_value.rb +1 -3
- data/lib/http_stub/configurer/request/http/factory.rb +2 -2
- data/lib/http_stub/configurer/request/omittable.rb +2 -4
- data/lib/http_stub/configurer/request/regexpable.rb +3 -8
- data/lib/http_stub/configurer/server/command_processor.rb +5 -7
- data/lib/http_stub/configurer/server/facade.rb +2 -4
- data/lib/http_stub/extensions/core/hash.rb +4 -11
- data/lib/http_stub/extensions/core/hash/formatted.rb +23 -0
- data/lib/http_stub/extensions/core/hash/indifferent_and_insensitive_access.rb +24 -0
- data/lib/http_stub/extensions/core/hash/with_indifferent_and_insensitive_access.rb +18 -0
- data/lib/http_stub/extensions/rack/handler.rb +1 -1
- data/lib/http_stub/rake/task_generators.rb +1 -1
- data/lib/http_stub/server/application.rb +10 -11
- data/lib/http_stub/server/application_helpers.rb +3 -5
- data/lib/http_stub/server/daemon.rb +5 -6
- data/lib/http_stub/server/request.rb +3 -10
- data/lib/http_stub/server/request/headers.rb +25 -0
- data/lib/http_stub/server/request/parameters.rb +21 -0
- data/lib/http_stub/server/request/request.rb +21 -0
- data/lib/http_stub/server/scenario/links.rb +1 -1
- data/lib/http_stub/server/stub/controller.rb +1 -1
- data/lib/http_stub/server/stub/empty.rb +4 -0
- data/lib/http_stub/server/stub/match/hash_matcher.rb +19 -0
- data/lib/http_stub/server/stub/match/omitted_value_matcher.rb +1 -3
- data/lib/http_stub/server/stub/match/rule/body.rb +2 -4
- data/lib/http_stub/server/stub/match/rule/headers.rb +5 -11
- data/lib/http_stub/server/stub/match/rule/json_body.rb +3 -5
- data/lib/http_stub/server/stub/match/rule/method.rb +1 -1
- data/lib/http_stub/server/stub/match/rule/parameters.rb +4 -7
- data/lib/http_stub/server/stub/match/rule/simple_body.rb +3 -3
- data/lib/http_stub/server/stub/match/rule/truthy.rb +27 -0
- data/lib/http_stub/server/stub/match/rule/uri.rb +3 -3
- data/lib/http_stub/server/stub/match/string_value_matcher.rb +4 -13
- data/lib/http_stub/server/stub/response.rb +5 -2
- data/lib/http_stub/server/stub/response/attribute/body.rb +32 -0
- data/lib/http_stub/server/stub/response/attribute/headers.rb +26 -0
- data/lib/http_stub/server/stub/response/attribute/interpolator.rb +26 -0
- data/lib/http_stub/server/stub/response/attribute/interpolator/headers.rb +27 -0
- data/lib/http_stub/server/stub/response/attribute/interpolator/parameters.rb +27 -0
- data/lib/http_stub/server/stub/response/base.rb +5 -16
- data/lib/http_stub/server/stub/response/file.rb +6 -2
- data/lib/http_stub/server/stub/response/text.rb +12 -0
- data/lib/http_stub/server/stub/stub.rb +4 -0
- data/lib/http_stub/version.rb +1 -1
- data/spec/acceptance/request_reference_spec.rb +44 -0
- data/spec/lib/http_stub/configurer/dsl/request_attribute_referencer_spec.rb +37 -0
- data/spec/lib/http_stub/configurer/dsl/request_referencer_spec.rb +37 -0
- data/spec/lib/http_stub/configurer/dsl/scenario_activator_spec.rb +14 -14
- data/spec/lib/http_stub/configurer/dsl/server_spec.rb +2 -6
- data/spec/lib/http_stub/configurer/dsl/stub_builder_spec.rb +34 -8
- data/spec/lib/http_stub/{server/formatted_hash_spec.rb → extensions/core/hash/formatted_spec.rb} +4 -6
- data/spec/lib/http_stub/{hash_with_indifferent_and_insensitive_access_spec.rb → extensions/core/hash/indifferent_and_insensitive_access_spec.rb} +8 -4
- data/spec/lib/http_stub/extensions/core/hash/with_indifferent_and_insensitive_access_spec.rb +41 -0
- data/spec/lib/http_stub/extensions/core/hash_spec.rb +11 -129
- data/spec/lib/http_stub/server/application_spec.rb +1 -1
- data/spec/lib/http_stub/server/payload_file_consolidator_spec.rb +1 -1
- data/spec/lib/http_stub/server/request/headers_spec.rb +89 -0
- data/spec/lib/http_stub/server/request/parameters_spec.rb +60 -0
- data/spec/lib/http_stub/server/request/request_spec.rb +84 -0
- data/spec/lib/http_stub/server/request_spec.rb +9 -98
- data/spec/lib/http_stub/server/response_spec.rb +2 -6
- data/spec/lib/http_stub/server/scenario/controller_spec.rb +1 -1
- data/spec/lib/http_stub/server/scenario/parser_spec.rb +3 -1
- data/spec/lib/http_stub/server/stub/controller_spec.rb +11 -4
- data/spec/lib/http_stub/server/stub/empty_spec.rb +10 -0
- data/spec/lib/http_stub/server/stub/match/hash_matcher_spec.rb +122 -0
- data/spec/lib/http_stub/server/stub/match/rule/body_spec.rb +4 -4
- data/spec/lib/http_stub/server/stub/match/rule/headers_spec.rb +54 -45
- data/spec/lib/http_stub/server/stub/match/rule/json_body_spec.rb +5 -5
- data/spec/lib/http_stub/server/stub/match/rule/method_spec.rb +19 -17
- data/spec/lib/http_stub/server/stub/match/rule/parameters_spec.rb +26 -33
- data/spec/lib/http_stub/server/stub/match/rule/simple_body_spec.rb +16 -25
- data/spec/lib/http_stub/server/stub/match/{truthy_matcher_spec.rb → rule/truthy_spec.rb} +3 -5
- data/spec/lib/http_stub/server/stub/match/rule/uri_spec.rb +16 -25
- data/spec/lib/http_stub/server/stub/match/string_value_matcher_spec.rb +2 -12
- data/spec/lib/http_stub/server/stub/parser_spec.rb +5 -3
- data/spec/lib/http_stub/server/stub/response/attribute/body_spec.rb +98 -0
- data/spec/lib/http_stub/server/stub/response/attribute/headers_spec.rb +57 -0
- data/spec/lib/http_stub/server/stub/response/attribute/interpolator/headers_spec.rb +53 -0
- data/spec/lib/http_stub/server/stub/response/attribute/interpolator/parameters_spec.rb +53 -0
- data/spec/lib/http_stub/server/stub/response/attribute/interpolator_spec.rb +41 -0
- data/spec/lib/http_stub/server/stub/response/base_spec.rb +6 -53
- data/spec/lib/http_stub/server/stub/response/file_spec.rb +48 -7
- data/spec/lib/http_stub/server/stub/response/text_spec.rb +66 -1
- data/spec/lib/http_stub/server/stub/stub_spec.rb +25 -4
- data/spec/spec_helper.rb +1 -0
- data/spec/support/http_stub/server/request_fixture.rb +1 -1
- metadata +86 -26
- data/lib/http_stub/hash_with_indifferent_and_insensitive_access.rb +0 -18
- data/lib/http_stub/server/formatted_hash.rb +0 -18
- data/lib/http_stub/server/header_parser.rb +0 -17
- data/lib/http_stub/server/stub/match/hash_with_string_value_matchers.rb +0 -26
- data/lib/http_stub/server/stub/match/truthy_matcher.rb +0 -25
- data/spec/lib/http_stub/server/header_parser_spec.rb +0 -51
- 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
|
-
|
12
|
-
|
13
|
-
|
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 =
|
21
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
4
|
+
module Request
|
5
5
|
|
6
|
-
|
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
|
@@ -16,7 +16,7 @@ module HttpStub
|
|
16
16
|
|
17
17
|
def match(request, logger)
|
18
18
|
stub = @registry.match(request, logger)
|
19
|
-
stub ? stub.
|
19
|
+
stub ? stub.response_for(request) : HttpStub::Server::Response::NOT_FOUND
|
20
20
|
end
|
21
21
|
|
22
22
|
def clear(logger)
|
@@ -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
|
-
|
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
|
-
|
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::
|
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
|
-
|
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
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
@@ -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
|
-
|
11
|
+
super(parameters || {}, "=", "&")
|
11
12
|
end
|
12
13
|
|
13
14
|
def matches?(request, _logger)
|
14
|
-
|
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 =
|
10
|
+
@body = body
|
11
11
|
end
|
12
12
|
|
13
13
|
def matches?(request, _logger)
|
14
|
-
|
14
|
+
HttpStub::Server::Stub::Match::StringValueMatcher.match?(@body, request.body)
|
15
15
|
end
|
16
16
|
|
17
17
|
def to_s
|
18
|
-
@body
|
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 =
|
10
|
+
@uri = uri
|
11
11
|
end
|
12
12
|
|
13
13
|
def matches?(request, _logger)
|
14
|
-
|
14
|
+
HttpStub::Server::Stub::Match::StringValueMatcher.match?(@uri, request.uri)
|
15
15
|
end
|
16
16
|
|
17
17
|
def to_s
|
18
|
-
@uri
|
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
|
-
|
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
|
25
|
-
|
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)
|
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
|