Search code examples
pythonflaskwerkzeughttp-method

How can I test multi-part uploads with FlaskClient (for unit testing)


The docs don't talk much about these, just basic params with PUT/POST/GET/DELETE, but I have a multipart upload that accompanies PUT, how can I do test it? Thanks!


Solution

  • To answer my own question:

    builder = EnvironBuilder(method='POST', data={'foo': 'this is some text',
    ...      'file': (StringIO('my file contents'), 'test.txt')})
    

    Since TestClient is a wrapper around EnvironBuilder, just extend the data dict with a file key, value, and then wrap the file object as a StringIO.