Example #1
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))
Example #2
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))
Example #3
0
    def _run_request(self, url, method, headers, body):
        """Run the http request and decode output.

        The call to make the request will catch a WSGIAppError from
        wsgi_intercept so that the real traceback from a catastrophic
        error in the intercepted app can be examined.
        """

        if 'user-agent' not in (key.lower() for key in headers):
            headers['user-agent'] = "gabbi/%s (Python httplib2)" % __version__

        try:
            response, content = self.http.request(url,
                                                  method=method,
                                                  headers=headers,
                                                  body=body)
        except wsgi_intercept.WSGIAppError as exc:
            # Extract and re-raise the wrapped exception.
            six.reraise(exc.exception_type, exc.exception_value, exc.traceback)

        # Set headers and location attributes for follow on requests
        self.response = response
        if 'location' in response:
            self.location = response['location']

        # Decode and store response
        decoded_output = utils.decode_response_content(response, content)
        self.content_type = response.get('content-type', '').lower()
        if (decoded_output and ('application/json' in self.content_type
                                or '+json' in self.content_type)):
            self.json_data = json.loads(decoded_output)
        else:
            self.json_data = None
        self.output = decoded_output
Example #4
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)
Example #5
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)
Example #6
0
File: case.py Project: cdent/gabbi
    def _run_request(self, url, method, headers, body, redirect=False):
        """Run the http request and decode output.

        The call to make the request will catch a WSGIAppError from
        wsgi_intercept so that the real traceback from a catastrophic
        error in the intercepted app can be examined.
        """

        if 'user-agent' not in (key.lower() for key in headers):
            headers['user-agent'] = "gabbi/%s (Python urllib3)" % __version__

        try:
            response, content = self.http.request(
                url,
                method=method,
                headers=headers,
                body=body,
                redirect=redirect
            )
        except wsgi_intercept.WSGIAppError as exc:
            # Extract and re-raise the wrapped exception.
            six.reraise(exc.exception_type, exc.exception_value,
                        exc.traceback)

        # Set headers and location attributes for follow on requests
        self.response = response
        if 'location' in response:
            self.location = response['location']

        # Decode and store response
        decoded_output = utils.decode_response_content(response, content)
        self.content_type = response.get('content-type', '').lower()
        loader_class = self.get_content_handler(self.content_type)
        if decoded_output and loader_class:
            # save structured response data
            self.response_data = loader_class.loads(decoded_output)
        else:
            self.response_data = None
        self.output = decoded_output