def _test_data_to_string(self, data, content_type): """Turn the request data into a string. If the data is not binary, replace template strings. If the result of the template handling is not a string, run the result through the dumper. """ dumper_class = self.get_content_handler(content_type) if not _is_complex_type(data): if isinstance(data, six.string_types) and data.startswith('<@'): info = self.load_data_file(data.replace('<@', '', 1)) if utils.not_binary(content_type): data = six.text_type(info, 'UTF-8') else: # Return early we are binary content return info else: # We have a complex data structure, try to dump it. if dumper_class: data = self.replace_template(data) data = dumper_class.dumps(data, test=self) else: raise ValueError('unable to process data to %s' % content_type) data = self.replace_template(data) # If the result after template handling is not a string, dump # it if there is a suitable dumper. if dumper_class and not isinstance(data, six.string_types): # If there are errors dumping we want them to raise to the # test harness. data = dumper_class.dumps(data, test=self) return data
def assert_in_or_print_output(self, expected, iterable): """Assert the iterable contains expected or print some output. If the output is long, it is limited by either GABBI_MAX_CHARS_OUTPUT in the environment or the MAX_CHARS_OUTPUT constant. """ if utils.not_binary(utils.parse_content_type(self.content_type)[0]): if expected in iterable: return if self.response_data: dumper_class = self.get_content_handler(self.content_type) if dumper_class: full_response = dumper_class.dumps(self.response_data, pretty=True) else: full_response = self.output else: full_response = self.output max_chars = os.getenv('GABBI_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT) response = full_response[0:max_chars] is_truncated = (len(response) != len(full_response)) if iterable == self.output: msg = "'%s' not found in %s%s" % (expected, response, '\n...truncated...' if is_truncated else '') else: msg = "'%s' not found in %s, %sresponse:\n%s" % ( expected, iterable, 'truncated ' if is_truncated else '', response) self.fail(msg) else: self.assertIn(expected, iterable)
def _print_body(self, headers, content): """Output body if not binary.""" if self._show_body and utils.not_binary( utils.extract_content_type(headers)[0]): self._verbose_output('') self._verbose_output( utils.decode_response_content(headers, content))
def _test_data_to_string(self, data, content_type): """Turn the request data into a string. If the data is not binary, replace template strings. If the result of the template handling is not a string, run the result through the dumper. """ dumper_class = self.get_content_handler(content_type) if not _is_complex_type(data): if isinstance(data, six.string_types) and data.startswith('<@'): info = self.load_data_file(data.replace('<@', '', 1)) if utils.not_binary(content_type): data = six.text_type(info, 'UTF-8') else: # Return early we are binary content return info else: # We have a complex data structure, try to dump it. if dumper_class: data = self.replace_template(data) data = dumper_class.dumps(data, test=self) else: raise ValueError( 'unable to process data to %s' % content_type) data = self.replace_template(data) # If the result after template handling is not a string, dump # it if there is a suitable dumper. if dumper_class and not isinstance(data, six.string_types): # If there are errors dumping we want them to raise to the # test harness. data = dumper_class.dumps(data, test=self) return data
def assert_in_or_print_output(self, expected, iterable): """Assert the iterable contains expected or print some output. If the output is long, it is limited by either GABBI_MAX_CHARS_OUTPUT in the environment or the MAX_CHARS_OUTPUT constant. """ if utils.not_binary(self.content_type): if expected in iterable: return if self.json_data: full_response = json.dumps(self.json_data, indent=2, separators=(',', ': ')) else: full_response = self.output max_chars = os.getenv('GABBI_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT) response = full_response[0:max_chars] is_truncated = (len(response) != len(full_response)) if iterable == self.output: msg = "'%s' not found in %s%s" % (expected, response, '\n...truncated...' if is_truncated else '') else: msg = "'%s' not found in %s, %sresponse:\n%s" % ( expected, iterable, 'truncated ' if is_truncated else '', response) self.fail(msg) else: self.assertIn(expected, iterable)
def assert_in_or_print_output(self, expected, iterable): if utils.not_binary(self.content_type): if expected in iterable: return if self.json_data: full_response = json.dumps(self.json_data, indent=2, separators=(',', ': ')) else: full_response = self.output max_chars = os.getenv('GABBIT_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT) response = full_response[0:max_chars] is_truncated = (len(response) != len(full_response)) if iterable == self.output: msg = "'%s' not found in %s%s" % ( expected, response, '\n...truncated...' if is_truncated else '' ) else: msg = "'%s' not found in %s, %sresponse:\n%s" % ( expected, iterable, 'truncated ' if is_truncated else '', response) self.fail(msg) else: self.assertIn(expected, iterable)
def assert_in_or_print_output(self, expected, iterable): """Assert the iterable contains expected or print some output. If the output is long, it is limited by either GABBI_MAX_CHARS_OUTPUT in the environment or the MAX_CHARS_OUTPUT constant. """ if utils.not_binary(self.content_type): if expected in iterable: return if self.json_data: full_response = json.dumps(self.json_data, indent=2, separators=(',', ': ')) else: full_response = self.output max_chars = os.getenv('GABBI_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT) response = full_response[0:max_chars] is_truncated = (len(response) != len(full_response)) if iterable == self.output: msg = "'%s' not found in %s%s" % ( expected, response, '\n...truncated...' if is_truncated else '' ) else: msg = "'%s' not found in %s, %sresponse:\n%s" % ( expected, iterable, 'truncated ' if is_truncated else '', response) self.fail(msg) else: self.assertIn(expected, iterable)
def _print_body(self, headers, content): """Output body if not binary.""" content_type = utils.extract_content_type(headers)[0] if self._show_body and utils.not_binary(content_type): content = utils.decode_response_content(headers, content) # TODO(cdent): Using the JSONHandler here instead of # just the json module to make it clear that eventually # we could pretty print any printable output by using a # handler's loads() and dumps(). Not doing that now # because it would be pointless (no other interesting # handlers) and this approach may be entirely wrong. if content and jsonhandler.JSONHandler.accepts(content_type): data = jsonhandler.JSONHandler.loads(content) content = jsonhandler.JSONHandler.dumps(data, pretty=True) self._verbose_output('') self._verbose_output(content)
def _print_body(self, headers, content): """Output body if not binary.""" content_type = utils.extract_content_type(headers)[0] if self._show_body and utils.not_binary(content_type): content = utils.decode_response_content(headers, content) # TODO(cdent): Using the JSONHandler here instead of # just the json module to make it clear that eventually # we could pretty print any printable output by using a # handler's loads() and dumps(). Not doing that now # because it would be pointless (no other interesting # handlers) and this approach may be entirely wrong. if jsonhandler.JSONHandler.accepts(content_type): data = jsonhandler.JSONHandler.loads(content) content = jsonhandler.JSONHandler.dumps(data, pretty=True) self._verbose_output('') self._verbose_output(content)
def _test_data_to_string(self, data, content_type): """Turn the request data into a string. If the data is not binary, replace template strings. """ if isinstance(data, str): if data.startswith('<@'): info = self._load_data_file(data.replace('<@', '', 1)) if utils.not_binary(content_type): try: info = str(info, 'UTF-8') except TypeError: info = info.encode('UTF-8') data = info else: return info else: data = json.dumps(data) return self.replace_template(data)
def _test_data_to_string(self, data, content_type): """Turn the request data into a string. If the data is not binary, replace template strings. """ if isinstance(data, str): if data.startswith('<@'): info = self.load_data_file(data.replace('<@', '', 1)) if utils.not_binary(content_type): data = six.text_type(info, 'UTF-8') else: return info else: dumper_class = self.get_content_handler(content_type) if dumper_class: data = self.replace_template(data) data = dumper_class.dumps(data, test=self) else: raise ValueError('unable to process data to %s' % content_type) return self.replace_template(data)
def assert_in_or_print_output(self, expected, iterable): """Assert the iterable contains expected or print some output. If the output is long, it is limited by either GABBI_MAX_CHARS_OUTPUT in the environment or the MAX_CHARS_OUTPUT constant. """ if utils.not_binary(utils.parse_content_type(self.content_type)[0]): if expected in iterable: return if self.response_data: dumper_class = self.get_content_handler(self.content_type) if dumper_class: full_response = dumper_class.dumps(self.response_data, pretty=True, test=self) else: full_response = self.output else: full_response = self.output max_chars = os.getenv('GABBI_MAX_CHARS_OUTPUT', MAX_CHARS_OUTPUT) response = full_response[0:max_chars] is_truncated = (len(response) != len(full_response)) if iterable == self.output: msg = "'%s' not found in %s%s" % ( expected, response, '\n...truncated...' if is_truncated else '' ) else: msg = "'%s' not found in %s, %sresponse:\n%s" % ( expected, iterable, 'truncated ' if is_truncated else '', response) self.fail(msg) else: self.assertIn(expected, iterable)
def _test_data_to_string(self, data, content_type): """Turn the request data into a string. If the data is not binary, replace template strings. """ if isinstance(data, str): if data.startswith('<@'): info = self._load_data_file(data.replace('<@', '', 1)) if utils.not_binary(content_type): try: info = str(info, 'UTF-8') except TypeError: info = info.encode('UTF-8') data = info else: return info else: dumper_class = self.get_content_handler(content_type) if dumper_class: data = dumper_class.dumps(data) else: raise ValueError( 'unable to process data to %s' % content_type) return self.replace_template(data)
def test_binary(self): for media_type in self.BINARY_TYPES: self.assertFalse(utils.not_binary(media_type), '%s should be binary' % media_type)
def test_not_binary(self): for media_type in self.NON_BINARY_TYPES: self.assertTrue(utils.not_binary(media_type), '%s should not be binary' % media_type)