Skip to content
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

Merged
merged 2 commits into from
Jun 1, 2023

Conversation

dbutenhof
Copy link
Member

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).

@dbutenhof dbutenhof added Server API Of and relating to application programming interfaces to services and functions labels May 26, 2023
@dbutenhof dbutenhof self-assigned this May 26, 2023
webbnh
webbnh previously approved these changes May 30, 2023
Copy link
Member

@webbnh webbnh left a 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.

Comment on lines 106 to 111
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"}
Copy link
Member

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().

Copy link
Member Author

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.)

dbutenhof added 2 commits May 30, 2023 13:00
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).
Copy link
Member

@webbnh webbnh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@siddardh-ra siddardh-ra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@dbutenhof dbutenhof merged commit f5a6b12 into distributed-system-analysis:main Jun 1, 2023
@dbutenhof dbutenhof deleted the disposition branch June 1, 2023 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Of and relating to application programming interfaces to services and functions Server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants