1414 TransportServerError ,
1515)
1616
17- from .conftest import TemporaryFile , strip_braces_spaces
17+ from .conftest import (
18+ TemporaryFile ,
19+ get_localhost_ssl_context_client ,
20+ strip_braces_spaces ,
21+ )
1822
1923query1_str = """
2024 query getContinents {
@@ -1285,7 +1289,10 @@ async def handler(request):
12851289
12861290@pytest .mark .asyncio
12871291@pytest .mark .parametrize ("ssl_close_timeout" , [0 , 10 ])
1288- async def test_aiohttp_query_https (event_loop , ssl_aiohttp_server , ssl_close_timeout ):
1292+ @pytest .mark .parametrize ("verify_https" , ["disabled" , "cert_provided" ])
1293+ async def test_aiohttp_query_https (
1294+ event_loop , ssl_aiohttp_server , ssl_close_timeout , verify_https
1295+ ):
12891296 from aiohttp import web
12901297 from gql .transport .aiohttp import AIOHTTPTransport
12911298
@@ -1300,8 +1307,20 @@ async def handler(request):
13001307
13011308 assert str (url ).startswith ("https://" )
13021309
1310+ extra_args = {}
1311+
1312+ if verify_https == "cert_provided" :
1313+ _ , ssl_context = get_localhost_ssl_context_client ()
1314+
1315+ extra_args ["ssl" ] = ssl_context
1316+ elif verify_https == "disabled" :
1317+ extra_args ["ssl" ] = False
1318+
13031319 transport = AIOHTTPTransport (
1304- url = url , timeout = 10 , ssl_close_timeout = ssl_close_timeout
1320+ url = url ,
1321+ timeout = 10 ,
1322+ ssl_close_timeout = ssl_close_timeout ,
1323+ ** extra_args ,
13051324 )
13061325
13071326 async with Client (transport = transport ) as session :
@@ -1318,6 +1337,65 @@ async def handler(request):
13181337 assert africa ["code" ] == "AF"
13191338
13201339
1340+ @pytest .mark .skip (reason = "We will change the default to fix this in a future version" )
1341+ @pytest .mark .asyncio
1342+ async def test_aiohttp_query_https_self_cert_fail (event_loop , ssl_aiohttp_server ):
1343+ """By default, we should verify the ssl certificate"""
1344+ from aiohttp .client_exceptions import ClientConnectorCertificateError
1345+ from aiohttp import web
1346+ from gql .transport .aiohttp import AIOHTTPTransport
1347+
1348+ async def handler (request ):
1349+ return web .Response (text = query1_server_answer , content_type = "application/json" )
1350+
1351+ app = web .Application ()
1352+ app .router .add_route ("POST" , "/" , handler )
1353+ server = await ssl_aiohttp_server (app )
1354+
1355+ url = server .make_url ("/" )
1356+
1357+ assert str (url ).startswith ("https://" )
1358+
1359+ transport = AIOHTTPTransport (url = url , timeout = 10 )
1360+
1361+ with pytest .raises (ClientConnectorCertificateError ) as exc_info :
1362+ async with Client (transport = transport ) as session :
1363+ query = gql (query1_str )
1364+
1365+ # Execute query asynchronously
1366+ await session .execute (query )
1367+
1368+ expected_error = "certificate verify failed: self-signed certificate"
1369+
1370+ assert expected_error in str (exc_info .value )
1371+ assert transport .session is None
1372+
1373+
1374+ @pytest .mark .asyncio
1375+ async def test_aiohttp_query_https_self_cert_warn (event_loop , ssl_aiohttp_server ):
1376+ from aiohttp import web
1377+ from gql .transport .aiohttp import AIOHTTPTransport
1378+
1379+ async def handler (request ):
1380+ return web .Response (text = query1_server_answer , content_type = "application/json" )
1381+
1382+ app = web .Application ()
1383+ app .router .add_route ("POST" , "/" , handler )
1384+ server = await ssl_aiohttp_server (app )
1385+
1386+ url = server .make_url ("/" )
1387+
1388+ assert str (url ).startswith ("https://" )
1389+
1390+ expected_warning = (
1391+ "WARNING: By default, AIOHTTPTransport does not verify ssl certificates."
1392+ " This will be fixed in the next major version."
1393+ )
1394+
1395+ with pytest .warns (Warning , match = expected_warning ):
1396+ AIOHTTPTransport (url = url , timeout = 10 )
1397+
1398+
13211399@pytest .mark .asyncio
13221400async def test_aiohttp_error_fetching_schema (event_loop , aiohttp_server ):
13231401 from aiohttp import web
0 commit comments