Skip to content

Comments

copy over some tests from alternative implementation#1694

Closed
adriangb wants to merge 3 commits intoKludex:fm/http-middlewarefrom
adriangb:copy-tests
Closed

copy over some tests from alternative implementation#1694
adriangb wants to merge 3 commits intoKludex:fm/http-middlewarefrom
adriangb:copy-tests

Conversation

@adriangb
Copy link
Contributor

Copying over tests from my implementation.

This drops the contextvar tests which in my opinion are unnecessary with this implementation.
It also adds types to the test file.

There's two main test cases being added:

  1. yielding after an early response (I added the fix for this, it's pretty straightforward)
  2. Modifying Response.media_type. In my implementation I was detecting this and overwriting the header, but I guess we could subclass Response and raise an exception if someone attempts to assign it?

Comment on lines -102 to -103
if not response_started:
raise RuntimeError("No response returned.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this here, we can just let the server handle it which is what would happen if we didn't install this middleware. I think we only do it in BaseHTTPMiddleware out of necessity / implementation detail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adriangb adriangb requested a review from florimondmanca June 15, 2022 05:31
@florimondmanca
Copy link
Contributor

@adriangb I think there are some changes here that you also mentioned in comments in #1691, so I'll first review and make some changes there later, then come back to reviewing this, if that's OK.

) -> None:
async def dispatch(request: HTTPConnection) -> AsyncGenerator[None, Response]:
resp = yield
resp.media_type = "text/csv"
Copy link
Contributor

@florimondmanca florimondmanca Jun 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be strange usage without modifying the response body, wouldn't it? Is there a situation where we'd actually do this w/o having to modify the response body and Content-Type header, which would require pure ASGI middleware (or the body modification API you thought about as a possible future improvement)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not sure what the use case would be, but you are able to set the attribute, so maybe we should error in this situation instead by overriding Response.__setattr__?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to be that defensive, to be honest. If we're clear enough in the documentation (which ought to be written before we consider merging this anyway, right?) that this is a "simplified response interface, allowing to inspect the status code and headers, or tweak headers", then maybe that's enough.

Though, now that I think of it — what if users do want to inspect response.body in the "on response" part of the dispatch flow? Right now, it's empty, and actually makes no sense because this is called before we start processing any chunk of the body. Hmm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I don't think there's any behavior that's right or wrong here, it's just about what will be least confusing.

@adriangb
Copy link
Contributor Author

I think you copied most of this over, I'll close this, thanks @florimondmanca

@adriangb adriangb closed this Jun 15, 2022
@adriangb adriangb deleted the copy-tests branch June 15, 2022 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants