diff --git a/lib/oauth2.js b/lib/oauth2.js index 3210171..efe5601 100644 --- a/lib/oauth2.js +++ b/lib/oauth2.js @@ -49,19 +49,25 @@ exports.OAuth2.prototype.buildAuthHeader= function(token) { return this._authMethod + ' ' + token; }; +exports.OAuth2.prototype._chooseHttpLibrary= function( parsedUrl ) { + var http_library= https; + // As this is OAUth2, we *assume* https unless told explicitly otherwise. + if( parsedUrl.protocol != "https:" ) { + http_library= http; + } + return http_library; +}; + exports.OAuth2.prototype._request= function(method, url, headers, post_body, access_token, callback) { - var http_library= https; var creds = crypto.createCredentials({ }); var parsedUrl= URL.parse( url, true ); if( parsedUrl.protocol == "https:" && !parsedUrl.port ) { parsedUrl.port= 443; } - // As this is OAUth2, we *assume* https unless told explicitly otherwise. - if( parsedUrl.protocol != "https:" ) { - http_library= http; - } + var http_library= this._chooseHttpLibrary( parsedUrl ); + var realHeaders= {}; for( var key in this._customHeaders ) { @@ -133,7 +139,7 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_ callback(e); }); - if( options.method == 'POST' && post_body ) { + if( (options.method == 'POST' || options.method == 'PUT') && post_body ) { request.write(post_body); } request.end(); diff --git a/tests/oauth2.js b/tests/oauth2.js index d16a0e0..f27e263 100644 --- a/tests/oauth2.js +++ b/tests/oauth2.js @@ -125,15 +125,68 @@ vows.describe('OAuth2').addBatch({ 'Given an OAuth2 instance with clientId, clientSecret and customHeaders': { topic: new OAuth2("clientId", "clientSecret", undefined, undefined, undefined, { 'SomeHeader': '123' }), - 'When calling get': { + 'When GETing': { 'we should see the custom headers mixed into headers property in options passed to http-library' : function(oa) { oa._executeRequest= function( http_library, options, callback ) { assert.equal(options.headers["SomeHeader"], "123"); }; oa.get("", {}); - } + }, } }, + 'Given an OAuth2 instance with a clientId and clientSecret': { + topic: new OAuth2("clientId", "clientSecret"), + 'When POSTing': { + 'we should see a given string being sent to the request' : function(oa) { + var bodyWritten= false; + oa._chooseHttpLibrary= function() { + return { + request: function(options) { + assert.equal(options.headers["Content-Type"], "text/plain"); + assert.equal(options.headers["Content-Length"], 26); + assert.equal(options.method, "POST"); + return { + end: function() {}, + on: function() {}, + write: function(body) { + bodyWritten= true; + assert.isNotNull(body); + assert.equal(body, "THIS_IS_A_POST_BODY_STRING") + } + } + } + }; + } + oa._request("POST", "", {"Content-Type":"text/plain"}, "THIS_IS_A_POST_BODY_STRING"); + assert.ok( bodyWritten ); + } + }, + 'When PUTing': { + 'we should see a given string being sent to the request' : function(oa) { + var bodyWritten= false; + oa._chooseHttpLibrary= function() { + return { + request: function(options) { + assert.equal(options.headers["Content-Type"], "text/plain"); + assert.equal(options.headers["Content-Length"], 25); + assert.equal(options.method, "PUT"); + return { + end: function() {}, + on: function() {}, + write: function(body) { + bodyWritten= true; + assert.isNotNull(body); + assert.equal(body, "THIS_IS_A_PUT_BODY_STRING") + } + } + } + }; + } + oa._request("PUT", "", {"Content-Type":"text/plain"}, "THIS_IS_A_PUT_BODY_STRING"); + assert.ok( bodyWritten ); + } + } + }, 'When the user passes in the User-Agent in customHeaders': { topic: new OAuth2("clientId", "clientSecret", undefined, undefined, undefined, { 'User-Agent': '123Agent' }),