keredson/boddle

test POST request with content type of multipart/form-data and a file in body

signalw opened this issue · 1 comments

I have a route like this:

@app.post('/submit')
def submit():
    img = bottle.request.files['img']
    img.save('/srv/images')

It's for getting and saving an image file from user. To test that, with boddle, I tried this:

imgFile = FileUpload(object(), 'test', 'test')
fd = FormsDict()
fd['img'] = imgFile
with boddle(method='post', path='/submit', files=fd):
  submit()

When I call submit() inside boddle, I've got a KeyError: 'img' exception. Then I realized bottle.request.files is not set because it just can't be overwritten.

BTW, while examining this, I opened #7 with more details about how it can't be overwritten.

So how are we supposed to test that? Should we add a new parameter for boddle and handle that properly? I think we can take in something like files=[(fileobj, fieldname, filename, headers)] and then construct a FormsDict and put it in self.environ. But that would make it look a bit complicated though. I mean right now boddle is very straightforward and I'm not sure if it's a good idea to bring in these things.

Any updates on this issue? Thanks