def make_http_call(self, api_call: ApiCall): exchange_request = ExchangeRequest.from_api_call(api_call, hide_secrets=False) exchange_request.headers = combine_request_headers( app_settings, exchange_request) exchange = HttpExchange(api_call_id=api_call.id, request=exchange_request) if api_call.mocked_response.is_enabled: exchange.response = ExchangeResponse.from_mocked_response( api_call.mocked_response) api_worker_data = ApiWorkerData(exchange=exchange, on_success=self.__on_success, on_failure=self.__on_failure) self.queue_worker_task(api_worker_data)
def __prepare_fuzzed_exchange_from_api_call(self, api_call: ApiCall): exchange_request: ExchangeRequest = self.__get_prepared_request(api_call) exchange_request.request_type = ExchangeRequestType.FUZZED exchange_request.request_body = self.__generate_fuzzed_payload( exchange_request.request_body ) return HttpExchange(api_call_id=api_call.id, request=exchange_request)
def __get_last_exchange(self, api_call_id): api_call_exchanges = app_settings.app_data_cache.get_api_call_exchanges( api_call_id) if api_call_exchanges: return api_call_exchanges[-1] else: return HttpExchange(api_call_id)
def mock_exchange(): return HttpExchange( api_call_id="2", id="5", request=ExchangeRequest( request_time="Thu Jun 20 09:04:55 2019", http_method="GET", http_url="http://127.0.0.1:8000/get", headers={"Content-Type": ContentType.JSON.value}, query_params=mock_flat_dict(), form_params=mock_flat_dict(), request_body=mock_raw_json(), ), type="http_exchange", response=ExchangeResponse( http_status_code=200, response_headers={ "server": "gunicorn/19.9.0", "date": "Thu, 20 Jun 2019 08:04:55 GMT", "connection": "close", "content-type": "application/json", "content-length": "273", "access-control-allow-origin": "*", "access-control-allow-credentials": "true", }, response_body='{\n "args": {}, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Connection": "keep-alive", \n "Host": "127.0.0.1:8000", \n "User-Agent": "python-requests/2.21.0"\n }, \n "origin": "127.0.0.1", \n "url": "http://127.0.0.1:8000/get"\n}\n', response_time=236.530_999_999_999_98, ), assertions=[], )
def display_last_exchange(self, api_call: ApiCall): api_call_exchanges = app_settings.app_data_cache.get_api_call_exchanges( api_call.id) if api_call_exchanges: last_exchange = api_call_exchanges[-1] self.refresh(last_exchange) else: self.refresh(HttpExchange(api_call.id))
def evaluate(self, api_test_case: ApiTestCase, exchange: HttpExchange): logging.info( f"Running {len(api_test_case.assertions)} assertions against exchange {exchange}" ) assertions_with_output = [ self.__evaluate_assertion(assertion, exchange) for assertion in api_test_case.comparable_assertions() ] exchange.assertions = assertions_with_output app_settings.app_data_writer.update_http_exchange(exchange) # Update API Call assertions status api_call = app_settings.app_data_cache.get_api_call( api_test_case.api_call_id) if assertions_with_output: api_call.last_assertion_result = all( [a.result for a in assertions_with_output]) else: api_call.last_assertion_result = None api_call_interactor.update_api_call(api_call.id, api_call)