Example #1
0
 def _empty_response(self, status_code: int, headers: typing.Mapping[str, str]):
     return Response(
         statusCode=status_code,
         body="",
         headers=headers,
         bodyAsJson=None,
         timestamp=None,
     )
Example #2
0
def gen_authflow_response(req: Request):
    if "redirect_uri" not in req.query:
        return Response(body="", statusCode=400, headers={})
    redirect_uri = req.query["redirect_uri"]

    redirect_uri = (redirect_uri if isinstance(redirect_uri, str) else
                    "".join(redirect_uri))

    code = generate_code()

    redirect_uri = f"{redirect_uri}?code={code}"

    if "state" in req.query:
        state = req.query["state"]
        redirect_uri = f"{redirect_uri}&state={state}"

    headers = dict(location=redirect_uri)
    return Response(body="", statusCode=302, headers=headers)
Example #3
0
    def _fake_json(
        self, status_code: int, headers: typing.Mapping[str, str], faker_data: FakerData
    ):
        bodyAsJson = self._fake_it(faker_data, faker_data.schema, 0)

        return Response(
            statusCode=status_code,
            body=json.dumps(bodyAsJson),
            bodyAsJson=bodyAsJson,
            headers=headers,
            timestamp=None,
        )
Example #4
0
 def _fake_text_response(self, status_code: int, headers: typing.Mapping[str, str]):
     return Response(
         statusCode=status_code,
         body=self._text_faker.sentence(),
         # TODO: can this be accomplished without a cast?
         headers=cast(
             Mapping[str, Union[str, Sequence[str]]],
             {**headers, "Content-Type": "text/plain"},
         ),
         bodyAsJson=None,
         timestamp=None,
     )
Example #5
0
    def flush(self, check_length=True):
        if len(self._response) > 0 and self._request is not None:
            if (
                not check_length
                or self._content_length is None
                or self._recieved_bytes >= self._content_length
            ):
                logger.debug(
                    "[%s] Flushing request, received %d bytes out of %d",
                    self._client_address,
                    self._recieved_bytes,
                    self._content_length or 0,
                )

                resp = b"".join(self._response)
                resp = response_from_bytes(resp)
                body = resp.data.decode("utf-8")

                resp = Response(
                    statusCode=resp.status,
                    body=body,
                    bodyAsJson=json.loads(body),
                    headers=dict(resp.getheaders()),
                    timestamp=None,
                )

                self._proxy_calback.on_request_complete(self._request, resp)

                self._request = None
                self._response = []
                self._content_length = None
                self._recieved_bytes = 0
        else:
            logger.debug(
                '[%s] Trying to flush incomplete data, request "%s", response chunks %d',
                self._client_address,
                str(self._request),
                len(self._response),
            )
Example #6
0
 def match_error(self, msg: str, req: Request):
     json_resp = {
         "message":
         "%s. Here is the full request: host=%s, path=%s, method=%s." %
         (msg, req.host, req.path, req.method.value)
     }
     return Response(
         statusCode=501,
         body=json.dumps(json_resp),
         bodyAsJson=json_resp,
         headers={},
         timestamp=None,
     )
Example #7
0
    pokeapi_schema = build_schema_batch(pokeapi_requests, UpdateMode.REPLAY)
    paths = list(pokeapi_schema.paths.keys())
    assert_that(paths, has_length(14))
    assert_that(paths, has_item("/v2/pokemon/"))
    assert_that(paths, has_item(matches_regexp(r"\/v2\/pokemon\/[\w]+\/")))
    assert_that(paths, has_item(matches_regexp(r"\/v2\/type\/[\w]+")))
    assert_that(paths, has_item(matches_regexp(r"\/v2\/ability\/[\w]+")))


get_pets_req = RequestBuilder.from_url("http://petstore.swagger.io/v1/pets")
get_one_pet_req = RequestBuilder.from_url(
    "http://petstore.swagger.io/v1/pets/32")

pet_res = Response(bodyAsJson=None,
                   timestamp=None,
                   body="",
                   statusCode=200,
                   headers={})


@pytest.fixture
def get_one_pet_exchange():
    return HttpExchange(request=get_one_pet_req, response=pet_res)


@pytest.fixture
def get_pets_exchange():
    return HttpExchange(request=get_pets_req, response=pet_res)


@pytest.fixture