Skip to content

Commit

Permalink
test(request_id): add tests for request id middleware context handling
Browse files Browse the repository at this point in the history
Add tests to verify that request_id is unique per request and correctly set in response headers. Tests include checks for isolation in async calls and persistence in synchronous requests.

Related to issue falconry#2260
  • Loading branch information
EricGoulart committed Oct 29, 2024
1 parent 7510647 commit 9c0f151
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
8 changes: 4 additions & 4 deletions examples/recipes/request_id_middleware.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# middleware.py

from uuid import uuid4

from context import ctx

from .request_id_context import ctx

class RequestIDMiddleware:
def process_request(self, req, resp):
ctx.request_id = str(uuid4())
request_id = str(uuid4())
ctx.request_id = request_id
req.context.request_id = request_id

# It may also be helpful to include the ID in the response
def process_response(self, req, resp, resource, req_succeeded):
Expand Down
26 changes: 16 additions & 10 deletions tests/test_recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import falcon
import falcon.testing

from examples.recipes import request_id_context
import asyncio

from examples.recipes.request_id_middleware import RequestIDMiddleware

class TestMultipartMixed:
"""Test parsing example from the now-obsolete RFC 1867:
Expand Down Expand Up @@ -143,27 +143,26 @@ def test_raw_path(self, asgi, app_kind, util):
)
assert result2.status_code == 200
assert result2.json == {'cached': True}



class TestRequestIDContext:
@pytest.fixture
def app(self):
app = falcon.App(middleware=[request_id_context.RequestIDMiddleware()])
app = falcon.App(middleware=[RequestIDMiddleware()])
app.add_route('/test', self.RequestIDResource())
return app

class RequestIDResource:
def on_get(self, req, resp):
resp.media = {'request_id': req.context.request_id}

@pytest.mark.asyncio
async def test_request_id_isolated_in_async(self, app):
def test_request_id_isolated_in_async(self, app):
async def make_request():
client = falcon.testing.TestClient(app)
response = await client.simulate_get('/test')
response = client.simulate_get('/test')
return response.json['request_id']

request_id1, request_id2 = await asyncio.gather(make_request(), make_request())
loop = asyncio.get_event_loop()
request_id1, request_id2 = loop.run_until_complete(asyncio.gather(make_request(), make_request()))
assert request_id1 != request_id2

def test_request_id_persistence(self, app):
Expand All @@ -176,3 +175,10 @@ def test_request_id_persistence(self, app):
request_id2 = response.json['request_id']

assert request_id1 != request_id2

def test_request_id_in_response_header(self, app):
client = falcon.testing.TestClient(app)

response = client.simulate_get('/test')
assert 'X-Request-ID' in response.headers
assert response.headers['X-Request-ID'] == response.json['request_id']

0 comments on commit 9c0f151

Please sign in to comment.