예제 #1
0
    def convert_response(self, raw_response):
        res = ExchangeResponse(http_status_code=raw_response.status_code,
                               response_body=raw_response.text)

        if res.response_body:
            res.response_body_type = guess_content_type(res.response_body)

        res.elapsed_time = raw_response.elapsed
        res.headers = raw_response.headers
        return res
예제 #2
0
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=[],
    )
예제 #3
0
    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)
예제 #4
0
    def make_http_call(self):
        # preparing request with variable substitutions
        var_tokens = get_variable_tokens(self.app_config)
        self.exchange.request = replace_variables(var_tokens, self.exchange.request)

        # deriving request content type
        req: ExchangeRequest = self.exchange.request
        logging.info(
            f"==>[{self.tname}] make_http_call({self.exchange.api_call_id}): Http {req.http_method} to {req.http_url}"
        )

        # converting request to k/v structure
        kwargs = dict(headers=req.headers, params=req.query_params)

        content_type = req.headers.get("%s" % CONTENT_TYPE_HEADER_IN_EXCHANGE, ContentType.NONE.value)

        if req.request_body:
            req.request_body_type = guess_content_type(req.request_body)
            content_type = req.headers.get(CONTENT_TYPE_HEADER_IN_EXCHANGE, req.request_body_type.value)

        if req.form_params and "application/x-www-form-urlencoded" in content_type:
            req.request_body_type = ContentType.FORM
            kwargs["data"] = req.form_params
        elif req.form_params:
            if content_type:
                del kwargs["headers"][CONTENT_TYPE_HEADER_IN_EXCHANGE]

            kwargs["files"] = {
                k: open_form_file(is_file_function(v).group(2))
                for k, v in req.form_params.items()
                if is_file_function(v)
            }
        elif req.request_body:
            kwargs["data"] = req.request_body

        # Signal API call started
        progress_message = f"{req.http_method} call to {req.http_url}"
        if req.is_fuzzed():
            http_exchange_signals.fuzzed_request_started.emit(
                progress_message, self.exchange.api_call_id
            )
        else:
            http_exchange_signals.request_started.emit(
                progress_message, self.exchange.api_call_id
            )

        if self.exchange.response.is_mocked:
            self.mock_exchange(var_tokens)
        else:
            response, err = self.requester.make_request(
                req.http_method, req.http_url, kwargs
            )
            self.exchange.request = self.update_request(
                self.exchange.request, response.request
            )

            # Building exchange response
            if err and isinstance(err, ConnectionError):
                nce: NewConnectionError = err.args[0].reason
                error_response = ExchangeResponse(
                    http_status_code=-1, response_body=str(nce.args[0])
                )
                self.exchange.response = error_response
                self.exchange.failed()
            elif err:
                error_response = ExchangeResponse(
                    http_status_code=-1, response_body=str(err)
                )
                self.exchange.response = error_response
                self.exchange.failed()
            else:
                self.exchange.response = self.convert_response(response)
                self.exchange.passed()

            logging.info(
                f"<== make_http_call({self.exchange.api_call_id}): "
                f"Received response in {self.exchange.response.elapsed_time}"
            )

            # Cleanup (for both success/failure)
            for fk, fv in kwargs.get("files", {}).items():
                fv.close()

            # This is to make sure that we cleanly quit this thread
            if self.halt_processing:
                self.halt_processing = False
                http_exchange_signals.request_finished.emit()
                http_exchange_signals.fuzzed_request_finished.emit()
                return

        if self.exchange.is_passed():
            self.signals.result.emit(self.exchange)
        else:
            self.signals.error.emit(self.exchange)

        if req.is_fuzzed():
            http_exchange_signals.fuzzed_request_finished.emit(
                self.exchange.api_call_id
            )
        else:
            http_exchange_signals.request_finished.emit(self.exchange.api_call_id)