Supporting Response(content=..., text=..., html=..., json=...)
#1227
Labels
enhancement
New feature or request
Milestone
We’d like to support instantiating
Response
instances directly.One really good example of why we might want to do that is for unit testing custom auth classes - #1217 (comment)
Currently
Response
instances are only ever created by the client itself, and...request=...
parameter.status_code
parameter.The first things we should do here are:
request
parameter become optional. Have a.request
property that returns aRequest
instance or raises a RuntimeError if the instance is not set. Have a property setter for the.request
property.Once we’ve done those things we should add support for setting the content of a response, using
content=..., text=..., html=..., json=...
parameters, which mirror the request parameters ofdata=..., files=..., json=...
, but differ because HTTP responses use different content types to HTTP requests.The
encode(...)
method incontent_streams.py
would becomeencode_request_body(data=..., files=..., json=..., boundary=...)
, and would be mirrored by aencode_response_body(content=..., text=..., html=..., json=...)
method, which would use the following content streams:content
- Support either bytes or byte iterator/aiterators. SettingContent-Length
orTransfer-Encoding: chunked
text
- SetContent-Type: text/plain; charset=“utf-8”
, and aContent-Length
header.html
- SetContent-Type: text/html; charset=“utf-8”
and aContent-Length
header.json
- SetContent-Type: application/json
and aContent-Length
header.We can tie in an
encoding=...
argument too, but let’s talk that through as a follow up.What this would then all give us is the ability to create responses like so...
Somewhat refs #1091
The text was updated successfully, but these errors were encountered: