From 10d44d86719b07126682ae2c930691a428544d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20M=2E=20P=C3=A9rez?= Date: Mon, 5 Mar 2018 08:47:12 +0100 Subject: [PATCH] Avoid passing device_id in body parameters (#190) --- src/spotify-web-api.js | 14 +++++++++++--- test/spotify-web-api.js | 29 ++++++++--------------------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/spotify-web-api.js b/src/spotify-web-api.js index 1173467b..e5ebe30e 100644 --- a/src/spotify-web-api.js +++ b/src/spotify-web-api.js @@ -1083,12 +1083,20 @@ SpotifyWebApi.prototype = { * otherwise an error. Not returned if a callback is given. */ play: function(options, callback) { + /*jshint camelcase: false */ + var _options = options || {}; + var queryParams = _options.device_id ? {device_id: _options.device_id} : null; + var postData = {}; + ['context_uri', 'uris', 'offset'].forEach(function(field) { + if (field in _options) { + postData[field] = _options[field]; + } + }); return WebApiRequest.builder(this.getAccessToken()) .withPath('/v1/me/player/play') - /*jshint camelcase: false */ - .withQueryParameters(options && options.device_id ? {device_id: options.device_id} : null) + .withQueryParameters(queryParams) .withHeaders({ 'Content-Type' : 'application/json' }) - .withBodyParameters(options) + .withBodyParameters(postData) .build() .execute(HttpManager.put, callback); }, diff --git a/test/spotify-web-api.js b/test/spotify-web-api.js index fd935939..1705fbe9 100644 --- a/test/spotify-web-api.js +++ b/test/spotify-web-api.js @@ -1130,25 +1130,6 @@ describe('Spotify Web API', function() { }); }); - it('should upload playlist cover image', function(done) { - sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) { - method.should.equal(superagent.put); - uri.should.equal('https://api.spotify.com/v1/users/thelinmichael/playlists/5ieJqeLJjjI8iJWaxeBLuK/images'); - (options.data).should.eql('longbase64uri') - callback(null, { statusCode : 202 }); - should.not.exist(options.query); - }); - - var api = new SpotifyWebApi(); - api.setAccessToken('long-access-token'); - - api.uploadCustomPlaylistCoverImage('thelinmichael', '5ieJqeLJjjI8iJWaxeBLuK', 'longbase64uri') - .then(function(data) { - (202).should.equal(data.statusCode); - done(); - }); - }); - it('should add tracks to playlist', function(done) { sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) { method.should.equal(superagent.post); @@ -1375,12 +1356,18 @@ describe('Spotify Web API', function() { }); - it('should resume the user\'s playback with a device id', function(done) { + it('should resume the user\'s playback with options', function(done) { sinon.stub(HttpManager, '_makeRequest', function(method, options, uri, callback) { method.should.equal(superagent.put); uri.should.equal('https://api.spotify.com/v1/me/player/play'); options.query.should.eql({device_id: 'my_device_id'}); + JSON.parse(options.data).should.eql({ + context_uri: 'my_context', + offset: { + position: 5 + } + }); callback(); }); @@ -1390,7 +1377,7 @@ describe('Spotify Web API', function() { accessToken : accessToken }); - api.play({device_id: 'my_device_id'}) + api.play({device_id: 'my_device_id', context_uri: 'my_context', offset: {'position': 5}}) .then(function(data) { done(); }, function(err) {