From 004ef98a2ce4cb034f6e5b33519478d36ca70296 Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Sat, 3 Jan 2015 20:26:35 +0200 Subject: [PATCH] Test Requests session functionality. --- spotipy/client.py | 4 ++-- tests/tests.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spotipy/client.py b/spotipy/client.py index af518e8..c783eba 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -82,7 +82,7 @@ class Spotify(object): r = self._session.request(method, url, headers=headers, **args) - if self.trace: + if self.trace: # pragma: no cover print() print(method, r.url) if payload: @@ -95,7 +95,7 @@ class Spotify(object): -1, u'%s:\n %s' % (r.url, r.json()['error']['message'])) if len(r.text) > 0: results = r.json() - if self.trace: + if self.trace: # pragma: no cover print('RESP', results) print() return results diff --git a/tests/tests.py b/tests/tests.py index 63befc8..0157ae5 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -2,6 +2,7 @@ import spotipy import unittest import pprint +from spotipy.client import SpotifyException class TestSpotipy(unittest.TestCase): @@ -114,6 +115,26 @@ class TestSpotipy(unittest.TestCase): except spotipy.SpotifyException: self.assertTrue(True) + def test_unauthenticated_post_fails(self): + with self.assertRaises(SpotifyException) as cm: + self.spotify.user_playlist_create("spotify", "Best hits of the 90s") + self.assertEqual(cm.exception.http_status, 401) + + def test_custom_requests_session(self): + from requests import Session + sess = Session() + sess.headers["user-agent"] = "spotipy-test" + with_custom_session = spotipy.Spotify(requests_session=sess) + self.assertTrue(with_custom_session.user(user="akx")["uri"] == "spotify:user:akx") + + def test_force_no_requests_session(self): + from requests import Session + with_no_session = spotipy.Spotify(requests_session=False) + self.assertFalse(isinstance(with_no_session._session, Session)) + self.assertTrue(with_no_session.user(user="akx")["uri"] == "spotify:user:akx") + + + ''' Need tests for: