Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multipart encoding in Data pattern #251

Closed
ziima opened this issue Mar 4, 2024 · 3 comments · Fixed by #252
Closed

Support multipart encoding in Data pattern #251

ziima opened this issue Mar 4, 2024 · 3 comments · Fixed by #252
Labels
bug Something isn't working

Comments

@ziima
Copy link

ziima commented Mar 4, 2024

Data pattern only handles urlencoded data, not multipart encoded. This is troublesome because by addition of files to the request, the encoding changes and Data pattern fails to match the request.

MWE

import httpx
import respx

with respx.mock() as rsps:
    rsps.post('http://example.org/', data={'answer': '42'})
    httpx.post('http://example.org', data={'answer': '42'})  # OK

with respx.mock() as rsps:
    rsps.post('http://example.org/', data={'answer': '42'})
    httpx.post('http://example.org', data={'answer': '42'}, files={'file': b'content'})
    # >>> respx.models.AllMockedAssertionError: RESPX: <Request('POST', 'http://example.org/')> not mocked!

Related to #115

@lundberg
Copy link
Owner

lundberg commented Mar 14, 2024

Definitely related to a files pattern. I haven't looked at how data changes, but it would be interesting to see if it would be possible to reconstruct without implementing the same logic as httpx has 🤔.

If we can solve this one, both issues would probably be solved, which is great.

@lundberg
Copy link
Owner

@ziima Thanks for the idea/suggestion in #115 to use the email module for extracting the encoded form data.

The new respx.utils.decode_data will need to be improved to add support for matching files, but should be a lot easier now that the surrounding decoding is in place.

Please try this PR on your code, if possible, to rule out any new bugs 🙏 🤞

@lundberg
Copy link
Owner

One potential bug is if a regular form text field together with a file without filename and content type text/plain is provided. Not sure, but my gut feeling says that this could get mixed up. If so, I'm not sure how to distinguish them 🤔

@lundberg lundberg added the bug Something isn't working label Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants