def test_keep_alive_is_on_by_default(self): con = AIOHttpConnection() assert { "connection": "keep-alive", "content-type": "application/json", "user-agent": con._get_default_user_agent(), } == con.headers
def test_default_user_agent(self): con = AIOHttpConnection() self.assertEqual( con._get_default_user_agent(), "elasticsearch-py/%s (Python %s)" % (__versionstr__, python_version()), )
def test_http_cloud_id(self): con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==" ) self.assertTrue(con.use_ssl) self.assertEqual( con.host, "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io") self.assertEqual(con.port, None) self.assertEqual( con.hostname, "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io") self.assertTrue(con.http_compress) con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", port=9243, ) self.assertEqual( con.host, "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io:9243", ) self.assertEqual(con.port, 9243) self.assertEqual( con.hostname, "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")
def test_http_auth_list(self): con = AIOHttpConnection(http_auth=["username", "secret"]) assert { "authorization": "Basic dXNlcm5hbWU6c2VjcmV0", "content-type": "application/json", "connection": "keep-alive", "user-agent": con._get_default_user_agent(), } == con.headers
def test_http_auth_tuple(self): con = AIOHttpConnection(http_auth=("username", "secret")) self.assertEqual( { "authorization": "Basic dXNlcm5hbWU6c2VjcmV0", "content-type": "application/json", "connection": "keep-alive", "user-agent": con._get_default_user_agent(), }, con.headers, )
def test_warns_if_using_non_default_ssl_kwargs_with_ssl_context(self): for kwargs in ( { "ssl_show_warn": False }, { "ssl_show_warn": True }, { "verify_certs": True }, { "verify_certs": False }, { "ca_certs": "/path/to/certs" }, { "ssl_show_warn": True, "ca_certs": "/path/to/certs" }, ): kwargs["ssl_context"] = ssl.create_default_context() with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") AIOHttpConnection(**kwargs) self.assertEqual(1, len(w)) self.assertEqual( "When using `ssl_context`, all other SSL related kwargs are ignored", str(w[0].message), )
async def _get_mock_connection(self, connection_params={}, response_body=b"{}"): con = AIOHttpConnection(**connection_params) await con._create_aiohttp_session() def _dummy_request(*args, **kwargs): class DummyResponse: async def __aenter__(self, *_, **__): return self async def __aexit__(self, *_, **__): pass async def text(self): return response_body.decode("utf-8", "surrogatepass") dummy_response = DummyResponse() dummy_response.headers = {} dummy_response.status = 200 _dummy_request.call_args = (args, kwargs) return dummy_response con.session.request = _dummy_request return con
async def test_nowarn_when_test_uses_https_if_verify_certs_is_off(self): with warnings.catch_warnings(record=True) as w: con = AIOHttpConnection(use_ssl=True, verify_certs=False, ssl_show_warn=False) await con._create_aiohttp_session() self.assertEqual(0, len(w)) self.assertIsInstance(con.session, aiohttp.ClientSession)
def test_uses_https_if_verify_certs_is_off(self): with warnings.catch_warnings(record=True) as w: con = AIOHttpConnection(use_ssl=True, verify_certs=False) assert 1 == len(w) assert ( "Connecting to https://localhost:9200 using SSL with verify_certs=False is insecure." == str(w[0].message)) assert con.use_ssl assert con.scheme == "https" assert con.host == "https://localhost:9200"
def test_uses_https_if_verify_certs_is_off(self): with warnings.catch_warnings(record=True) as w: con = AIOHttpConnection(use_ssl=True, verify_certs=False) self.assertEqual(1, len(w)) self.assertEqual( "Connecting to https://localhost:9200 using SSL with verify_certs=False is insecure.", str(w[0].message), ) self.assertTrue(con.use_ssl) self.assertEqual(con.scheme, "https") self.assertEqual(con.host, "https://localhost:9200")
def test_cloud_id_http_compress_override(self): # 'http_compress' will be 'True' by default for connections with # 'cloud_id' set but should prioritize user-defined values. con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", ) self.assertEqual(con.http_compress, True) con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", http_compress=False, ) self.assertEqual(con.http_compress, False) con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", http_compress=True, ) self.assertEqual(con.http_compress, True)
def test_api_key_auth(self): # test with tuple con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", api_key=("elastic", "changeme1"), ) self.assertEqual(con.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTE=") self.assertEqual( con.host, "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io") # test with base64 encoded string con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", api_key="ZWxhc3RpYzpjaGFuZ2VtZTI=", ) self.assertEqual(con.headers["authorization"], "ApiKey ZWxhc3RpYzpjaGFuZ2VtZTI=") self.assertEqual( con.host, "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io")
def test_http_cloud_id(self): con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==" ) assert con.use_ssl assert ( con.host == "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io") assert con.port is None assert con.hostname == "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io" assert con.http_compress con = AIOHttpConnection( cloud_id= "cluster:dXMtZWFzdC0xLmF3cy5mb3VuZC5pbyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5NyQ0ZmE4ODIxZTc1NjM0MDMyYmVkMWNmMjIxMTBlMmY5Ng==", port=9243, ) assert ( con.host == "https://4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io:9243" ) assert con.port == 9243 assert con.hostname == "4fa8821e75634032bed1cf22110e2f97.us-east-1.aws.found.io"
async def test_ssl_context(self): try: context = ssl.create_default_context() except AttributeError: # if create_default_context raises an AttributeError Exception # it means SSLContext is not available for that version of python # and we should skip this test. raise SkipTest( "Test test_ssl_context is skipped cause SSLContext is not available for this version of ptyhon" ) con = AIOHttpConnection(use_ssl=True, ssl_context=context) await con._create_aiohttp_session() self.assertTrue(con.use_ssl) self.assertEqual(con.session.connector._ssl, context)
def test_timeout_set(self): con = AIOHttpConnection(timeout=42) assert 42 == con.timeout
def test_doesnt_use_https_if_not_specified(self): con = AIOHttpConnection() assert not con.use_ssl
def test_opaque_id(self): con = AIOHttpConnection(opaque_id="app-1") assert con.headers["x-opaque-id"] == "app-1"
def test_no_warning_when_using_ssl_context(self): ctx = ssl.create_default_context() with warnings.catch_warnings(record=True) as w: AIOHttpConnection(ssl_context=ctx) self.assertEqual(0, len(w), str([x.message for x in w]))
def test_doesnt_use_https_if_not_specified(self): con = AIOHttpConnection() self.assertFalse(con.use_ssl)
def test_opaque_id(self): con = AIOHttpConnection(opaque_id="app-1") self.assertEqual(con.headers["x-opaque-id"], "app-1")
def test_timeout_set(self): con = AIOHttpConnection(timeout=42) self.assertEqual(42, con.timeout)
async def test_aiohttp_connection(self): # Defaults conn = AIOHttpConnection("httpbin.org", port=443, use_ssl=True) user_agent = conn._get_default_user_agent() status, data = await self.httpbin_anything(conn) assert status == 200 assert data["method"] == "GET" assert data["headers"] == { "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": user_agent, } # http_compress=False conn = AIOHttpConnection("httpbin.org", port=443, use_ssl=True, http_compress=False) status, data = await self.httpbin_anything(conn) assert status == 200 assert data["method"] == "GET" assert data["headers"] == { "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": user_agent, } # http_compress=True conn = AIOHttpConnection("httpbin.org", port=443, use_ssl=True, http_compress=True) status, data = await self.httpbin_anything(conn) assert status == 200 assert data["headers"] == { "Accept-Encoding": "gzip,deflate", "Content-Type": "application/json", "Host": "httpbin.org", "User-Agent": user_agent, } # Headers conn = AIOHttpConnection( "httpbin.org", port=443, use_ssl=True, http_compress=True, headers={"header1": "value1"}, ) status, data = await self.httpbin_anything(conn, headers={ "header2": "value2", "header1": "override!" }) assert status == 200 assert data["headers"] == { "Accept-Encoding": "gzip,deflate", "Content-Type": "application/json", "Host": "httpbin.org", "Header1": "override!", "Header2": "value2", "User-Agent": user_agent, }