def __register_network_info(self, request, response): from arjuna import Arjuna from arjuna.tpi.helper.arjtype import NetworkPacketInfo # Should be configurable sub_network_packets = [] for redir_resp in response.redir_history: redir_req = redir_resp.request sub_network_packets.append( NetworkPacketInfo(label="Sub-Request: {} {}".format( redir_req.method, redir_req.url), request=str(redir_req), response=str(redir_resp), sub_network_packets=tuple())) # The request for last response object was the last request and hence the last redirection. if response.redir_history: # last_req = response.last_request # if not last_req: last_req = response.request sub_network_packets.append( NetworkPacketInfo(label="Sub-Request: {} {}".format( last_req.method, last_req.url), request=str(last_req), response=str(response), sub_network_packets=tuple())) Arjuna.get_report_metadata().add_network_packet_info( NetworkPacketInfo(label=request.label, request=str(request), response=str(response), sub_network_packets=tuple(sub_network_packets)))
def _send(self, request) -> HttpResponse: ''' Send the provided HttpRequest to server. In case of ConnectionError, retries the connection 5 times at a gap of 1 second. Currently, not configurable. Returns `HttpResponse` object. In case of redirections, this is the last HttpResponse object, which encapsulates all redirections which can be retrieved from it. ''' from arjuna import Arjuna, log_info from arjuna.tpi.helper.arjtype import NetworkPacketInfo log_info(request.label) max_connection_retries = 5 try: counter = 0 exc_flag = False exc_desc = None while counter < max_connection_retries: counter += 1 try: if self._session.proxies: response = HttpResponse(self, self._session.send(request._request, allow_redirects=request.allow_redirects, timeout=request.timeout, proxies=self._session.proxies, verify=False)) else: response = HttpResponse(self, self._session.send(request._request, allow_redirects=request.allow_redirects, timeout=request.timeout)) except (ProxyError, InvalidProxyURL) as e: raise HttpConnectError(request, "There is an error in connecting to the configured proxy. Proxy settings: {}. Error: {}".format(self.__session.proxies, str(e))) except ConnectionError as f: exc_flag = True exc_desc = str(f) time.sleep(1) continue else: break if exc_flag: raise HttpConnectError(request, "Connection error despite trying 5 times. Error: {}".format(exc_desc)) except TooManyRedirects as e: response = HttpResponse(self._session, e.response) self.__register_network_info(request, response) raise HttpSendError(self, response, str(e) + ". Error redir URL: " + e.response.url) except Exception as e: import traceback response = "Error in sending the request\n" response += e.__class__.__name__ + ":" + str(e) + "\n" response += traceback.format_exc() Arjuna.get_report_metadata().add_network_packet_info( NetworkPacketInfo(label=request.label, request=str(request), response=str(response), sub_network_packets=tuple()) ) raise e else: self.__register_network_info(request, response) if request.xcodes is not None: self._validate_status_code(response, request.xcodes) return response
def get_pagewise_network_packet_info(self, filter_resources=True): page_network_info = [] from arjuna import Arjuna, ArjunaOption for har_page in self.__har_parser.pages: sub_network_packets = [] if filter_resources: entries = har_page.filter_entries( content_type= ".*(text/plain|application/xhtml+xml|text/html|json|application/xml|text/xml)" ) else: entries = har_page.entries for entry in entries: req_method = entry['request']['method'] req_url = entry['request']['url'] req_headers = { header['name']: header['value'] for header in entry['request']['headers'] } if 'content' in entry['request']: req_content = entry['request']['content']['text'] else: req_content = None req_repr = HttpRequest.repr_as_str(method=req_method, url=req_url, headers=req_headers, content=req_content) resp_status = entry['response']['status'] resp_status_text = entry['response']['statusText'] resp_headers = { header['name']: header['value'] for header in entry['response']['headers'] } if 'content' in entry['response']: if 'text' in entry['response']['content']: resp_content = entry['response']['content']['text'] else: resp_content = None else: resp_content = None resp_repr = HttpResponse.repr_as_str( status_code=resp_status, status_msg=resp_status_text, headers=resp_headers, content=resp_content) sub_network_packets.append( NetworkPacketInfo(label=f"{req_method} {req_url}", request=req_repr, response=resp_repr, sub_network_packets=[])) if sub_network_packets: page_network_info.append( NetworkPacketInfo(label=har_page.title, request="", response="", sub_network_packets=sub_network_packets)) return page_network_info
def send(self): from arjuna import Arjuna, log_info from arjuna.tpi.helper.arjtype import NetworkPacketInfo log_info(self.__printable_label) max_connection_retries = 5 try: counter = 0 exc_flag = False while counter < max_connection_retries: counter += 1 try: response = HttpResponse(self.__session, self.__session.send(self.__req)) except ConnectionError: exc_flag = True time.sleep(1) continue else: break if exc_flag: raise Exception("Connection error despite trying 5 times.") except Exception as e: import traceback response = "Error in sending the request\n" response += e.__class__.__name__ + ":" + str(e) + "\n" response += traceback.format_exc() Arjuna.get_report_metadata().add_network_packet_info( NetworkPacketInfo(label=self.label, request=str(self), response=str(response))) raise e else: # Should be configurable redir_network_packets = [] for redir_resp in response.redir_history: redir_req = redir_resp.request redir_network_packets.append( NetworkPacketInfo(label="Sub-Request: {} {}".format( redir_req.method, redir_req.url), request=str(redir_req), response=str(redir_resp), redir_network_packets=tuple())) # The request for last response object was the last request and hence the last redirection. if response.redir_history: last_req = response.last_request if not last_req: last_req = response.request redir_network_packets.append( NetworkPacketInfo(label="Sub-Request: {} {}".format( last_req.method, last_req.url), request=str(last_req), response=str(response), redir_network_packets=tuple())) Arjuna.get_report_metadata().add_network_packet_info( NetworkPacketInfo( label=self.label, request=str(self), response=str(response), redir_network_packets=tuple(redir_network_packets))) if self.__xcodes is not None and response.status_code not in self.__xcodes: raise HttpUnexpectedStatusCode(self.__req, response) return response