Pylons / webtest

Wraps any WSGI application and makes it easy to send test requests to that application, without starting up an HTTP server.
https://docs.pylonsproject.org/projects/webtest/en/latest/
Other
336 stars 110 forks source link

Multiple file input support #240

Closed azmeuk closed 2 years ago

azmeuk commented 2 years ago

It seems that webtest cannot handle multiple file fields:

Here files is a <input type="file" name="files" multiple /> input.

from webtest import Upload

def test_foobar(app):
    res = app.get("/my-form")
    res.form["files"] = [Upload("foo.txt", b"foo"), Upload("bar.txt", b"bar")]
    res = res.form.submit()

Here is the last frame of the generated stack trace:

self = <webtest.app.TestApp object at 0x7f7b3eceb3a0>, file_info = [b'files', <Upload "foo.png">, <Upload "bar.png">]

    def _get_file_info(self, file_info):
        if len(file_info) == 2:
            # It only has a filename
            filename = file_info[1]
            if self.relative_to:
                filename = os.path.join(self.relative_to, filename)
            f = open(filename, 'rb')
            content = f.read()
            f.close()
            return (file_info[0], filename, content, None)
        elif 3 <= len(file_info) <= 4:
            content = file_info[2]
            if not isinstance(content, bytes):
>               raise ValueError('File content must be %s not %s'
                                 % (bytes, type(content)))
E               ValueError: File content must be <class 'bytes'> not <class 'webtest.forms.Upload'>

I suggest making webtest handle multiple file fields.