Exemple #1
0
    async def test_body_bytes_get_passed_untouched(self):
        t = AsyncTransport([{}], connection_class=DummyConnection)

        body = b"\xe4\xbd\xa0\xe5\xa5\xbd"
        await t.perform_request("GET", "/", body=body)
        assert 1 == len(t.get_connection().calls)
        assert ("GET", "/", None, body) == t.get_connection().calls[0][0]
    async def test_client_meta_header(self):
        t = AsyncTransport([{}], connection_class=DummyConnection)

        await t.perform_request("GET", "/", body={})
        assert len(t.get_connection().calls) == 1
        headers = t.get_connection().calls[0][1]["headers"]
        assert re.match(
            r"^es=[0-9.]+p?,py=[0-9.]+p?,t=[0-9.]+p?$",
            headers["x-elastic-client-meta"],
        )

        class DummyConnectionWithMeta(DummyConnection):
            HTTP_CLIENT_META = ("dm", "1.2.3")

        t = AsyncTransport([{}], connection_class=DummyConnectionWithMeta)

        await t.perform_request("GET",
                                "/",
                                body={},
                                headers={"Custom": "header"})
        assert len(t.get_connection().calls) == 1
        headers = t.get_connection().calls[0][1]["headers"]
        assert re.match(
            r"^es=[0-9.]+p?,py=[0-9.]+p?,t=[0-9.]+p?,dm=1.2.3$",
            headers["x-elastic-client-meta"],
        )
        assert headers["Custom"] == "header"
Exemple #3
0
    async def test_send_get_body_as_post(self):
        t = AsyncTransport([{}],
                           send_get_body_as="POST",
                           connection_class=DummyConnection)

        await t.perform_request("GET", "/", body={})
        assert 1 == len(t.get_connection().calls)
        assert ("POST", "/", None, b"{}") == t.get_connection().calls[0][0]
    async def test_client_meta_header_not_sent(self):
        t = AsyncTransport([{}],
                           meta_header=False,
                           connection_class=DummyConnection)

        await t.perform_request("GET", "/", body={})
        assert len(t.get_connection().calls) == 1
        headers = t.get_connection().calls[0][1]["headers"]
        assert headers is None
Exemple #5
0
    async def test_send_get_body_as_source(self):
        t = AsyncTransport([{}],
                           send_get_body_as="source",
                           connection_class=DummyConnection)

        await t.perform_request("GET", "/", body={})
        assert 1 == len(t.get_connection().calls)
        assert ("GET", "/", {
            "source": "{}"
        }, None) == t.get_connection().calls[0][0]
Exemple #6
0
    async def test_body_surrogates_replaced_encoded_into_bytes(self):
        t = AsyncTransport([{}], connection_class=DummyConnection)

        await t.perform_request("GET", "/", body="你好\uda6a")
        assert 1 == len(t.get_connection().calls)
        assert (
            "GET",
            "/",
            None,
            b"\xe4\xbd\xa0\xe5\xa5\xbd\xed\xa9\xaa",
        ) == t.get_connection().calls[0][0]
Exemple #7
0
    async def test_request_timeout_extracted_from_params_and_passed(self):
        t = AsyncTransport([{}], connection_class=DummyConnection)

        await t.perform_request("GET", "/", params={"request_timeout": 42})
        assert 1 == len(t.get_connection().calls)
        assert ("GET", "/", {}, None) == t.get_connection().calls[0][0]
        assert {
            "timeout": 42,
            "ignore": (),
            "headers": None,
        } == t.get_connection().calls[0][1]
Exemple #8
0
    async def test_request_with_custom_user_agent_header(self):
        t = AsyncTransport([{}], connection_class=DummyConnection)

        await t.perform_request(
            "GET", "/", headers={"user-agent": "my-custom-value/1.2.3"})
        assert 1 == len(t.get_connection().calls)
        assert {
            "timeout": None,
            "ignore": (),
            "headers": {
                "user-agent": "my-custom-value/1.2.3"
            },
        } == t.get_connection().calls[0][1]
Exemple #9
0
    async def test_sniff_will_use_seed_connections(self):
        t = AsyncTransport([{
            "data": CLUSTER_NODES
        }],
                           connection_class=DummyConnection)
        await t._async_call()
        t.set_connections([{"data": "invalid"}])

        await t.sniff_hosts()
        assert 1 == len(t.connection_pool.connections)
        assert "http://1.1.1.1:123" == t.get_connection().host
Exemple #10
0
    async def test_opaque_id(self):
        t = AsyncTransport([{}],
                           opaque_id="app-1",
                           connection_class=DummyConnection)

        await t.perform_request("GET", "/")
        assert 1 == len(t.get_connection().calls)
        assert ("GET", "/", None, None) == t.get_connection().calls[0][0]
        assert {
            "timeout": None,
            "ignore": (),
            "headers": None,
        } == t.get_connection().calls[0][1]

        # Now try with an 'x-opaque-id' set on perform_request().
        await t.perform_request("GET",
                                "/",
                                headers={"x-opaque-id": "request-1"})
        assert 2 == len(t.get_connection().calls)
        assert ("GET", "/", None, None) == t.get_connection().calls[1][0]
        assert {
            "timeout": None,
            "ignore": (),
            "headers": {
                "x-opaque-id": "request-1"
            },
        } == t.get_connection().calls[1][1]
Exemple #11
0
    async def test_request_will_fail_after_X_retries(self):
        t = AsyncTransport(
            [{
                "exception": ConnectionError("abandon ship")
            }],
            connection_class=DummyConnection,
        )

        connection_error = False
        try:
            await t.perform_request("GET", "/")
        except ConnectionError:
            connection_error = True

        assert connection_error
        assert 4 == len(t.get_connection().calls)