Add modifications for snapshot uploads (#40503)

Co-authored-by: springstan <46536646+springstan@users.noreply.github.com>
This commit is contained in:
Joakim Sørensen 2020-09-25 10:02:26 +02:00 committed by GitHub
parent 371b589cb2
commit 2d429ea678
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from .const import X_HASS_IS_ADMIN, X_HASS_USER_ID, X_HASSIO
_LOGGER = logging.getLogger(__name__)
MAX_UPLOAD_SIZE = 1024 * 1024 * 1024
NO_TIMEOUT = re.compile(
r"^(?:"
@ -71,6 +72,16 @@ class HassIOView(HomeAssistantView):
read_timeout = _get_timeout(path)
data = None
headers = _init_header(request)
if path == "snapshots/new/upload":
# We need to reuse the full content type that includes the boundary
headers[
"Content-Type"
] = request._stored_content_type # pylint: disable=protected-access
# Snapshots are big, so we need to adjust the allowed size
request._client_max_size = ( # pylint: disable=protected-access
MAX_UPLOAD_SIZE
)
try:
with async_timeout.timeout(10):

View File

@ -129,3 +129,21 @@ async def test_forwarding_user_info(hassio_client, hass_admin_user, aioclient_mo
req_headers = aioclient_mock.mock_calls[0][-1]
req_headers["X-Hass-User-ID"] == hass_admin_user.id
req_headers["X-Hass-Is-Admin"] == "1"
async def test_snapshot_upload_headers(hassio_client, aioclient_mock):
"""Test that we forward the full header for snapshot upload."""
content_type = "multipart/form-data; boundary='--webkit'"
aioclient_mock.get("http://127.0.0.1/snapshots/new/upload")
resp = await hassio_client.get(
"/api/hassio/snapshots/new/upload", headers={"Content-Type": content_type}
)
# Check we got right response
assert resp.status == 200
assert len(aioclient_mock.mock_calls) == 1
req_headers = aioclient_mock.mock_calls[0][-1]
req_headers["Content-Type"] == content_type