Skip to content

Commit 71c5c1a

Browse files
authored
Merge branch 'master' into chore_update_graphql_core_to_3_3_0
2 parents e64605c + 5e47f5f commit 71c5c1a

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed

gql/transport/aiohttp.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ async def execute(
295295

296296
async with self.session.post(self.url, ssl=self.ssl, **post_args) as resp:
297297

298+
# Saving latest response headers in the transport
299+
self.response_headers = resp.headers
300+
298301
async def raise_response_error(resp: aiohttp.ClientResponse, reason: str):
299302
# We raise a TransportServerError if the status code is 400 or higher
300303
# We raise a TransportProtocolError in the other cases
@@ -325,9 +328,6 @@ async def raise_response_error(resp: aiohttp.ClientResponse, reason: str):
325328
if "errors" not in result and "data" not in result:
326329
await raise_response_error(resp, 'No "data" or "errors" keys in answer')
327330

328-
# Saving latest response headers in the transport
329-
self.response_headers = resp.headers
330-
331331
return ExecutionResult(
332332
errors=result.get("errors"),
333333
data=result.get("data"),

tests/test_aiohttp.py

+47
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,53 @@ async def handler(request):
172172
assert "401, message='Unauthorized'" in str(exc_info.value)
173173

174174

175+
@pytest.mark.asyncio
176+
async def test_aiohttp_error_code_429(event_loop, aiohttp_server):
177+
from aiohttp import web
178+
from gql.transport.aiohttp import AIOHTTPTransport
179+
180+
async def handler(request):
181+
# Will generate http error code 429
182+
return web.Response(
183+
text="""
184+
<html>
185+
<head>
186+
<title>Too Many Requests</title>
187+
</head>
188+
<body>
189+
<h1>Too Many Requests</h1>
190+
<p>I only allow 50 requests per hour to this Web site per
191+
logged in user. Try again soon.</p>
192+
</body>
193+
</html>""",
194+
content_type="text/html",
195+
status=429,
196+
headers={"Retry-After": "3600"},
197+
)
198+
199+
app = web.Application()
200+
app.router.add_route("POST", "/", handler)
201+
server = await aiohttp_server(app)
202+
203+
url = server.make_url("/")
204+
205+
transport = AIOHTTPTransport(url=url)
206+
207+
async with Client(transport=transport) as session:
208+
209+
query = gql(query1_str)
210+
211+
with pytest.raises(TransportServerError) as exc_info:
212+
await session.execute(query)
213+
214+
assert "429, message='Too Many Requests'" in str(exc_info.value)
215+
216+
# Checking response headers are saved in the transport
217+
assert hasattr(transport, "response_headers")
218+
assert isinstance(transport.response_headers, Mapping)
219+
assert transport.response_headers["Retry-After"] == "3600"
220+
221+
175222
@pytest.mark.asyncio
176223
async def test_aiohttp_error_code_500(event_loop, aiohttp_server):
177224
from aiohttp import web

tests/test_requests.py

+49
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,55 @@ def test_code():
148148
await run_sync_test(event_loop, server, test_code)
149149

150150

151+
@pytest.mark.aiohttp
152+
@pytest.mark.asyncio
153+
async def test_requests_error_code_429(event_loop, aiohttp_server, run_sync_test):
154+
from aiohttp import web
155+
from gql.transport.requests import RequestsHTTPTransport
156+
157+
async def handler(request):
158+
# Will generate http error code 429
159+
return web.Response(
160+
text="""
161+
<html>
162+
<head>
163+
<title>Too Many Requests</title>
164+
</head>
165+
<body>
166+
<h1>Too Many Requests</h1>
167+
<p>I only allow 50 requests per hour to this Web site per
168+
logged in user. Try again soon.</p>
169+
</body>
170+
</html>""",
171+
content_type="text/html",
172+
status=429,
173+
headers={"Retry-After": "3600"},
174+
)
175+
176+
app = web.Application()
177+
app.router.add_route("POST", "/", handler)
178+
server = await aiohttp_server(app)
179+
180+
url = server.make_url("/")
181+
182+
def test_code():
183+
transport = RequestsHTTPTransport(url=url)
184+
185+
with Client(transport=transport) as session:
186+
187+
query = gql(query1_str)
188+
189+
with pytest.raises(TransportServerError) as exc_info:
190+
session.execute(query)
191+
192+
assert "429, message='Too Many Requests'" in str(exc_info.value)
193+
194+
# Checking response headers are saved in the transport
195+
assert hasattr(transport, "response_headers")
196+
assert isinstance(transport.response_headers, Mapping)
197+
assert transport.response_headers["Retry-After"] == "3600"
198+
199+
151200
@pytest.mark.aiohttp
152201
@pytest.mark.asyncio
153202
async def test_requests_error_code_500(event_loop, aiohttp_server, run_sync_test):

0 commit comments

Comments
 (0)