Skip to content

Commit 2747ab6

Browse files
authored
Handle empty response better (#321)
* Handle empty response better * Handle empty response better
1 parent 27c0d29 commit 2747ab6

2 files changed

Lines changed: 14 additions & 62 deletions

File tree

src/spotifyaio/spotify.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from aiohttp.hdrs import METH_GET, METH_POST, METH_PUT
1212
from yarl import URL
1313

14-
from spotifyaio.exceptions import SpotifyConnectionError, SpotifyError
14+
from spotifyaio.exceptions import SpotifyConnectionError
1515
from spotifyaio.models import (
1616
Album,
1717
Artist,
@@ -113,15 +113,8 @@ async def _request(
113113
msg = "Timeout occurred while connecting to Spotify"
114114
raise SpotifyConnectionError(msg) from exception
115115

116-
content_type = response.headers.get("Content-Type", "")
117-
118-
if "application/json" not in content_type:
119-
text = await response.text()
120-
msg = "Unexpected response from Spotify"
121-
raise SpotifyError(
122-
msg,
123-
{"Content-Type": content_type, "response": text},
124-
)
116+
if response.status == 204:
117+
return ""
125118

126119
return await response.text()
127120

tests/test_spotify.py

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import pytest
1212
from yarl import URL
1313

14-
from spotifyaio import RepeatMode, SpotifyClient, SpotifyConnectionError, SpotifyError
14+
from spotifyaio import RepeatMode, SpotifyClient, SpotifyConnectionError
1515

1616
from . import load_fixture
1717
from .const import HEADERS, SPOTIFY_URL
@@ -74,21 +74,6 @@ async def _get_token() -> str:
7474
assert spotify._token == "token" # pylint: disable=protected-access
7575

7676

77-
async def test_unexpected_server_response(
78-
responses: aioresponses,
79-
authenticated_client: SpotifyClient,
80-
) -> None:
81-
"""Test handling unexpected response."""
82-
responses.get(
83-
f"{SPOTIFY_URL}/v1/me/player?additional_types=track,episode",
84-
status=200,
85-
headers={"Content-Type": "plain/text"},
86-
body="Yes",
87-
)
88-
with pytest.raises(SpotifyError):
89-
assert await authenticated_client.get_playback()
90-
91-
9277
async def test_timeout(
9378
responses: aioresponses,
9479
) -> None:
@@ -167,8 +152,7 @@ async def test_transfer_playback(
167152
) -> None:
168153
"""Test transferring playback."""
169154
responses.put(
170-
f"{SPOTIFY_URL}/v1/me/player",
171-
status=204,
155+
f"{SPOTIFY_URL}/v1/me/player", status=200, body="3o0RYoo5iOMKSmEbunsbvW"
172156
)
173157
await authenticated_client.transfer_playback("test")
174158
responses.assert_called_once_with(
@@ -296,10 +280,7 @@ async def test_resume_playback(
296280
path="/v1/me/player/play",
297281
query=expected_params,
298282
)
299-
responses.put(
300-
url,
301-
status=204,
302-
)
283+
responses.put(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
303284
await authenticated_client.start_playback(**arguments)
304285
responses.assert_called_once_with(
305286
f"{SPOTIFY_URL}/v1/me/player/play",
@@ -331,10 +312,7 @@ async def test_pause_playback(
331312
path="/v1/me/player/pause",
332313
query=expected_params,
333314
)
334-
responses.put(
335-
url,
336-
status=204,
337-
)
315+
responses.put(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
338316
await authenticated_client.pause_playback(**arguments)
339317
responses.assert_called_once_with(
340318
f"{SPOTIFY_URL}/v1/me/player/pause",
@@ -366,10 +344,7 @@ async def test_next_track(
366344
path="/v1/me/player/next",
367345
query=expected_params,
368346
)
369-
responses.post(
370-
url,
371-
status=204,
372-
)
347+
responses.post(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
373348
await authenticated_client.next_track(**arguments)
374349
responses.assert_called_once_with(
375350
f"{SPOTIFY_URL}/v1/me/player/next",
@@ -401,10 +376,7 @@ async def test_previous_track(
401376
path="/v1/me/player/previous",
402377
query=expected_params,
403378
)
404-
responses.post(
405-
url,
406-
status=204,
407-
)
379+
responses.post(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
408380
await authenticated_client.previous_track(**arguments)
409381
responses.assert_called_once_with(
410382
f"{SPOTIFY_URL}/v1/me/player/previous",
@@ -439,10 +411,7 @@ async def test_seek_track(
439411
path="/v1/me/player/seek",
440412
query=expected_params,
441413
)
442-
responses.put(
443-
url,
444-
status=204,
445-
)
414+
responses.put(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
446415
await authenticated_client.seek_track(**arguments)
447416
responses.assert_called_once_with(
448417
f"{SPOTIFY_URL}/v1/me/player/seek",
@@ -479,10 +448,7 @@ async def test_set_repeat(
479448
path="/v1/me/player/repeat",
480449
query=expected_params,
481450
)
482-
responses.put(
483-
url,
484-
status=204,
485-
)
451+
responses.put(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
486452
await authenticated_client.set_repeat(**arguments)
487453
responses.assert_called_once_with(
488454
f"{SPOTIFY_URL}/v1/me/player/repeat",
@@ -517,10 +483,7 @@ async def test_set_volume(
517483
path="/v1/me/player/volume",
518484
query=expected_params,
519485
)
520-
responses.put(
521-
url,
522-
status=204,
523-
)
486+
responses.put(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
524487
await authenticated_client.set_volume(**arguments)
525488
responses.assert_called_once_with(
526489
f"{SPOTIFY_URL}/v1/me/player/volume",
@@ -555,10 +518,7 @@ async def test_set_shuffle(
555518
path="/v1/me/player/shuffle",
556519
query=expected_params,
557520
)
558-
responses.put(
559-
url,
560-
status=204,
561-
)
521+
responses.put(url, status=200, body="3o0RYoo5iOMKSmEbunsbvW")
562522
await authenticated_client.set_shuffle(**arguments)
563523
responses.assert_called_once_with(
564524
f"{SPOTIFY_URL}/v1/me/player/shuffle",
@@ -590,8 +550,7 @@ async def test_add_to_queue(
590550
) -> None:
591551
"""Test adding to queue."""
592552
responses.post(
593-
f"{SPOTIFY_URL}/v1/me/player/queue",
594-
status=204,
553+
f"{SPOTIFY_URL}/v1/me/player/queue", status=200, body="3o0RYoo5iOMKSmEbunsbvW"
595554
)
596555
await authenticated_client.add_to_queue(**arguments)
597556
responses.assert_called_once_with(

0 commit comments

Comments
 (0)