| 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 |
"""Base classes for response and content handlers.""" |
| 14 |
|
| 15 |
from gabbi.exception import GabbiFormatError |
| 16 |
|
| 17 |
|
| 18 |
class ResponseHandler: |
| 19 |
"""Add functionality for making assertions about an HTTP response. |
| 20 |
|
| 21 |
A subclass may implement two methods: ``action`` and ``preprocess``. |
| 22 |
|
| 23 |
``preprocess`` takes one argument, the ``TestCase``. It is called exactly |
| 24 |
once for each test before looping across the assertions. It is used, |
| 25 |
rarely, to copy the ``test.output`` into a useful form (such as a parsed |
| 26 |
DOM). |
| 27 |
|
| 28 |
``action`` takes two or three arguments. If ``test_key_value`` is a list |
| 29 |
``action`` is called with the test case and a single list item. If |
| 30 |
``test_key_value`` is a dict then ``action`` is called with the test case |
| 31 |
and a key and value pair. |
| 32 |
""" |
| 33 |
|
| 34 |
test_key_suffix = '' |
| 35 |
test_key_value = [] |
| 36 |
|
| 37 |
def __init__(self): |
| 38 |
self._register() |
| 39 |
|
| 40 |
def __call__(self, test): |
| 41 |
if test.test_data[self._key]: |
| 42 |
self.preprocess(test) |
| 43 |
if not isinstance( |
| 44 |
test.test_data[self._key], type(self.test_key_value)): |
| 45 |
raise GabbiFormatError( |
| 46 |
"%s in '%s' has incorrect type, must be %s" |
| 47 |
% (self._key, test.test_data['name'], |
| 48 |
type(self.test_key_value))) |
| 49 |
for item in test.test_data[self._key]: |
| 50 |
try: |
| 51 |
value = test.test_data[self._key][item] |
| 52 |
except (TypeError, KeyError): |
| 53 |
value = None |
| 54 |
self.action(test, item, value=value) |
| 55 |
|
| 56 |
def preprocess(self, test): |
| 57 |
"""Do any pre-single-test preprocessing.""" |
| 58 |
pass |
| 59 |
|
| 60 |
def action(self, test, item, value=None): |
| 61 |
"""Test an individual entry for this response handler. |
| 62 |
|
| 63 |
If the entry is a key value pair the key is in item and the |
| 64 |
value in value. Otherwise the entry is considered a single item |
| 65 |
from a list. |
| 66 |
""" |
| 67 |
pass |
| 68 |
|
| 69 |
def is_regex(self, value): |
| 70 |
"""Check if the value is formatted to looks like a regular expression. |
| 71 |
|
| 72 |
Meaning it starts and ends with "/". |
| 73 |
""" |
| 74 |
return ( |
| 75 |
value.startswith('/') and value.endswith('/') and len(value) > 1 |
| 76 |
) |
| 77 |
|
| 78 |
def _register(self): |
| 79 |
"""Register this handler on the provided test class.""" |
| 80 |
self.response_handler = None |
| 81 |
self.content_handler = None |
| 82 |
if self.test_key_suffix: |
| 83 |
self._key = 'response_%s' % self.test_key_suffix |
| 84 |
self.test_base = {self._key: self.test_key_value} |
| 85 |
self.response_handler = self |
| 86 |
if hasattr(self, 'accepts'): |
| 87 |
self.content_handler = self |
| 88 |
|
| 89 |
def __eq__(self, other): |
| 90 |
if isinstance(other, ResponseHandler): |
| 91 |
return self.__class__ == other.__class__ |
| 92 |
return False |
| 93 |
|
| 94 |
def __ne__(self, other): |
| 95 |
return not self.__eq__(other) |
| 96 |
|
| 97 |
|
| 98 |
class ContentHandler(ResponseHandler): |
| 99 |
"""A subclass of ResponseHandlers that adds content handling.""" |
| 100 |
|
| 101 |
@staticmethod |
| 102 |
def accepts(content_type): |
| 103 |
"""Return True if this handler can handler this type.""" |
| 104 |
return False |
| 105 |
|
| 106 |
@classmethod |
| 107 |
def replacer(cls, response_data, path): |
| 108 |
"""Return the string that is replacing RESPONSE.""" |
| 109 |
return path |
| 110 |
|
| 111 |
@staticmethod |
| 112 |
def dumps(data, pretty=False, test=None): |
| 113 |
"""Return structured data as a string. |
| 114 |
|
| 115 |
If pretty is true, prettify. |
| 116 |
""" |
| 117 |
return data |
| 118 |
|
| 119 |
@staticmethod |
| 120 |
def loads(data): |
| 121 |
"""Create structured (Python) data from a stream. |
| 122 |
|
| 123 |
If there is a failure decoding then the handler should |
| 124 |
repackage the error as a gabbi.exception.GabbiDataLoadError. |
| 125 |
""" |
| 126 |
return data |
| 127 |
|
| 128 |
@staticmethod |
| 129 |
def load_data_file(test, file_path): |
| 130 |
"""Return the string content of the file specified by the file_path.""" |
| 131 |
return test.load_data_file(file_path) |