Skip to content

Commit

Permalink
Fixed bug with download of BLOB null, refs #1050
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Oct 29, 2020
1 parent cefd058 commit 89519f9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion datasette/views/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ async def get(self, request, db_name, table, pk_path, column):
"Content-Disposition": 'attachment; filename="{}"'.format(filename),
}
return Response(
body=rows[0][column],
body=rows[0][column] or b"",
status=200,
headers=headers,
content_type="application/binary",
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ def generate_sortable_rows(num):
TABLE_PARAMETERIZED_SQL = [
("insert into binary_data (data) values (?);", [b"\x15\x1c\x02\xc7\xad\x05\xfe"]),
("insert into binary_data (data) values (?);", [b"\x15\x1c\x03\xc7\xad\x05\xfe"]),
("insert into binary_data (data) values (null);", []),
]

EXTRA_DATABASE_SQL = """
Expand Down
6 changes: 4 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_database_page(app_client):
"name": "binary_data",
"columns": ["data"],
"primary_keys": [],
"count": 2,
"count": 3,
"hidden": False,
"fts_table": None,
"foreign_keys": {"incoming": [], "outgoing": []},
Expand Down Expand Up @@ -1812,6 +1812,7 @@ def test_inspect_file_used_for_count(app_client_immutable_and_inspect_file):
[
{"rowid": 1, "data": {"$base64": True, "encoded": "FRwCx60F/g=="}},
{"rowid": 2, "data": {"$base64": True, "encoded": "FRwDx60F/g=="}},
{"rowid": 3, "data": None},
],
None,
),
Expand All @@ -1820,7 +1821,8 @@ def test_inspect_file_used_for_count(app_client_immutable_and_inspect_file):
None,
(
'{"rowid": 1, "data": {"$base64": true, "encoded": "FRwCx60F/g=="}}\n'
'{"rowid": 2, "data": {"$base64": true, "encoded": "FRwDx60F/g=="}}'
'{"rowid": 2, "data": {"$base64": true, "encoded": "FRwDx60F/g=="}}\n'
'{"rowid": 3, "data": null}'
),
),
],
Expand Down
29 changes: 22 additions & 7 deletions tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,21 +1238,36 @@ def test_binary_data_display(app_client):
'<td class="col-rowid type-int">2</td>',
'<td class="col-data type-bytes"><a class="blob-download" href="/fixtures/binary_data/-/blob/2/data.blob">&lt;Binary:\xa07\xa0bytes&gt;</a></td>',
],
[
'<td class="col-Link type-pk"><a href="/fixtures/binary_data/3">3</a></td>',
'<td class="col-rowid type-int">3</td>',
'<td class="col-data type-none">\xa0</td>',
],
]
assert expected_tds == [
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")
]


def test_blob_download(app_client):
response = app_client.get("/fixtures/binary_data/-/blob/1/data.blob")
@pytest.mark.parametrize(
"path,expected_body,expected_filename",
[
(
"/fixtures/binary_data/-/blob/1/data.blob",
b"\x15\x1c\x02\xc7\xad\x05\xfe",
"binary_data-1-data.blob",
),
("/fixtures/binary_data/-/blob/3/data.blob", b"", "binary_data-3-data.blob"),
],
)
def test_blob_download(app_client, path, expected_body, expected_filename):
response = app_client.get(path)
assert response.status == 200
assert response.body == b"\x15\x1c\x02\xc7\xad\x05\xfe"
assert response.body == expected_body
assert response.headers["x-content-type-options"] == "nosniff"
assert (
response.headers["content-disposition"]
== 'attachment; filename="binary_data-1-data.blob"'
)
assert response.headers[
"content-disposition"
] == 'attachment; filename="{}"'.format(expected_filename)
assert response.headers["content-type"] == "application/binary"


Expand Down

0 comments on commit 89519f9

Please sign in to comment.