-
-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: update request-id recipe to use contextvars #2404
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2727fef
refactor: request-id to use contextvars
EricGoulart 1a4b3fa
Merge branch 'master' into master
vytas7 b65571b
Merge branch 'master' into master
vytas7 c499cc2
adding test to test_recipes
EricGoulart 7510647
Merge remote-tracking branch 'origin/master'
EricGoulart 9c0f151
test(request_id): add tests for request id middleware context handling
EricGoulart 54b83b6
test(request_id): add tests for request id middleware context handling
EricGoulart 1220b21
Merge branch 'master' into master
vytas7 fd348be
Merge branch 'master' into master
vytas7 63d269e
minor changes in tests
EricGoulart 7c371e8
Merge branch 'master' into issue2382
vytas7 a3cba51
Merge branch 'master' into issue2382
vytas7 e3bb626
Merge branch 'master' into issue2382
vytas7 eec5e42
fix(recipes): fix request-id recipe tests
vytas7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
# context.py | ||
|
||
import threading | ||
import contextvars | ||
|
||
|
||
class _Context: | ||
def __init__(self): | ||
self._thread_local = threading.local() | ||
self._request_id_var = contextvars.ContextVar('request_id', default=None) | ||
|
||
@property | ||
def request_id(self): | ||
return getattr(self._thread_local, 'request_id', None) | ||
return self._request_id_var.get() | ||
|
||
@request_id.setter | ||
def request_id(self, value): | ||
self._thread_local.request_id = value | ||
self._request_id_var.set(value) | ||
|
||
|
||
ctx = _Context() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,3 +141,42 @@ 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, util): | ||
recipe = util.load_module('examples/recipes/request_id_middleware.py') | ||
|
||
app = falcon.App(middleware=[recipe.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} | ||
|
||
def test_request_id_isolated(self, app): | ||
client = falcon.testing.TestClient(app) | ||
request_id1 = client.simulate_get('/test').json['request_id'] | ||
request_id2 = client.simulate_get('/test').json['request_id'] | ||
|
||
assert request_id1 != request_id2 | ||
|
||
def test_request_id_persistence(self, app): | ||
client = falcon.testing.TestClient(app) | ||
|
||
response = client.simulate_get('/test') | ||
request_id1 = response.json['request_id'] | ||
|
||
response = client.simulate_get('/test') | ||
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'] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could also test that's the same as the value returned in the body |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could simply replace this with an Intersphinx link to the
contextvars
module, but this applies to the current version too, so not a requirement for merging this.