Skip to content

Commit c4f2dc2

Browse files
authored
Adding docs and test about cookies (#202)
1 parent 24b037d commit c4f2dc2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

docs/transports/aiohttp.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,39 @@ This transport uses the `aiohttp`_ library and allows you to send GraphQL querie
1212

1313
.. literalinclude:: ../code_examples/aiohttp_async.py
1414

15+
Authentication
16+
--------------
17+
18+
There are multiple ways to authenticate depending on the server configuration.
19+
20+
1. Using HTTP Headers
21+
22+
.. code-block:: python
23+
24+
transport = AIOHTTPTransport(
25+
url='https://SERVER_URL:SERVER_PORT/graphql',
26+
headers={'Authorization': 'token'}
27+
)
28+
29+
2. Using HTTP Cookies
30+
31+
You can manually set the cookies which will be sent with each connection:
32+
33+
.. code-block:: python
34+
35+
transport = AIOHTTPTransport(url=url, cookies={"cookie1": "val1"})
36+
37+
Or you can use a cookie jar to save cookies set from the backend and reuse them later.
38+
39+
In some cases, the server will set some connection cookies after a successful login mutation
40+
and you can save these cookies in a cookie jar to reuse them in a following connection
41+
(See `issue 197`_):
42+
43+
.. code-block:: python
44+
45+
jar = aiohttp.CookieJar()
46+
transport = AIOHTTPTransport(url=url, client_session_args={'cookie_jar': jar})
47+
48+
1549
.. _aiohttp: https://docs.aiohttp.org
50+
.. _issue 197: https://github.com/graphql-python/gql/issues/197

tests/test_aiohttp.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,39 @@ async def handler(request):
6969
assert africa["code"] == "AF"
7070

7171

72+
@pytest.mark.asyncio
73+
async def test_aiohttp_cookies(event_loop, aiohttp_server):
74+
from aiohttp import web
75+
from gql.transport.aiohttp import AIOHTTPTransport
76+
77+
async def handler(request):
78+
assert "COOKIE" in request.headers
79+
assert "cookie1=val1" == request.headers["COOKIE"]
80+
81+
return web.Response(text=query1_server_answer, content_type="application/json")
82+
83+
app = web.Application()
84+
app.router.add_route("POST", "/", handler)
85+
server = await aiohttp_server(app)
86+
87+
url = server.make_url("/")
88+
89+
sample_transport = AIOHTTPTransport(url=url, cookies={"cookie1": "val1"})
90+
91+
async with Client(transport=sample_transport,) as session:
92+
93+
query = gql(query1_str)
94+
95+
# Execute query asynchronously
96+
result = await session.execute(query)
97+
98+
continents = result["continents"]
99+
100+
africa = continents[0]
101+
102+
assert africa["code"] == "AF"
103+
104+
72105
@pytest.mark.asyncio
73106
async def test_aiohttp_error_code_500(event_loop, aiohttp_server):
74107
from aiohttp import web

tests/test_requests.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,43 @@ def test_code():
6464
await run_sync_test(event_loop, server, test_code)
6565

6666

67+
@pytest.mark.aiohttp
68+
@pytest.mark.asyncio
69+
async def test_requests_cookies(event_loop, aiohttp_server, run_sync_test):
70+
from aiohttp import web
71+
from gql.transport.requests import RequestsHTTPTransport
72+
73+
async def handler(request):
74+
assert "COOKIE" in request.headers
75+
assert "cookie1=val1" == request.headers["COOKIE"]
76+
77+
return web.Response(text=query1_server_answer, content_type="application/json")
78+
79+
app = web.Application()
80+
app.router.add_route("POST", "/", handler)
81+
server = await aiohttp_server(app)
82+
83+
url = server.make_url("/")
84+
85+
def test_code():
86+
sample_transport = RequestsHTTPTransport(url=url, cookies={"cookie1": "val1"})
87+
88+
with Client(transport=sample_transport,) as session:
89+
90+
query = gql(query1_str)
91+
92+
# Execute query synchronously
93+
result = session.execute(query)
94+
95+
continents = result["continents"]
96+
97+
africa = continents[0]
98+
99+
assert africa["code"] == "AF"
100+
101+
await run_sync_test(event_loop, server, test_code)
102+
103+
67104
@pytest.mark.aiohttp
68105
@pytest.mark.asyncio
69106
async def test_requests_error_code_500(event_loop, aiohttp_server, run_sync_test):

0 commit comments

Comments
 (0)