Skip to content

Commit e70b147

Browse files
authored
Add missing content type to backup http endpoint (#152433)
1 parent 031b127 commit e70b147

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

homeassistant/components/backup/http.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing import IO, cast
99

1010
from aiohttp import BodyPartReader
11-
from aiohttp.hdrs import CONTENT_DISPOSITION
11+
from aiohttp.hdrs import CONTENT_DISPOSITION, CONTENT_TYPE
1212
from aiohttp.web import FileResponse, Request, Response, StreamResponse
1313
from multidict import istr
1414

@@ -76,7 +76,8 @@ async def get(
7676
return Response(status=HTTPStatus.NOT_FOUND)
7777

7878
headers = {
79-
CONTENT_DISPOSITION: f"attachment; filename={slugify(backup.name)}.tar"
79+
CONTENT_DISPOSITION: f"attachment; filename={slugify(backup.name)}.tar",
80+
CONTENT_TYPE: "application/x-tar",
8081
}
8182

8283
try:

tests/components/backup/test_http.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
from collections.abc import AsyncIterator
55
from io import BytesIO, StringIO
66
import json
7+
import re
78
import tarfile
89
from typing import Any
910
from unittest.mock import patch
1011

1112
from aiohttp import web
13+
from aiohttp.hdrs import CONTENT_DISPOSITION, CONTENT_TYPE
1214
import pytest
1315

1416
from homeassistant.components.backup import (
@@ -166,10 +168,19 @@ async def _test_downloading_encrypted_backup(
166168
agent_id: str,
167169
) -> None:
168170
"""Test downloading an encrypted backup file."""
171+
172+
def assert_tar_download_response(resp: web.Response) -> None:
173+
assert resp.status == 200
174+
assert resp.headers.get(CONTENT_TYPE, "") == "application/x-tar"
175+
assert re.match(
176+
r"attachment; filename=.*\.tar", resp.headers.get(CONTENT_DISPOSITION, "")
177+
)
178+
169179
# Try downloading without supplying a password
170180
client = await hass_client()
171181
resp = await client.get(f"/api/backup/download/c0cb53bd?agent_id={agent_id}")
172-
assert resp.status == 200
182+
assert_tar_download_response(resp)
183+
173184
backup = await resp.read()
174185
# We expect a valid outer tar file, but the inner tar file is encrypted and
175186
# can't be read
@@ -187,7 +198,7 @@ async def _test_downloading_encrypted_backup(
187198
resp = await client.get(
188199
f"/api/backup/download/c0cb53bd?agent_id={agent_id}&password=wrong"
189200
)
190-
assert resp.status == 200
201+
assert_tar_download_response(resp)
191202
backup = await resp.read()
192203
# We expect a truncated outer tar file
193204
with (
@@ -200,7 +211,7 @@ async def _test_downloading_encrypted_backup(
200211
resp = await client.get(
201212
f"/api/backup/download/c0cb53bd?agent_id={agent_id}&password=hunter2"
202213
)
203-
assert resp.status == 200
214+
assert_tar_download_response(resp)
204215
backup = await resp.read()
205216
# We expect a valid outer tar file, the inner tar file is decrypted and can be read
206217
with (

0 commit comments

Comments
 (0)