Exemplo n.º 1
0
def test_callback_returning_response(httpx_mock: HTTPXMock):
    def custom_response(request: httpx.Request, *args, **kwargs):
        return to_response(json={"url": str(request.url)})

    httpx_mock.add_callback(custom_response, url="http://test_url")

    with httpx.Client() as client:
        response = client.get("http://test_url")
        assert response.json() == {"url": "http://test_url"}
Exemplo n.º 2
0
async def test_callback_raising_exception(httpx_mock: HTTPXMock):
    def raise_timeout(request, ext):
        raise httpx.ReadTimeout(
            f"Unable to read within {ext['timeout']['read']}", request=request)

    httpx_mock.add_callback(raise_timeout, url="http://test_url")

    async with httpx.AsyncClient() as client:
        with pytest.raises(httpx.ReadTimeout) as exception_info:
            await client.get("http://test_url")
        assert str(exception_info.value) == "Unable to read within 5.0"
Exemplo n.º 3
0
def test_callback_matching_method(httpx_mock: HTTPXMock):
    def custom_response(*args, **kwargs) -> httpx.Response:
        return to_response(json=["content"])

    httpx_mock.add_callback(custom_response, method="GET")

    with httpx.Client() as client:
        response = client.get("http://test_url")
        assert response.json() == ["content"]

        response = client.get("http://test_url2")
        assert response.json() == ["content"]
Exemplo n.º 4
0
def test_callback_executed_twice(httpx_mock: HTTPXMock):
    def custom_response(*args, **kwargs):
        return to_response(json=["content"])

    httpx_mock.add_callback(custom_response)

    with httpx.Client() as client:
        response = client.get("http://test_url")
        assert response.json() == ["content"]

        response = client.post("http://test_url")
        assert response.json() == ["content"]
Exemplo n.º 5
0
 async def test_attributes_not_none(self, httpx_mock: HTTPXMock):
     """Check that certain attributes are not None."""
     httpx_mock.add_callback(self.custom_matcher)
     for receiver, spec in TESTING_RECEIVERS.items():
         print("Receiver: {}".format(receiver))
         # Switch receiver and update to load new sample files
         self.testing_receiver = receiver
         self.denon = denonavr.DenonAVR(FAKE_IP, add_zones=spec[0])
         await self.denon.async_update()
         assert self.denon.power is not None, (
             "Power status is None for receiver {}".format(receiver))
         assert self.denon.state is not None, (
             "State is None for receiver {}".format(receiver))
Exemplo n.º 6
0
async def test_timeout(httpx_mock: HTTPXMock):
    """Test if the connection is hitting the timeout."""
    def raise_timeout(request):
        """Set the timeout for the requests."""
        raise httpx.ReadTimeout(
            f"Unable to read within {request.extensions['timeout']}",
            request=request)

    httpx_mock.add_callback(raise_timeout)

    with pytest.raises(httpx.ReadTimeout):
        client = Luftdaten(SENSOR_ID)
        await client.get_data()
async def test_timeout(httpx_mock: HTTPXMock):
    """Test if the connection is hitting the timeout."""
    def raise_timeout(request):
        """Set the timeout for the requests."""
        raise httpx.ReadTimeout(
            f"Unable to read within {request.extensions['timeout']}",
            request=request)

    httpx_mock.add_callback(raise_timeout)

    with pytest.raises(exceptions.GlancesApiConnectionError):
        client = Glances()
        await client.get_metrics("mem")
Exemplo n.º 8
0
 async def test_input_func_switch(self, httpx_mock: HTTPXMock):
     """Switch through all input functions of all tested receivers."""
     httpx_mock.add_callback(self.custom_matcher)
     for receiver, spec in TESTING_RECEIVERS.items():
         # Switch receiver and update to load new sample files
         self.testing_receiver = receiver
         self.denon = denonavr.DenonAVR(FAKE_IP, add_zones=spec[0])
         # Switch through all functions and check if successful
         for name, zone in self.denon.zones.items():
             print("Receiver: {}, Zone: {}".format(receiver, name))
             await self.denon.zones[name].async_update()
             assert len(zone.input_func_list) > 0
             for input_func in zone.input_func_list:
                 await self.denon.zones[name].async_set_input_func(
                     input_func)
Exemplo n.º 9
0
 async def test_receiver_type(self, httpx_mock: HTTPXMock):
     """Check that receiver type is determined correctly."""
     httpx_mock.add_callback(self.custom_matcher)
     for receiver, spec in TESTING_RECEIVERS.items():
         print("Receiver: {}".format(receiver))
         # Switch receiver and update to load new sample files
         self.testing_receiver = receiver
         self.denon = denonavr.DenonAVR(FAKE_IP, add_zones=spec[0])
         await self.denon.async_update()
         assert self.denon.receiver_type == spec[1].type, (
             "Receiver type is {} not {} for receiver {}".format(
                 self.denon.receiver_type, spec[1].type, receiver))
         assert self.denon.receiver_port == spec[1].port, (
             "Receiver port is {} not {} for receiver {}".format(
                 self.denon.receiver_port, spec[1].port, receiver))
Exemplo n.º 10
0
 async def test_sound_mode(self, httpx_mock: HTTPXMock):
     """Check if a valid sound mode is returned."""
     httpx_mock.add_callback(self.custom_matcher)
     for receiver, spec in TESTING_RECEIVERS.items():
         # Switch receiver and update to load new sample files
         self.testing_receiver = receiver
         self.denon = denonavr.DenonAVR(FAKE_IP, add_zones=spec[0])
         # Switch through all functions and check if successful
         for name in self.denon.zones:
             print("Receiver: {}, Zone: {}".format(receiver, name))
             await self.denon.zones[name].async_update()
             support_sound_mode = self.denon.zones[name].support_sound_mode
             sound_mode = self.denon.zones[name].sound_mode
             assert (
                 sound_mode in [*SOUND_MODE_MAPPING, None] or
                 support_sound_mode is not True)
Exemplo n.º 11
0
def test_callback_with_pattern_in_url(httpx_mock: HTTPXMock):
    def custom_response(request: httpx.Request, *args, **kwargs):
        return to_response(json={"url": str(request.url)})

    def custom_response2(request: httpx.Request, *args, **kwargs):
        return to_response(http_version="HTTP/2.0", json={"url": str(request.url)})

    httpx_mock.add_callback(custom_response, url=re.compile(".*test.*"))
    httpx_mock.add_callback(custom_response2, url="http://unmatched")

    with httpx.Client() as client:
        response = client.get("http://unmatched")
        assert response.http_version == "HTTP/2.0"

        response = client.get("http://test_url")
        assert response.http_version == "HTTP/1.1"