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

Add a files pattern? #115

Closed
lundberg opened this issue Nov 23, 2020 · 1 comment · Fixed by #253
Closed

Add a files pattern? #115

lundberg opened this issue Nov 23, 2020 · 1 comment · Fixed by #253
Labels
enhancement New feature or request

Comments

@lundberg
Copy link
Owner

As mentioned in #87, it might be of interest to allow matching a request on files being sent.

  • What do we want to match on?
  • Is it even possible once internal HTTPX request is built.
route = respx.post("https://example.org/", files=?)

Since uploading files with HTTPX supports multiple arg types, it might only be possible to match on file names, if given. Reading a file object's content could affect HTTPX internals, if not handled properly, like reseting seek point etc.

Needs further investigation, multiple files.

@ziima
Copy link

ziima commented Mar 4, 2024

I used this utility function to extract files from the httpx.Request in tests:

import email
from httpx import Request
from typing import Any, Dict

def _get_data(request: Request) -> Dict[str, Any]:
    raw_data = (
        b'MIME-Version: 1.0\nContent-Type: '
        + request.headers['Content-Type'].encode(request.headers.encoding)
        + b'\n\n' + request.read())
    msg = email.message_from_bytes(raw_data)
    data = {}
    for payload in msg.get_payload():
        key = payload.get_param('name', header='Content-Disposition')
        decode = payload.get_content_maintype().startswith('application')
        data[key] = payload.get_payload(decode=decode)
    return data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants