Skip to content

Commit 07151b3

Browse files
[7.x] Skip default headers from AIOHttpConnection
Co-authored-by: Seth Michael Larson <seth.larson@elastic.co>
1 parent c781f3e commit 07151b3

File tree

3 files changed

+211
-0
lines changed

3 files changed

+211
-0
lines changed

elasticsearch/_async/http_aiohttp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ async def _create_aiohttp_session(self):
360360
self.loop = get_running_loop()
361361
self.session = aiohttp.ClientSession(
362362
headers=self.headers,
363+
skip_auto_headers=("accept", "accept-encoding"),
363364
auto_decompress=True,
364365
loop=self.loop,
365366
cookie_jar=aiohttp.DummyCookieJar(),

test_elasticsearch/test_async/test_connection.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import gzip
2020
import io
21+
import json
2122
import ssl
2223
import warnings
2324
from platform import python_version
@@ -316,3 +317,75 @@ async def test_surrogatepass_into_bytes(self):
316317
con = await self._get_mock_connection(response_body=buf)
317318
status, headers, data = await con.perform_request("GET", "/")
318319
assert u"你好\uda6a" == data
320+
321+
322+
class TestConnectionHttpbin:
323+
"""Tests the HTTP connection implementations against a live server E2E"""
324+
325+
async def httpbin_anything(self, conn, **kwargs):
326+
status, headers, data = await conn.perform_request("GET", "/anything", **kwargs)
327+
data = json.loads(data)
328+
data["headers"].pop(
329+
"X-Amzn-Trace-Id", None
330+
) # Remove this header as it's put there by AWS.
331+
return (status, data)
332+
333+
async def test_aiohttp_connection(self):
334+
# Defaults
335+
conn = AIOHttpConnection("httpbin.org", port=443, use_ssl=True)
336+
user_agent = conn._get_default_user_agent()
337+
status, data = await self.httpbin_anything(conn)
338+
assert status == 200
339+
assert data["method"] == "GET"
340+
assert data["headers"] == {
341+
"Content-Type": "application/json",
342+
"Host": "httpbin.org",
343+
"User-Agent": user_agent,
344+
}
345+
346+
# http_compress=False
347+
conn = AIOHttpConnection(
348+
"httpbin.org", port=443, use_ssl=True, http_compress=False
349+
)
350+
status, data = await self.httpbin_anything(conn)
351+
assert status == 200
352+
assert data["method"] == "GET"
353+
assert data["headers"] == {
354+
"Content-Type": "application/json",
355+
"Host": "httpbin.org",
356+
"User-Agent": user_agent,
357+
}
358+
359+
# http_compress=True
360+
conn = AIOHttpConnection(
361+
"httpbin.org", port=443, use_ssl=True, http_compress=True
362+
)
363+
status, data = await self.httpbin_anything(conn)
364+
assert status == 200
365+
assert data["headers"] == {
366+
"Accept-Encoding": "gzip,deflate",
367+
"Content-Type": "application/json",
368+
"Host": "httpbin.org",
369+
"User-Agent": user_agent,
370+
}
371+
372+
# Headers
373+
conn = AIOHttpConnection(
374+
"httpbin.org",
375+
port=443,
376+
use_ssl=True,
377+
http_compress=True,
378+
headers={"header1": "value1"},
379+
)
380+
status, data = await self.httpbin_anything(
381+
conn, headers={"header2": "value2", "header1": "override!"}
382+
)
383+
assert status == 200
384+
assert data["headers"] == {
385+
"Accept-Encoding": "gzip,deflate",
386+
"Content-Type": "application/json",
387+
"Host": "httpbin.org",
388+
"Header1": "override!",
389+
"Header2": "value2",
390+
"User-Agent": user_agent,
391+
}

test_elasticsearch/test_connection.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import gzip
2020
import io
21+
import json
2122
import os
2223
import re
2324
import ssl
@@ -866,3 +867,139 @@ def test_surrogatepass_into_bytes(self):
866867
con = self._get_mock_connection(response_body=buf)
867868
status, headers, data = con.perform_request("GET", "/")
868869
self.assertEqual(u"你好\uda6a", data)
870+
871+
872+
class TestConnectionHttpbin:
873+
"""Tests the HTTP connection implementations against a live server E2E"""
874+
875+
def httpbin_anything(self, conn, **kwargs):
876+
status, headers, data = conn.perform_request("GET", "/anything", **kwargs)
877+
data = json.loads(data)
878+
data["headers"].pop(
879+
"X-Amzn-Trace-Id", None
880+
) # Remove this header as it's put there by AWS.
881+
return (status, data)
882+
883+
def test_urllib3_connection(self):
884+
# Defaults
885+
conn = Urllib3HttpConnection("httpbin.org", port=443, use_ssl=True)
886+
user_agent = conn._get_default_user_agent()
887+
status, data = self.httpbin_anything(conn)
888+
assert status == 200
889+
assert data["method"] == "GET"
890+
assert data["headers"] == {
891+
"Accept-Encoding": "identity",
892+
"Content-Type": "application/json",
893+
"Host": "httpbin.org",
894+
"User-Agent": user_agent,
895+
}
896+
897+
# http_compress=False
898+
conn = Urllib3HttpConnection(
899+
"httpbin.org", port=443, use_ssl=True, http_compress=False
900+
)
901+
status, data = self.httpbin_anything(conn)
902+
assert status == 200
903+
assert data["method"] == "GET"
904+
assert data["headers"] == {
905+
"Accept-Encoding": "identity",
906+
"Content-Type": "application/json",
907+
"Host": "httpbin.org",
908+
"User-Agent": user_agent,
909+
}
910+
911+
# http_compress=True
912+
conn = Urllib3HttpConnection(
913+
"httpbin.org", port=443, use_ssl=True, http_compress=True
914+
)
915+
status, data = self.httpbin_anything(conn)
916+
assert status == 200
917+
assert data["headers"] == {
918+
"Accept-Encoding": "gzip,deflate",
919+
"Content-Type": "application/json",
920+
"Host": "httpbin.org",
921+
"User-Agent": user_agent,
922+
}
923+
924+
# Headers
925+
conn = Urllib3HttpConnection(
926+
"httpbin.org",
927+
port=443,
928+
use_ssl=True,
929+
http_compress=True,
930+
headers={"header1": "value1"},
931+
)
932+
status, data = self.httpbin_anything(
933+
conn, headers={"header2": "value2", "header1": "override!"}
934+
)
935+
assert status == 200
936+
assert data["headers"] == {
937+
"Accept-Encoding": "gzip,deflate",
938+
"Content-Type": "application/json",
939+
"Host": "httpbin.org",
940+
"Header1": "override!",
941+
"Header2": "value2",
942+
"User-Agent": user_agent,
943+
}
944+
945+
def test_requests_connection(self):
946+
# Defaults
947+
conn = RequestsHttpConnection("httpbin.org", port=443, use_ssl=True)
948+
user_agent = conn._get_default_user_agent()
949+
status, data = self.httpbin_anything(conn)
950+
assert status == 200
951+
assert data["method"] == "GET"
952+
assert data["headers"] == {
953+
"Accept-Encoding": "identity",
954+
"Content-Type": "application/json",
955+
"Host": "httpbin.org",
956+
"User-Agent": user_agent,
957+
}
958+
959+
# http_compress=False
960+
conn = RequestsHttpConnection(
961+
"httpbin.org", port=443, use_ssl=True, http_compress=False
962+
)
963+
status, data = self.httpbin_anything(conn)
964+
assert status == 200
965+
assert data["method"] == "GET"
966+
assert data["headers"] == {
967+
"Accept-Encoding": "identity",
968+
"Content-Type": "application/json",
969+
"Host": "httpbin.org",
970+
"User-Agent": user_agent,
971+
}
972+
973+
# http_compress=True
974+
conn = RequestsHttpConnection(
975+
"httpbin.org", port=443, use_ssl=True, http_compress=True
976+
)
977+
status, data = self.httpbin_anything(conn)
978+
assert status == 200
979+
assert data["headers"] == {
980+
"Accept-Encoding": "gzip,deflate",
981+
"Content-Type": "application/json",
982+
"Host": "httpbin.org",
983+
"User-Agent": user_agent,
984+
}
985+
986+
# Headers
987+
conn = RequestsHttpConnection(
988+
"httpbin.org",
989+
port=443,
990+
use_ssl=True,
991+
http_compress=True,
992+
headers={"header1": "value1"},
993+
)
994+
status, data = self.httpbin_anything(
995+
conn, headers={"header2": "value2", "header1": "override!"}
996+
)
997+
assert status == 200
998+
assert data["headers"] == {
999+
"Accept-Encoding": "gzip,deflate",
1000+
"Content-Type": "application/json",
1001+
"Host": "httpbin.org",
1002+
"Header1": "override!",
1003+
"Header2": "value2",
1004+
"User-Agent": user_agent,
1005+
}

0 commit comments

Comments
 (0)