-
Notifications
You must be signed in to change notification settings - Fork 108
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
Touch up Content-Disposition header #3439
Touch up Content-Disposition header #3439
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, although you may want to reconsider the placement of the assertions you added to the tests.
def mock_send_file(path_or_file, *args, **kwargs): | ||
nonlocal file_sent | ||
file_sent = path_or_file | ||
assert str(path_or_file) == "/dataset/1-default/default.csv" | ||
assert kwargs["as_attachment"] is False | ||
assert kwargs["download_name"] == "default.csv" | ||
return {"status": "OK"} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Be aware that, if any of these assert
's fails, it will raise an exception which will be caught by Werkzeug, not PyTest. I expect that that behavior will be noted by the assertion at line 117, but you might want to keep the file_sent
mechanism (or replace it with something more comprehensive) as a belt-and-suspenders approach (i.e., the assertions are great, but they should probably be performed in the test function itself rather than the mock).
Ditto in test_get_result_tarball()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That raises several interesting points.
First off, we are directly calling Flask's send_file
, not Werkzeug, whereas the test is mocking werkzeug.utils
... we're depending on an implicit assumed behavior of Flask's send_file
that I'm not sure is formally guaranteed anywhere.
Be that as it may, currently Flask's send_file
does nothing but directly call Werkzeug's, and there is no exception handler involved other than our own in ApiBase.__dispatch
.
On the other hand, although the assert message will show in the traceback the primary visible symptom of a failure will still be a server internal error, which isn't ideal, and keeping the assert
outside of the mock would result in a much better diagnostic in case of failure. (Sigh.)
PBENCH-1169 Help clients with the output of `GET /datasets/<id>/inventory/` by "tuning" the `Content-Disposition` header automatically generated by Werkzeug's `send_file` API. I started thinking I would construct the header and looking into how to add headers to the `Response` object constructed by `send_file`. It actually does a good job of guessing the filename, but I decided to help it by providing the name we want, and I'm designating the raw tarfile as an `attachment` that we can't reasonably expect a client (especially the UI) to do anything with "inline" (the default).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good 👍
PBENCH-1169
Help clients with the output of
GET /datasets/<id>/inventory/
by "tuning" theContent-Disposition
header automatically generated by Werkzeug'ssend_file
API.I started thinking I would construct the header and looking into how to add headers to the
Response
object constructed bysend_file
. It actually does a good job of guessing the filename, but I decided to help it by providing the name we want, and I'm designating the raw tarfile as anattachment
that we can't reasonably expect a client (especially the UI) to do anything with "inline" (the default).