Class: ApiSim::Matchers::RequestBodyMatcher

Inherits:
BaseMatcher
  • Object
show all
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

Instance Method Summary collapse

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.



9
10
11
12
13
14
15
16
17
18
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 9

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 = route
  @http_method = http_method
  @schema = schema
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def default
  @default
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def headers
  @headers
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def http_method
  @http_method
end

#matcherObject (readonly)

Returns the value of attribute matcher.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def matcher
  @matcher
end

#response_bodyObject (readonly)

Returns the value of attribute response_body.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def response_body
  @response_body
end

#response_codeObject (readonly)

Returns the value of attribute response_code.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def response_code
  @response_code
end

#routeObject (readonly)

Returns the value of attribute route.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def route
  @route
end

#schemaObject (readonly)

Returns the value of attribute schema.



7
8
9
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 7

def schema
  @schema
end

Instance Method Details

#match_on_body?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 27

def match_on_body?
  true
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 20

def matches?(request)
  request.body.rewind
  body = request.body.read
  request.body.rewind
  request.path == route && request.request_method == http_method && matcher.match(body)
end

#to_sObject



31
32
33
34
35
# File 'lib/api_sim/matchers/request_body_matcher.rb', line 31

def to_s
  <<-DOC.gsub(/^\s+/, '')
    #{http_method} #{route} /#{matcher.source}/ -> (#{response_code}) #{response_body[0..20]}...
  DOC
end