From d8b573144627aa37134427753ae9eb1a9b75c415 Mon Sep 17 00:00:00 2001 From: bdickason Date: Thu, 7 Jul 2011 02:42:55 -0400 Subject: [PATCH] Added support and test coverage for following 301 redirects --- lib/oauth.js | 4 +-- tests/oauth.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 8ee56ce..c7f3b82 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -353,8 +353,8 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke if ( response.statusCode >= 200 && response.statusCode <= 299 ) { callback(null, data, response); } else { - // Follow 302 redirects with Location HTTP header - if(response.statusCode == 302 && response.headers && response.headers.location) { + // Follow 301 or 302 redirects with Location HTTP header + if((response.statusCode == 301 || response.statusCode == 302) && response.headers && response.headers.location) { self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback); } else { diff --git a/tests/oauth.js b/tests/oauth.js index 22c687b..0609df6 100644 --- a/tests/oauth.js +++ b/tests/oauth.js @@ -600,6 +600,74 @@ vows.describe('OAuth').addBatch({ } } }, + 'And A 301 redirect is received' : { + 'and there is a location header' : { + 'it should (re)perform the secure request but with the new location' : function(oa) { + var op= oa._createClient; + var psr= oa._performSecureRequest; + var responseCounter = 1; + var callbackCalled = false; + var DummyResponse =function() { + if( responseCounter == 1 ){ + this.statusCode= 301; + this.headers= {location:"http://redirectto.com"}; + responseCounter++; + } + else { + this.statusCode= 200; + } + } + DummyResponse.prototype= events.EventEmitter.prototype; + DummyResponse.prototype.setEncoding= function() {} + + try { + oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { + return new DummyRequest( new DummyResponse() ); + } + oa._performSecureRequest= function( oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) { + if( responseCounter == 1 ) { + assert.equal(url, "http://originalurl.com"); + } + else { + assert.equal(url, "http://redirectto.com"); + } + return psr.call(oa, oauth_token, oauth_token_secret, method, url, extra_params, post_body, post_content_type, callback ) + } + + oa._performSecureRequest("token", "token_secret", 'POST', 'http://originalurl.com', {"scope": "foobar,1,2"}, null, null, function() { + // callback + assert.equal(responseCounter, 2); + callbackCalled= true; + }); + assert.equal(callbackCalled, true) + } + finally { + oa._createClient= op; + oa._performSecureRequest= psr; + } + } + }, + 'but there is no location header' : { + 'it should execute the callback, passing the HTTP Response code' : function(oa) { + var op= oa._createClient; + var callbackCalled = false; + try { + oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { + return new DummyRequest( new DummyResponse(301) ); + } + oa._performSecureRequest("token", "token_secret", 'POST', 'http://originalurl.com', {"scope": "foobar,1,2"}, null, null, function(error) { + // callback + assert.equal(error.statusCode, 301); + callbackCalled= true; + }); + assert.equal(callbackCalled, true) + } + finally { + oa._createClient= op; + } + } + } + }, 'And A 302 redirect is received' : { 'and there is a location header' : { 'it should (re)perform the secure request but with the new location' : function(oa) {