Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to test sending request.files? #326

Unanswered
pamelafox asked this question in Q&A
Discussion options

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)

You must be logged in to vote

Replies: 1 comment 11 replies

Comment options

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.

You must be logged in to vote
11 replies
Comment options

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")},
 )
Comment options

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)

Comment options

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.

Comment options

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.

Comment options

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,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /