Class: ApiSim::Matchers::RequestBodyMatcher
- Inherits:
-
BaseMatcher
- Object
- BaseMatcher
- ApiSim::Matchers::RequestBodyMatcher
- Defined in:
- lib/api_sim/matchers/request_body_matcher.rb
Constant Summary
Constants inherited from BaseMatcher
BaseMatcher::ALWAYS_TRUE_MATCHER, BaseMatcher::DEFAULT_RACK_RESPONSE
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#http_method ⇒ Object
readonly
Returns the value of attribute http_method.
-
#matcher ⇒ Object
readonly
Returns the value of attribute matcher.
-
#response_body ⇒ Object
readonly
Returns the value of attribute response_body.
-
#response_code ⇒ Object
readonly
Returns the value of attribute response_code.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Attributes inherited from BaseMatcher
Instance Method Summary collapse
-
#initialize(http_method:, route:, response_code: 200, response_body: '', headers: {}, default: false, body_matches:, schema: nil) ⇒ RequestBodyMatcher
constructor
A new instance of RequestBodyMatcher.
- #match_on_body? ⇒ Boolean
- #matches?(request) ⇒ Boolean
- #to_s ⇒ Object
Methods inherited from BaseMatcher
#custom_matcher?, #overridden!, #overridden?, #readonly?, #record_request, #requests, #reset!, #response
Constructor Details
#initialize(http_method:, route:, response_code: 200, response_body: '', headers: {}, default: false, body_matches:, schema: nil) ⇒ RequestBodyMatcher
Returns a new instance of RequestBodyMatcher.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 10 def initialize(http_method:, route:, response_code: 200, response_body: '', headers: {}, default: false, body_matches:, schema: nil) @default = default @matcher = Regexp.compile(body_matches) @headers = headers @response_body = response_body @response_code = response_code @route = Mustermann.new(route) @http_method = http_method @schema = schema end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def default @default end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def headers @headers end |
#http_method ⇒ Object (readonly)
Returns the value of attribute http_method.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def http_method @http_method end |
#matcher ⇒ Object (readonly)
Returns the value of attribute matcher.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def matcher @matcher end |
#response_body ⇒ Object (readonly)
Returns the value of attribute response_body.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def response_body @response_body end |
#response_code ⇒ Object (readonly)
Returns the value of attribute response_code.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def response_code @response_code end |
#route ⇒ Object (readonly)
Returns the value of attribute route.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def route @route end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
8 9 10 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 8 def schema @schema end |
Instance Method Details
#match_on_body? ⇒ Boolean
28 29 30 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 28 def match_on_body? true end |
#matches?(request) ⇒ Boolean
21 22 23 24 25 26 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 21 def matches?(request) request.body.rewind body = request.body.read request.body.rewind route.match(request.path) && request.request_method == http_method && matcher.match(body) end |
#to_s ⇒ Object
32 33 34 35 36 |
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 32 def to_s <<-DOC.gsub(/^\s+/, '') #{http_method} #{route} /#{matcher.source}/ -> (#{response_code}) #{response_body[0..20]}... DOC end |