Example #1
0
 def test_invalid_schema(self):
     signal = http_checks.check_fail(rex.InvalidSchema())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_INVALID_SCHEMA", signal)
Example #2
0
 def test_url_required(self):
     signal = http_checks.check_fail(rex.URLRequired())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_URL_REQUIRED", signal)
Example #3
0
 def test_invalid_url(self):
     signal = http_checks.check_fail(rex.InvalidURL())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_INVALID_URL", signal)
Example #4
0
 def test_missing_schema(self):
     signal = http_checks.check_fail(rex.MissingSchema())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_MISSING_SCHEMA", signal)
Example #5
0
 def test_read_timeout(self):
     signal = http_checks.check_fail(rex.ReadTimeout())
     self._assert_has_tags(self.timeout_tags, signal)
     self._assert_has_slug("HTTP_FAIL_READ_TIMEOUT", signal)
Example #6
0
 def test_connect_timeout(self):
     signal = http_checks.check_fail(rex.ConnectTimeout())
     self._assert_has_tags(self.timeout_tags, signal)
     self._assert_has_slug("HTTP_FAIL_CONNECT_TIMEOUT", signal)
 def test_SSL_error(self):
     signal = http_checks.check_fail(rex.SSLError())
     self._assert_has_tags(self.conn_fail_tags, signal)
     self._assert_has_slug("HTTP_FAIL_SSL_ERROR", signal)
 def test_connect_timeout(self):
     signal = http_checks.check_fail(rex.ConnectTimeout())
     self._assert_has_tags(self.timeout_tags, signal)
     self._assert_has_slug("HTTP_FAIL_CONNECT_TIMEOUT", signal)
 def test_url_required(self):
     signal = http_checks.check_fail(rex.URLRequired())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_URL_REQUIRED", signal)
 def test_proxy_error(self):
     signal = http_checks.check_fail(rex.ProxyError())
     self._assert_has_tags(self.conn_fail_tags, signal)
     self._assert_has_slug("HTTP_FAIL_PROXY_ERROR", signal)
 def test_invalid_schema(self):
     signal = http_checks.check_fail(rex.InvalidSchema())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_INVALID_SCHEMA", signal)
 def test_missing_schema(self):
     signal = http_checks.check_fail(rex.MissingSchema())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_MISSING_SCHEMA", signal)
 def test_invalid_url(self):
     signal = http_checks.check_fail(rex.InvalidURL())
     self._assert_has_tags(self.bad_request_tags, signal)
     self._assert_has_slug("HTTP_FAIL_INVALID_URL", signal)
Example #14
0
 def test_proxy_error(self):
     signal = http_checks.check_fail(rex.ProxyError())
     self._assert_has_tags(self.conn_fail_tags, signal)
     self._assert_has_slug("HTTP_FAIL_PROXY_ERROR", signal)
        def _wrapper(*args, **kwargs):
            """Logging wrapper for any method that returns a requests response.

            Logs requestslib response objects, and the args and kwargs
            sent to the request() method, to the provided log at the provided
            log level.
            """
            kwargs_copy = deepcopy(kwargs)
            if kwargs_copy.get("sanitize"):
                kwargs_copy = string_utils.sanitize_secrets(kwargs_copy)
            logline = '{0} {1}'.format(args, string_utils.compress(
                kwargs_copy))

            try:
                log.debug(_safe_decode(logline))
            except Exception as exception:
                # Ignore all exceptions that happen in logging, then log them
                log.info(
                    _LI(
                        'Exception occurred while logging signature of calling'
                        'method in http client'))
                log.exception(exception)

            # Make the request and time its execution
            response = None
            no_resp_time = None
            signals = syntribos.signal.SignalHolder()
            try:
                start = time()
                response = func(*args, **kwargs)
            except requests.exceptions.RequestException as exc:
                signals.register(http_checks.check_fail(exc))
                log.log(level, _("A call to request() failed."))
                log.exception(exc)
                log.log(level, "=" * 80)
            except Exception as exc:
                log.critical(_LC(
                    'Call to Requests failed due to exception'))
                log.exception(exc)
                signals.register(syntribos.signal.from_generic_exception(exc))
                raise exc

            if len(signals) > 0 and response is None:
                no_resp_time = time() - start
                log.log(level,
                        _(
                            'Request failed, elapsed time....: %.6f sec.\n'
                        ), no_resp_time)
                return (response, signals)

            # requests lib 1.0.0 renamed body to data in the request object
            request_body = ''
            if 'body' in dir(response.request):
                request_body = response.request.body
            elif 'data' in dir(response.request):
                request_body = response.request.data
            else:
                log.info(
                    _LI(
                        "Unable to log request body, neither a 'data' nor a "
                        "'body' object could be found"))

            # requests lib 1.0.4 removed params from response.request
            request_params = ''
            request_url = response.request.url
            if 'params' in dir(response.request):
                request_params = response.request.params
            elif '?' in request_url:
                request_url, request_params = request_url.split('?')

            req_body_len = 0
            req_header_len = 0
            if response.request.headers:
                req_header_len = len(response.request.headers)
                request_headers = response.request.headers
            if response.request.body:
                req_body_len = len(response.request.body)
            response_content = response.content
            if kwargs_copy.get("sanitize"):
                response_content = string_utils.sanitize_secrets(
                    response_content)
                request_params = string_utils.sanitize_secrets(request_params)
                request_headers = string_utils.sanitize_secrets(
                    request_headers)
                request_body = string_utils.sanitize_secrets(request_body)
            logline = ''.join([
                '\n{0}\nREQUEST SENT\n{0}\n'.format('-' * 12),
                'request method.......: {0}\n'.format(response.request.method),
                'request url..........: {0}\n'.format(string_utils.compress(
                    request_url)),
                'request params.......: {0}\n'.format(string_utils.compress
                                                      (request_params)),
                'request headers size.: {0}\n'.format(req_header_len),
                'request headers......: {0}\n'.format(string_utils.compress(
                    request_headers)),
                'request body size....: {0}\n'.format(req_body_len),
                'request body.........: {0}\n'.format(string_utils.compress
                                                      (request_body))])

            try:
                log.log(level, _safe_decode(logline))
            except Exception as exception:
                # Ignore all exceptions that happen in logging, then log them
                log.log(level, '\n{0}\nREQUEST INFO\n{0}\n'.format('-' * 12))
                log.exception(exception)

            logline = ''.join([
                '\n{0}\nRESPONSE RECEIVED\n{0}\n'.format('-' * 17),
                'response status..: {0}\n'.format(response),
                'response headers.: {0}\n'.format(response.headers),
                'response time....: {0}\n'.format
                (response.elapsed.total_seconds()),
                'response size....: {0}\n'.format(len(response.content)),
                'response body....: {0}\n'.format(response_content),
                '-' * 79])
            try:
                log.log(level, _safe_decode(logline))
            except Exception as exception:
                # Ignore all exceptions that happen in logging, then log them
                log.log(level, '\n{0}\nRESPONSE INFO\n{0}\n'.format('-' * 13))
                log.exception(exception)
            return (response, signals)
Example #16
0
 def test_SSL_error(self):
     signal = http_checks.check_fail(rex.SSLError())
     self._assert_has_tags(self.conn_fail_tags, signal)
     self._assert_has_slug("HTTP_FAIL_SSL_ERROR", signal)
 def test_read_timeout(self):
     signal = http_checks.check_fail(rex.ReadTimeout())
     self._assert_has_tags(self.timeout_tags, signal)
     self._assert_has_slug("HTTP_FAIL_READ_TIMEOUT", signal)