Class: Dalziel::Matchers::RequestMatcher
- Inherits:
-
Object
- Object
- Dalziel::Matchers::RequestMatcher
- Defined in:
- lib/dalziel.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#json_expression ⇒ Object
readonly
Returns the value of attribute json_expression.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Instance Method Summary collapse
- #all_stubbed_requests ⇒ Object
- #does_not_match?(response) ⇒ Boolean
- #failure_message ⇒ Object
-
#initialize(json_expression) ⇒ RequestMatcher
constructor
A new instance of RequestMatcher.
- #matches?(request_pattern) ⇒ Boolean
- #payload_matcher ⇒ Object
- #show_request ⇒ Object
Constructor Details
#initialize(json_expression) ⇒ RequestMatcher
Returns a new instance of RequestMatcher.
122 123 124 |
# File 'lib/dalziel.rb', line 122 def initialize(json_expression) @json_expression = json_expression end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
120 121 122 |
# File 'lib/dalziel.rb', line 120 def body @body end |
#json_expression ⇒ Object (readonly)
Returns the value of attribute json_expression.
120 121 122 |
# File 'lib/dalziel.rb', line 120 def json_expression @json_expression end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
120 121 122 |
# File 'lib/dalziel.rb', line 120 def request @request end |
Instance Method Details
#all_stubbed_requests ⇒ Object
168 169 170 |
# File 'lib/dalziel.rb', line 168 def all_stubbed_requests WebMock::RequestRegistry.instance.requested_signatures end |
#does_not_match?(response) ⇒ Boolean
145 146 147 |
# File 'lib/dalziel.rb', line 145 def does_not_match?(response) fail "Inverted matching is not implemented with this matcher" end |
#failure_message ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/dalziel.rb', line 149 def if @request == [] "No request matched" elsif !@is_json "Accept header is not JSON.\n\n%s\n\nAccept is %s" % [ show_request, @accept.inspect ] else "Request body did not match.\n\n%s\n\n%s" % [ show_request, payload_matcher.last_error ] end end |
#matches?(request_pattern) ⇒ Boolean
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/dalziel.rb', line 126 def matches?(request_pattern) @request = nil all_stubbed_requests.each { |request_signature, _count| if request_pattern.matches?(request_signature) @request = request_signature break end } return false if @request.nil? @body = JSON.parse(@request.body) @accept = @request.headers["Accept"] @is_json = @accept =~ /\bjson$/ @json_match = payload_matcher =~ @body @is_json && @json_match end |
#payload_matcher ⇒ Object
164 165 166 |
# File 'lib/dalziel.rb', line 164 def payload_matcher @payload_matcher ||= JsonExpressions::Matcher.new(json_expression) end |
#show_request ⇒ Object
159 160 161 162 |
# File 'lib/dalziel.rb', line 159 def show_request headers = Dalziel.format_headers(request.headers) Dalziel.indent "%s %s\n\n%s\n\n%s" % [ request.method.to_s.upcase, request.uri.to_s, headers, JSON.pretty_generate(body) ] end |