[7.x] Move client meta header logic to Transport
Co-authored-by: Seth Michael Larson <[email protected]>
This commit is contained in:
co-authored by
Seth Michael Larson
parent
82e42e6435
commit
fe5367382b
@@ -17,7 +17,6 @@
|
||||
# under the License.
|
||||
|
||||
import ssl
|
||||
import re
|
||||
import gzip
|
||||
import io
|
||||
from mock import patch
|
||||
@@ -317,43 +316,3 @@ class TestAIOHttpConnection:
|
||||
con = await self._get_mock_connection(response_body=buf)
|
||||
status, headers, data = await con.perform_request("GET", "/")
|
||||
assert u"你好\uda6a" == data
|
||||
|
||||
async def test_meta_header_value(self):
|
||||
con = await self._get_mock_connection()
|
||||
assert con.meta_header is True
|
||||
|
||||
await con.perform_request("GET", "/", body=b"{}")
|
||||
|
||||
_, kwargs = con.session.request.call_args
|
||||
headers = kwargs["headers"]
|
||||
assert re.match(
|
||||
r"^es=[0-9]+\.[0-9]+\.[0-9]+p?,py=[0-9]+\.[0-9]+\.[0-9]+p?,"
|
||||
r"t=[0-9]+\.[0-9]+\.[0-9]+p?,ai=[0-9]+\.[0-9]+\.[0-9]+p?$",
|
||||
headers["x-elastic-client-meta"],
|
||||
)
|
||||
|
||||
con = await self._get_mock_connection()
|
||||
assert con.meta_header is True
|
||||
|
||||
await con.perform_request(
|
||||
"GET", "/", body=b"{}", params={"_client_meta": (("h", "bp"),)}
|
||||
)
|
||||
|
||||
(method, url), kwargs = con.session.request.call_args
|
||||
headers = kwargs["headers"]
|
||||
assert method == "GET"
|
||||
assert str(url) == "http://localhost:9200/"
|
||||
assert re.match(
|
||||
r"^es=[0-9]+\.[0-9]+\.[0-9]+p?,py=[0-9]+\.[0-9]+\.[0-9]+p?,"
|
||||
r"t=[0-9]+\.[0-9]+\.[0-9]+p?,ai=[0-9]+\.[0-9]+\.[0-9]+p?,h=bp$",
|
||||
headers["x-elastic-client-meta"],
|
||||
)
|
||||
|
||||
con = await self._get_mock_connection(connection_params={"meta_header": False})
|
||||
assert con.meta_header is False
|
||||
|
||||
await con.perform_request("GET", "/", body=b"{}")
|
||||
|
||||
_, kwargs = con.session.request.call_args
|
||||
headers = kwargs["headers"]
|
||||
assert "x-elastic-client-meta" not in (x.lower() for x in headers)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
# under the License.
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import re
|
||||
import asyncio
|
||||
import json
|
||||
from mock import patch
|
||||
@@ -118,7 +119,7 @@ class TestTransport:
|
||||
assert isinstance(t.connection_pool, DummyConnectionPool)
|
||||
|
||||
async def test_request_timeout_extracted_from_params_and_passed(self):
|
||||
t = AsyncTransport([{}], connection_class=DummyConnection)
|
||||
t = AsyncTransport([{}], connection_class=DummyConnection, meta_header=False)
|
||||
|
||||
await t.perform_request("GET", "/", params={"request_timeout": 42})
|
||||
assert 1 == len(t.get_connection().calls)
|
||||
@@ -130,7 +131,9 @@ class TestTransport:
|
||||
} == t.get_connection().calls[0][1]
|
||||
|
||||
async def test_opaque_id(self):
|
||||
t = AsyncTransport([{}], opaque_id="app-1", connection_class=DummyConnection)
|
||||
t = AsyncTransport(
|
||||
[{}], opaque_id="app-1", connection_class=DummyConnection, meta_header=False
|
||||
)
|
||||
|
||||
await t.perform_request("GET", "/")
|
||||
assert 1 == len(t.get_connection().calls)
|
||||
@@ -152,7 +155,7 @@ class TestTransport:
|
||||
} == t.get_connection().calls[1][1]
|
||||
|
||||
async def test_request_with_custom_user_agent_header(self):
|
||||
t = AsyncTransport([{}], connection_class=DummyConnection)
|
||||
t = AsyncTransport([{}], connection_class=DummyConnection, meta_header=False)
|
||||
|
||||
await t.perform_request(
|
||||
"GET", "/", headers={"user-agent": "my-custom-value/1.2.3"}
|
||||
@@ -182,6 +185,39 @@ class TestTransport:
|
||||
assert 1 == len(t.get_connection().calls)
|
||||
assert ("POST", "/", None, b"{}") == 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"
|
||||
|
||||
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
|
||||
|
||||
async def test_body_gets_encoded_into_bytes(self):
|
||||
t = AsyncTransport([{}], connection_class=DummyConnection)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user