Testclient does not treat readable data correctly #1561
-
I think this is a potential issue with the TestClient When posting streaming data, a client call like with open(archive_path, 'rb') as archive_file:
requests.put(url, headers={'Content-Type': 'application/gzip'}, data=archive_file) can be processed by a server route with a helper method async def _write_request_stream(request: Request, target: BufferedWriter):
async for data in request.stream():
target.write(data) However this doesn't work with the TestClient, i needed the following workaround on the server from shutil import copyfileobj as copybuffers
async def _write_request_stream(request: Request, target: BufferedWriter):
async for data in request.stream():
if isinstance(data, BufferedReader):
copybuffers(data, target)
else:
target.write(data) IMO the test client should treat body arguments with a There seems to be some handling for a request body that is a generator, See https://github.com/encode/starlette/blob/87641dc8e7e72755f1b8f39dfc9b3bfbdf41c923/starlette/testclient.py#L212 , Shall I create an issue , and maybe contribute a PR? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
We changed the HTTP client from requests to |
Beta Was this translation helpful? Give feedback.
-
I'll preemptively close this as outdated, but please let me know. |
Beta Was this translation helpful? Give feedback.
I'll preemptively close this as outdated, but please let me know.