From 03b83306968ca4a1900143f00a2861516c1d0502 Mon Sep 17 00:00:00 2001 From: Ryan Ausanka-Crues Date: Thu, 27 Jul 2017 11:33:31 -0700 Subject: [PATCH] Modify _performSecureRequest to not always set the content-type header to application/x-www-form-urlencoded if using post/get/etc methods. Addresses #331 --- lib/oauth.js | 6 +++++- tests/oauthtests.js | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 50dccf99..9d015730 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -28,6 +28,7 @@ exports.OAuth= function(requestUrl, accessUrl, consumerKey, consumerSecret, vers throw new Error("Un-supported signature method: " + signatureMethod ) this._signatureMethod= signatureMethod; this._nonceSize= nonceSize || 32; + this._contentType = 'application/x-www-form-urlencoded'; this._headers= customHeaders || {"Accept" : "*/*", "Connection" : "close", "User-Agent" : "Node authentication"} @@ -309,7 +310,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke var orderedParameters= this._prepareParameters(oauth_token, oauth_token_secret, method, url, extra_params); if( !post_content_type ) { - post_content_type= "application/x-www-form-urlencoded"; + post_content_type= this._contentType; } var parsedUrl= URL.parse( url, false ); if( parsedUrl.protocol == "http:" && !parsedUrl.port ) parsedUrl.port= 80; @@ -452,6 +453,9 @@ exports.OAuth.prototype.setClientOptions= function(options) { this._clientOptions= mergedOptions; }; +exports.OAuth.prototype.setContentType= function(contentType) { + this._contentType = contentType; +}; exports.OAuth.prototype.getOAuthAccessToken= function(oauth_token, oauth_token_secret, oauth_verifier, callback) { var extraParams= {}; diff --git a/tests/oauthtests.js b/tests/oauthtests.js index d36bfed7..aa81b4d4 100644 --- a/tests/oauthtests.js +++ b/tests/oauthtests.js @@ -320,7 +320,48 @@ vows.describe('OAuth').addBatch({ oa._createClient= op; } } - } + }, + 'Setting content-type on OAuth object should set content-type on request to value passed to setContentType': function(oa) { + var op= oa._createClient; + var opContentType = oa._contentType; + var expectedContentType = 'application/json'; + oa.setContentType(expectedContentType); + var createClientHeaders = null; + try { + oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { + createClientHeaders = headers; + return { + write: function(post_body){ + } + }; + }; + oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"}); + assert.equal(createClientHeaders['Content-Type'], expectedContentType); + } + finally { + oa._createClient= op; + oa._contentType = opContentType; + } + }, + 'Not setting content-type on OAuth object should set content-type on request to application/x-www-form-urlencoded': function(oa) { + var createClientHeaders = null; + var op= oa._createClient; + try { + oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { + createClientHeaders = headers; + return { + write: function(post_body){ + } + }; + }; + oa._performSecureRequest("token", "token_secret", 'POST', 'http://foo.com/protected_resource', {"scope": "foobar,1,2"}); + assert.equal(createClientHeaders['Content-Type'], 'application/x-www-form-urlencoded'); + } + finally { + oa.setContentType(null); + oa._createClient= op; + } + } }, 'When performing a secure' : { topic: new OAuth("http://foo.com/RequestToken",