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
Submitting a form with a blank file input differs to real browser behaviour #250
Comments
The MechanicalSoup behavior makes more sense to me, but since the goal is to mimick browsers, we should definitely change that. Should be straightforward to fix: https://github.com/MechanicalSoup/MechanicalSoup/blob/master/mechanicalsoup/browser.py#L179-L180 Do you want to try a patch for that? |
Do I want to try a patch that you've written? Sure. |
I meant "try writing a patch". The code itself should be straightforward, I pointed you to the place where we just skip ( |
So I made an attempt to fix it - and writing a test was the more complicated part. In particular, it's harder to process multiform data in a sensible way. As a suggestion (after a quick look here), I'm thinking that it would be best to make it easier to process multipart data in one of two ways:
Either approach would require adding new dependencies - so I'm interested in what way you think is best to proceed. |
I don't think you need any new dependency. We already have tests to check the content of the file, using httpbin. See eg. https://github.com/MechanicalSoup/MechanicalSoup/blob/master/tests/test_browser.py#L84-L109 The new test should be very similar, but without the (Writing this, I realize that we don't actually check that the file is found, which should be fixed in #251) |
Discussion on the flask issue here: pallets/flask#3130 |
When used with a browser, the support for uploading photos as part of bulk registration resulted in errors when a ZIP file was not uploaded. This did not show up in testing because MechanicalSoup versions 0.11 and earlier do not handle missing file uploads the same way as a browser does <MechanicalSoup/MechanicalSoup#250>. Similar issues apply when no CSV file is uploaded (should produce an error, did not with a browser). This change fixes the code handling uploads to handle empty filenames, as used by browsers when no file is uploaded, the same as the form field for the upload being missing, with consequent changes to handle the case of empty zip_ref following a missing ZIP upload. Tests are added for the browser-like case of missing CSV uploads, though those are only properly effective with newer MechanicalSoup. This passes testing with both older and newer MechanicalSoup.
First of all, I'm really enjoying using MechanicalSoup after moving away from mechanize, so thanks for all of the work on it!
So I've encountered what I think is a bug - or certainly a deviation from the behaviour of regular browser behaviour.
If you have a form which contains a file input, and you submit it without selecting a file:
To recreate it, here's a very simple CherryPy app that will show the behaviour.
It prints the submitted repsonse to the log, as well as in the content of the response.
Here's how I'm testing what MechanicalSoup does - I set the "name" input element, but leave the "somefile" one blank.
And here's output from the app's logs - I tested with Chromium, then MechanicalSoup, and then Firefox - as you can see, the "file" input is processed as a
_cpreqbody.Part
object when submitted via a browser (even though no file was selected), while it isNone
for MechanicalSoup's interaction.The text was updated successfully, but these errors were encountered: