Esempio n. 1
0
    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
Esempio n. 2
0
    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)
Esempio n. 3
0
 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))
Esempio n. 4
0
File: case.py Progetto: cdent/gabbi
    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
Esempio n. 5
0
 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))
Esempio n. 6
0
    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)
Esempio n. 7
0
    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)
Esempio n. 8
0
    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)
Esempio n. 9
0
 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)
Esempio n. 10
0
 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)
Esempio n. 11
0
    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)
Esempio n. 12
0
    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)
Esempio n. 13
0
    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)
Esempio n. 14
0
File: case.py Progetto: cdent/gabbi
    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)
Esempio n. 15
0
    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)
Esempio n. 16
0
 def test_binary(self):
     for media_type in self.BINARY_TYPES:
         self.assertFalse(utils.not_binary(media_type),
                          '%s should be binary' % media_type)
Esempio n. 17
0
 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)