def test_log_response(mocked_logger): response = mock.MagicMock() response.url = 'http://mock.rulezz' response.request.method = 'GET' response.status_code = 200 response.reason = 'OK' response.text = "<html>body</html>" response.headers = { 'Date': 'Sun, 10 May 2020 22:31:21 GMT', 'Expires': '-1', 'Cache-Control': 'private, max-age=0', 'Content-Type': 'text/html; charset=ISO-8859-1', 'P3P': 'CP="This is not a P3P policy! See g.co/p3phelp for more info."', 'Content-Encoding': 'gzip', 'Server': 'gws', 'X-XSS-Protection': '0', 'X-Frame-Options': 'SAMEORIGIN', 'Set-Cookie': '1P_JAR=2020-05-10-22; expires=Tue, 09-Jun-2020 22:31:21 GMT; path=/; domain=.google.it; Secure, NID=204=1JvfFtAYLcnQfWYzh5h0K-PttJP8IvuJdcaej_-utCvMHqavyFkwmthddhQZ-sQ6nZkNCWybVUuzTtaNoEK4TD1FG-TA7QIUR2P6-kj-vN0zkjiS4VdfQbxFnfwtIgkBxFDuoAZsoa_oYm0ODjJ4JAZfdXueqrZJ38tDtIOXpVU; expires=Mon, 09-Nov-2020 22:31:21 GMT; path=/; domain=.google.it; HttpOnly', 'Alt-Svc': 'h3-27=":443"; ma=2592000,h3-25=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"', 'Transfer-Encoding': 'chunked' } # noqa log_response(response) assert mocked_logger.info.call_args[0][0] == ( "%s Response : url=%s \n " % (response.request.method.upper(), response.url) + "status=%s, reason=%s \n " % (response.status_code, response.reason) + "headers=%s \n " % response.headers + "body=%s \n " % response.text)
def _common_request( self, method, session, uri, **kwargs): method_function = getattr(session, method) self._capture_output() log_options = kwargs.pop('log_options', None) resp = method_function( self._get_url(session, uri), params=kwargs.pop('params', None), timeout=self._get_timeout(kwargs.pop('timeout', None)), cookies=self.cookies, verify=self.verify, **kwargs) log.log_request(resp, log_options=log_options) self._print_debug() session.last_resp = resp log.log_response(resp, log_options=log_options) data = kwargs.get('data', None) if is_file_descriptor(data): data.close() return resp
def _common_request( self, method, session, uri, **kwargs): if session: method_function = getattr(session, method) else: method_function = getattr(requests, method) self._capture_output() resp = method_function( self._get_url(session, uri), timeout=self._get_timeout(kwargs.pop('timeout', None)), cookies=kwargs.pop('cookies', self.cookies), **kwargs) log.log_request(resp) self._print_debug() log.log_response(resp) self.last_response = resp data = kwargs.get('data', None) if is_file_descriptor(data): data.close() return resp
def _common_request( self, method, session, uri, **kwargs): # TODO this won't log the real headers that have been added by requests itself # it should be moved after the real request to log those. # But at the same time in case of failure debug information are not logged. # (maybe it should be analysed) log.log_request(method, session, uri, **kwargs) method_function = getattr(session, method) self._capture_output() resp = method_function( self._get_url(session, uri), params=utils.utf8_urlencode(kwargs.pop('params', None)), timeout=self._get_timeout(kwargs.pop('timeout', None)), cookies=self.cookies, verify=self.verify, **kwargs) self._print_debug() session.last_resp = resp log.log_response(method, resp) return resp
def test_log_response_base64_body(mocked_logger): response = mock.MagicMock() response.url = 'http://mock.rulezz' response.request.method = 'GET' response.status_code = 200 response.reason = 'OK' response.content = b"<html>body</html>" log_response(response, log_options={'response_body_binary': True}) assert mocked_logger.info.call_args[0][0] == ( "%s Response : url=%s \n " % (response.request.method.upper(), response.url) + "status=%s, reason=%s \n " % (response.status_code, response.reason) + "body(base64=True)=PGh0bWw+Ym9keTwvaHRtbD4= \n ")
def test_log_response(mocked_logger): response = mock.MagicMock() response.url = 'http://mock.rulezz' response.request.method = 'GET' response.status_code = 200 response.reason = 'OK' response.text = "<html>body</html>" log_response(response) assert mocked_logger.info.call_args[0][0] == ( "%s Response : url=%s \n " % (response.request.method.upper(), response.url) + "status=%s, reason=%s \n " % (response.status_code, response.reason) + "body(base64=False)=%s \n " % response.text)
def _common_request(self, method, session, uri, **kwargs): log.log_request(method, session, uri, **kwargs) method_function = getattr(session, method) self._capture_output() resp = method_function( self._get_url(session, uri), params=utils.utf8_urlencode(kwargs.pop('params', None)), timeout=self._get_timeout(kwargs.pop('timeout', None)), cookies=self.cookies, verify=self.verify, **kwargs) self._print_debug() session.last_resp = resp log.log_response(method, resp) return resp