Skip to content

Commit

Permalink
fix(client): allow redirects in stream URL check
Browse files Browse the repository at this point in the history
- Update PodMeClient to follow redirects when checking stream URLs
- Adjust mocked responses in tests to handle redirects properly
  • Loading branch information
bendikrb committed Oct 15, 2024
1 parent f818b07 commit 401a08d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion podme_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ async def check_stream_url(self, stream_url: URL | str) -> FetchedFileInfo | Non
_LOGGER.debug("Checking stream URL: <%s>", stream_url)

# Check if the audio URL is directly downloadable
response = await self.session.head(stream_url)
response = await self.session.head(stream_url, allow_redirects=True)
if response.status != HTTPStatus.OK:
raise PodMeApiStreamUrlError(f"Stream URL is not downloadable: <{stream_url}>")
content_length = response.headers.get("Content-Length")
Expand Down
40 changes: 34 additions & 6 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,15 @@ def setup_stream_mocks(
repeat=2,
)
else:
redirect_url = stream_url.with_name(f"redir_{stream_url.name}")
resp = {
"status": 302,
"headers": {
"Accept-Ranges": "bytes",
"Content-Type": "audio/mpeg",
"Content-Length": str(len(files["normal.mp3"])),
}
"Location": str(redirect_url),
},
}
if head_request_error:
resp = {"status": 404}

aresponses.add(
stream_url.host,
stream_url.path,
Expand All @@ -324,10 +323,24 @@ def setup_stream_mocks(
)

resp = {
"body": files["normal.mp3"],
"headers": {
"Accept-Ranges": "bytes",
"Content-Type": "audio/mpeg",
"Content-Length": str(len(files["normal.mp3"])),
}
}
aresponses.add(
redirect_url.host,
redirect_url.path,
"HEAD",
aresponses.Response(**resp),
repeat=2,
)

resp = {
"status": 302,
"headers": {
"Location": str(redirect_url),
},
}
if get_request_error:
Expand All @@ -340,6 +353,21 @@ def setup_stream_mocks(
repeat=2,
)

resp = {
"body": files["normal.mp3"],
"headers": {
"Content-Type": "audio/mpeg",
"Content-Length": str(len(files["normal.mp3"])),
},
}
aresponses.add(
redirect_url.host,
redirect_url.path,
"GET",
aresponses.Response(**resp),
repeat=2,
)

# Add response for episode info
aresponses.add(
URL(PODME_API_URL).host,
Expand Down

0 comments on commit 401a08d

Please sign in to comment.