Test Requests session functionality.

This commit is contained in:
Aarni Koskela 2015-01-03 20:26:35 +02:00
parent 62ced74126
commit 004ef98a2c
2 changed files with 23 additions and 2 deletions

View File

@ -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

View File

@ -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: