14
14
TransportServerError ,
15
15
)
16
16
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
+ )
18
22
19
23
query1_str = """
20
24
query getContinents {
@@ -1285,7 +1289,10 @@ async def handler(request):
1285
1289
1286
1290
@pytest .mark .asyncio
1287
1291
@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
+ ):
1289
1296
from aiohttp import web
1290
1297
from gql .transport .aiohttp import AIOHTTPTransport
1291
1298
@@ -1300,8 +1307,20 @@ async def handler(request):
1300
1307
1301
1308
assert str (url ).startswith ("https://" )
1302
1309
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
+
1303
1319
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 ,
1305
1324
)
1306
1325
1307
1326
async with Client (transport = transport ) as session :
@@ -1318,6 +1337,65 @@ async def handler(request):
1318
1337
assert africa ["code" ] == "AF"
1319
1338
1320
1339
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
+
1321
1399
@pytest .mark .asyncio
1322
1400
async def test_aiohttp_error_fetching_schema (event_loop , aiohttp_server ):
1323
1401
from aiohttp import web
0 commit comments