| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
"""Core response handlers.""" |
| 14 |
|
| 15 |
from gabbi.handlers import base |
| 16 |
|
| 17 |
|
| 18 |
class StringResponseHandler(base.ResponseHandler): |
| 19 |
"""Test for matching strings in the the response body.""" |
| 20 |
|
| 21 |
test_key_suffix = 'strings' |
| 22 |
test_key_value = [] |
| 23 |
|
| 24 |
def action(self, test, expected, value=None): |
| 25 |
is_regex = self.is_regex(expected) |
| 26 |
expected = test.replace_template(expected, escape_regex=is_regex) |
| 27 |
|
| 28 |
if is_regex: |
| 29 |
# Trim off / |
| 30 |
expected = expected[1:-1] |
| 31 |
test.assertRegex( |
| 32 |
test.output, expected, |
| 33 |
'Expect response body %s to match /%s/' % |
| 34 |
(test.output, expected)) |
| 35 |
else: |
| 36 |
test.assert_in_or_print_output(expected, test.output) |
| 37 |
|
| 38 |
|
| 39 |
class ForbiddenHeadersResponseHandler(base.ResponseHandler): |
| 40 |
"""Test that listed headers are not in the response.""" |
| 41 |
|
| 42 |
test_key_suffix = 'forbidden_headers' |
| 43 |
test_key_value = [] |
| 44 |
|
| 45 |
def action(self, test, forbidden, value=None): |
| 46 |
# normalize forbidden header to lower case |
| 47 |
forbidden = test.replace_template(forbidden).lower() |
| 48 |
test.assertNotIn(forbidden, test.response, |
| 49 |
'Forbidden header %s found in response' % forbidden) |
| 50 |
|
| 51 |
|
| 52 |
class HeadersResponseHandler(base.ResponseHandler): |
| 53 |
"""Compare expected headers with actual headers. |
| 54 |
|
| 55 |
If a header value is wrapped in ``/`` it is treated as a raw |
| 56 |
regular expression. |
| 57 |
|
| 58 |
Headers values are always treated as strings. |
| 59 |
""" |
| 60 |
|
| 61 |
test_key_suffix = 'headers' |
| 62 |
test_key_value = {} |
| 63 |
|
| 64 |
def action(self, test, header, value=None): |
| 65 |
header = header.lower() # case-insensitive comparison |
| 66 |
response = test.response |
| 67 |
|
| 68 |
header_value = str(value) |
| 69 |
is_regex = self.is_regex(header_value) |
| 70 |
header_value = test.replace_template(header_value, |
| 71 |
escape_regex=is_regex) |
| 72 |
|
| 73 |
try: |
| 74 |
response_value = str(response[header]) |
| 75 |
except KeyError: |
| 76 |
raise AssertionError( |
| 77 |
"'%s' header not present in response: %s" % ( |
| 78 |
header, response.keys())) |
| 79 |
|
| 80 |
if is_regex: |
| 81 |
header_value = header_value[1:-1] |
| 82 |
test.assertRegex( |
| 83 |
response_value, header_value, |
| 84 |
'Expect header %s to match /%s/, got %s' % |
| 85 |
(header, header_value, response_value)) |
| 86 |
else: |
| 87 |
test.assertEqual(header_value, response_value, |
| 88 |
'Expect header %s with value %s, got %s' % |
| 89 |
(header, header_value, response[header])) |