Example #1
0
    async def test_client_always_has_default_timeout_configured(self):

        client = HttpClient()
        client.session_class = self.session_class_mock

        await client.get(TEST_URL)

        client.session_class.assert_called_with(
            timeout=default_http_client_timeout,
            headers=ANY,
            raise_for_status=True,
        )
Example #2
0
    async def test_delete(self):
        expected_headers = {"X-Header": "Value"}
        timeout = ClientTimeout(connect=5, total=10)
        client = HttpClient()
        client.session_class = self.session_class_mock

        await client.delete(TEST_URL, timeout=timeout, headers=expected_headers)
        client._session.request.assert_awaited_with(
            "delete",
            TEST_URL,
            timeout=timeout,
            headers=expected_headers,
            raise_for_status=True,
            allow_redirects=True,
        )
Example #3
0
    async def test_always_make_request_with_follow_redirect(self):
        """
        Seguimos redirect por padrão
        """
        client = HttpClient()
        client.session_class = self.session_class_mock

        await client.get(TEST_URL)
        client._session.request.assert_called_with(
            "get",
            TEST_URL,
            timeout=ANY,
            headers=ANY,
            allow_redirects=True,
            raise_for_status=True,
        )
Example #4
0
    async def test_can_override_option_to_automatically_raise_when_request_fails(
        self
    ):
        timeout = ClientTimeout(connect=1, total=5)
        client = HttpClient()
        client.session_class = self.session_class_mock

        await client.get(TEST_URL, raise_for_status=False)
        client._session.request.assert_awaited_with(
            "get",
            ANY,
            timeout=ANY,
            headers=ANY,
            raise_for_status=False,
            allow_redirects=True,
        )
Example #5
0
    async def test_can_pass_extra_kwarg_to_aiohttp_client_sesson(self):
        """
        Confirmamos que quando passamos argumentos extras para o HttpClient
        isso é repassado para o ClientSession
        """
        client = HttpClient()
        client.session_class = self.session_class_mock

        await client.put(TEST_URL, json={"key": "value"})
        client._session.request.assert_awaited_with(
            "put",
            TEST_URL,
            timeout=None,
            headers={},
            raise_for_status=True,
            allow_redirects=True,
            json={"key": "value"},
        )
Example #6
0
    async def test_can_override_timeout_passing_a_new_timeout_on_the_request(
        self
    ):
        """
        client.get(..., timeout=ClientTimeout(...))
        """
        timeout = ClientTimeout(connect=1, total=5)
        client = HttpClient()
        client.session_class = self.session_class_mock

        await client.get(TEST_URL, timeout=timeout)
        client._session.request.assert_awaited_with(
            "get",
            ANY,
            timeout=timeout,
            headers=ANY,
            allow_redirects=True,
            raise_for_status=True,
        )
Example #7
0
    async def test_can_choose_a_different_timeout_on_client_instantiation(self):
        new_timeout = ClientTimeout(total=2, connect=5)
        client = HttpClient(timeout=new_timeout)
        client.session_class = self.session_class_mock

        await client.get(TEST_URL)

        client.session_class.assert_called_with(
            timeout=new_timeout, headers=ANY, raise_for_status=True
        )

        client._session.request.assert_awaited_with(
            "get",
            ANY,
            timeout=None,
            headers=ANY,
            allow_redirects=True,
            raise_for_status=True,
        )
Example #8
0
    async def test_raise_bad_request_exception_when_response_is_400(self):
        client = HttpClient()
        url = "https://httpbin.org/status/400"

        try:
            await client.get(url)
        except HTTPBadRequest as e:
            self.assertEqual(HTTPStatus.BAD_REQUEST, e.status)
            self.assertEqual(url, str(e.request_info.url))
            self.assertEqual("GET", e.request_info.method)
            self.assertIsNotNone(e.request_info.headers)
Example #9
0
    async def test_raise_internal_error_exception_when_response_is_500(self):
        client = HttpClient()
        url = "https://httpbin.org/status/500"

        try:
            await client.get(url)
        except HTTPInternalServerError as e:
            self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, e.status)
            self.assertEqual(url, str(e.request_info.url))
            self.assertEqual("GET", e.request_info.method)
            self.assertIsNotNone(e.request_info.headers)
Example #10
0
    async def test_reuse_session_on_subsequent_requests(self):
        client = HttpClient()
        with aioresponses() as rsps:
            rsps.get(TEST_URL, status=200)
            rsps.get(TEST_URL, status=200)

            await client.get(TEST_URL)

            client_session = client._session

            await client.get(TEST_URL)

            self.assertTrue(client_session is client._session)
Example #11
0
 def __init__(
     self,
     url: str,
     user: Optional[str] = None,
     password: Optional[str] = None,
 ) -> None:
     self.client = HttpClient()
     self.address = url
     self.base_url = f"{self.address}/v1/scheduler"
     self.auth_data = None
     if user and password:
         auth_string = f"{user}:{password}"
         self.auth_data = b64encode(
             auth_string.encode("utf8")).decode("utf8")
Example #12
0
    async def test_re_raise_original_exception(self):
        """
        Se o request lançar uma exception que não estamos tratando, devemos re-lançar.
        """
        client = HttpClient()
        url = "https://httpbin.org/status/415"

        try:
            await client.get(url)
        except ClientResponseError as e:
            self.assertEqual(HTTPStatus.UNSUPPORTED_MEDIA_TYPE, e.status)
            self.assertEqual(url, str(e.request_info.url))
            self.assertEqual("GET", e.request_info.method)
            self.assertIsNotNone(e.request_info.headers)
Example #13
0
    async def test_can_merge_default_headers_with_headers_passed_on_the_request_real_request(
        self
    ):
        """
        O aioresponses não funciona quando usamos default_headers no ClientSession.
        """
        default_headers = {"X-Header": "Value"}
        additional_headers = {"X-More": "Other"}

        client = HttpClient(headers=default_headers)
        resp = await client.get(
            "https://httpbin.org/headers", headers=additional_headers
        )
        self.assertEqual(HTTPStatus.OK, resp.status)

        resp_data = await resp.json()
        self.assertEqual("Value", resp_data["headers"].get("X-Header"))
        self.assertEqual("Other", resp_data["headers"].get("X-More"))
Example #14
0
    async def test_headers_passed_on_the_request_ovewrites_default_headers(
        self
    ):
        """
        Se um header passado em `client.get(..., headers={})` tiver o mesmo nome de um
        outro header passado na instanciação do http client, o header do request
        deve ser usado
        """
        default_headers = {"X-Header": "Value"}
        additional_headers = {"X-More": "Other", "X-Header": "Override"}

        client = HttpClient(headers=default_headers)
        with aioresponses() as rsps:
            rsps.get(TEST_URL, status=200)
            resp = await client.get(TEST_URL, headers=additional_headers)
            self.assertEqual(HTTPStatus.OK, resp.status)

            req = rsps.requests.get(("get", URL(TEST_URL)))
            expected_headers = {**additional_headers}
            self.assertEqual(expected_headers, req[0].kwargs.get("headers"))
Example #15
0
    async def test_exceptions_have_detail_info_about_the_request_that_failed(
        self
    ):
        """
        Cada exceção lançada deve carregar algumas infos sobre o request original.
        A ClientResponseError da aiohttp tem tudo que queremos.

        A exception lançada pelo client contém:
          - request_info original
          - status (int)
        """
        client = HttpClient()
        url = "https://httpbin.org/status/404"

        try:
            await client.get(url)
        except HTTPNotFound as e:
            self.assertEqual(HTTPStatus.NOT_FOUND, e.status)
            self.assertEqual(url, str(e.request_info.url))
            self.assertEqual("GET", e.request_info.method)
            self.assertIsNotNone(e.request_info.headers)
Example #16
0
 def __init__(self):
     self._http_client = HttpClient(
         headers={
             "Content-Type": "application/json",
             "Authorization": f"Token {settings.AUTOSCALER_AUTH_TOKEN}",
         })
Example #17
0
        """
        Carrega no elasticsearch local os datapoints de estatísticas de apps
        O `timestamp` usado em cada datapoint é o `timestamp_to_use`.
        """
        for datapoint in datapoints:
            datapoint["timestamp"] = timestamp_to_use.isoformat()
            await self.esclient.index(
                index=index_name, doc_type="stats", body=datapoint
            )
        # para dar tempo do Elasticsearch local indexar os dados recém inseridos
        await asyncio.sleep(3)


CHRONOS_BASE_URL = f"{settings.SCHEDULED_JOBS_SERVICE_ADDRESS}/v1/scheduler"

http_client = HttpClient()


async def _cleanup_chronos():
    resp = await http_client.get(f"{CHRONOS_BASE_URL}/jobs")
    all_jobs_json = await resp.json()
    for job in all_jobs_json:
        await http_client.delete(f"{CHRONOS_BASE_URL}/job/{job['name']}")


async def _load_jobs_into_chronos(*jobs_list):
    await _cleanup_chronos()
    for job in jobs_list:
        await http_client.post(f"{CHRONOS_BASE_URL}/iso8601", json=job)

    await asyncio.sleep(1)
Example #18
0
 def __init__(self, mesos_api_url: str, *aditional_api_urls) -> None:
     self.mesos_adresses = tuple([mesos_api_url]) + aditional_api_urls
     self.http_client = HttpClient()