Repository navigation
fix: forward messages sent before the response start in the cache middleware - #93
Merged
Merged
Conversation
The previous commit stops the cache middleware making up a response start when a message arrives before the real one. It still treats http.response.debug as a message that carries content. That is wrong: debug carries test metadata (the template and its context), not content. When it arrives after the start, the middleware stops caching a response it could store. Forward debug as soon as it arrives and leave buffering alone. This order is safe. Starlette's TemplateResponse already sends debug before the start, and TestClient accepts debug at any point. Tests: - Add unit tests that need no Redis and use the real Jinja2Templates under TestClient. They fail on the code from before redis#92 was fixed, and they also fail if Starlette changes the order it sends messages. - Take debug out of the group of messages that replace the body. Those tests sent a start, then debug, and no body, which no real server or response does. - Check scope["extensions"] before the fake response sends debug, as Starlette does. - Cover debug before and after the start, on uncached and cached routes. - Add jinja2 to the test dependency group. Before this, only the docs tools installed it, and the CI test sessions don't install those. Docs: - Remove debug from the list of responses the middleware cannot buffer. - Tell users that a cache hit renders no template, so TestClient reports no response.template or response.context for it. Refs redis#92
Collaborator
|
Hey @davidpoblador thanks for this contribution! I extended your solution a bit, but it is mostly based on your work. Check out the commit message for details. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #93 +/- ##
==========================================
- Coverage 97.45% 97.38% -0.07%
==========================================
Files 15 15
Lines 2634 2642 +8
==========================================
+ Hits 2567 2573 +6
- Misses 67 69 +2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #92.
Starlette's
TemplateResponsesendshttp.response.debugbeforehttp.response.startwhen the server supports that extension, andTestClientdoes. The cache middleware took any non-body message as a signal to flush the buffered start, even when no start had arrived yet. As a result:TestClientfailed with "Received multiple http.response.start messages"Now the middleware forwards anything that arrives before the start as-is, and caching works as usual. Messages after the start are handled the same as before.
I added two tests next to the existing extension message tests and a short note in the architecture guide.