-
-
Notifications
You must be signed in to change notification settings - Fork 203
-
I'm trying to test request.files, and haven't figured out a way to get a fake file to show up in request.files when I'm doing a post() to test_client. I found this gist for Flask:
https://gist.github.com/DazWorrall/1779861
Is that what I should be doing in Quart as well? (I'm surprised it requires a custom request class but that's okay if that's the case)
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 11 replies
-
Did passing file data like client.post("url", data={"avatar": (BytesIO(avatar_bytes), "face.png")}) not work? https://werkzeug.palletsprojects.com/en/3.0.x/test/#request-body (I don't know if Quart is using Werkzeug's test client or an equivalent API yet, but this is how it would be done in Flask.)
I'm not entirely clear what that gist is trying to accomplish, but it was also written 13 years ago before the test client was more developed. The fact that it's still useful to people is a bit alarming.
Beta Was this translation helpful? Give feedback.
All reactions
-
I figured it out!
stream = b'------WebKitFormBoundaryr1D8WqBUjhPTDqlM\r\nContent-Disposition: form-data; name="file"; filename="a.txt"\r\nContent-Type: text/plain\r\n\r\n'
stream += b"foo;bar\n"
stream += b"\r\n------WebKitFormBoundaryr1D8WqBUjhPTDqlM--\r\n"
response = await auth_client.post(
"/upload",
headers={
"Authorization": "Bearer test",
"Content-type": "multipart/form-data; boundary=----WebKitFormBoundaryr1D8WqBUjhPTDqlM",
"Content-length": str(len(stream)),
},
files={"file": FileStorage(BytesIO(stream), filename="a.txt")},
)
Beta Was this translation helpful? Give feedback.
All reactions
-
I think there is something else at play here, as I've written a test in 1a65d32 and it works as expected. I would also expect Quart's test client to overwrite the Context-Type header when files is used. (I would expect the boundary addition to be required for data though)
Beta Was this translation helpful? Give feedback.
All reactions
-
I'll try mimicing your test exactly- I didn't know about FileStorage in my first attempts, so maybe if I use FileStorage, the other issues go away.
Beta Was this translation helpful? Give feedback.
All reactions
-
That worked! So I think the key thing is the usage of the FileStorage data structure. I recommend documenting on https://quart.palletsprojects.com/en/latest/how_to_guides/testing.html and I can send a PR to add it there if you're on board.
Beta Was this translation helpful? Give feedback.
All reactions
-
If somebody comes across this and also needs to combine form data with file upload:
multipartData = { "container": "0a5afda2-b745-4b04-9ba0-178ae8662a05", "name": f"File-{uuid4()}", } files = { "data": FileStorage( stream=io.BytesIO(b"examplebinary"), content_type="application/pdf", filename="test.pdf", ) } response = await testclient.post( "/documents", files=files, form=multipartData, )
Beta Was this translation helpful? Give feedback.