From c78a9ca129884a353d0715c3ed11c3f5dc76b765 Mon Sep 17 00:00:00 2001 From: Neil Mansilla Date: Thu, 23 Jun 2011 09:14:46 -0700 Subject: [PATCH 1/4] Match all 2xx response codes. --- lib/oauth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oauth.js b/lib/oauth.js index 7630451..958139c 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -350,7 +350,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke data+=chunk; }); response.on('end', function () { - if( response.statusCode != 200 ) { + if (!( response.statusCode >= 200 && response.statusCode <= 299 )) { // Follow 302 redirects with Location HTTP header if(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); From 3ed11c05bd1ad39e99cafafcbc7f3732947129dd Mon Sep 17 00:00:00 2001 From: ciaranj Date: Thu, 23 Jun 2011 22:18:11 +0100 Subject: [PATCH 2/4] Inverting the branch logic for easier reading --- lib/oauth.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/oauth.js b/lib/oauth.js index 958139c..e407180 100644 --- a/lib/oauth.js +++ b/lib/oauth.js @@ -350,7 +350,9 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke data+=chunk; }); response.on('end', function () { - if (!( response.statusCode >= 200 && response.statusCode <= 299 )) { + 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) { self._performSecureRequest( oauth_token, oauth_token_secret, method, response.headers.location, extra_params, post_body, post_content_type, callback); @@ -358,8 +360,6 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke else { callback({ statusCode: response.statusCode, data: data }, data, response); } - } else { - callback(null, data, response); } }); }); From 5a6c2594ec71ce6a5f214a14adc73ac8c90ba5b0 Mon Sep 17 00:00:00 2001 From: ciaranj Date: Thu, 23 Jun 2011 22:19:17 +0100 Subject: [PATCH 3/4] Add in specific test for supporting 200 responsecode and one for a 210 responsecode --- tests/oauth.js | 94 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/tests/oauth.js b/tests/oauth.js index 6a00bfb..e05907b 100644 --- a/tests/oauth.js +++ b/tests/oauth.js @@ -4,6 +4,25 @@ var vows = require('vows'), OAuth= require('../lib/oauth').OAuth, OAuthEcho= require('../lib/oauth').OAuthEcho; +var DummyResponse =function( statusCode ) { + this.statusCode= statusCode; + this.headers= {}; +} +DummyResponse.prototype= events.EventEmitter.prototype; +DummyResponse.prototype.setEncoding= function() {} + +var DummyRequest =function( response ) { + this.response= response; +} +DummyRequest.prototype= events.EventEmitter.prototype; +DummyRequest.prototype.write= function(post_body){} +DummyRequest.prototype.write= function(post_body){ + this.emit('response',this.response); +} +DummyRequest.prototype.end= function(){ + this.response.emit('end'); +} + vows.describe('OAuth').addBatch({ 'When generating the signature base string described in http://oauth.net/core/1.0/#sig_base_example': { topic: new OAuth(null, null, null, null, null, null, "HMAC-SHA1"), @@ -511,6 +530,46 @@ vows.describe('OAuth').addBatch({ } }, 'Request With a Callback' : { + 'and a 200 response code is received' : { + 'it should callback successfully' : function(oa) { + var op= oa._createClient; + var callbackCalled = false; + try { + oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { + return new DummyRequest( new DummyResponse(200) ); + } + oa._performSecureRequest("token", "token_secret", 'POST', 'http://originalurl.com', {"scope": "foobar,1,2"}, null, null, function(error) { + // callback + callbackCalled= true; + assert.equal(error, undefined); + }); + assert.equal(callbackCalled, true) + } + finally { + oa._createClient= op; + } + } + }, + 'and a 210 response code is received' : { + 'it should callback successfully' : function(oa) { + var op= oa._createClient; + var callbackCalled = false; + try { + oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { + return new DummyRequest( new DummyResponse(210) ); + } + oa._performSecureRequest("token", "token_secret", 'POST', 'http://originalurl.com', {"scope": "foobar,1,2"}, null, null, function(error) { + // callback + callbackCalled= true; + assert.equal(error, undefined); + }); + 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) { @@ -531,21 +590,9 @@ vows.describe('OAuth').addBatch({ DummyResponse.prototype= events.EventEmitter.prototype; DummyResponse.prototype.setEncoding= function() {} - var DummyRequest =function() { - this.response= new DummyResponse(); - } - DummyRequest.prototype= events.EventEmitter.prototype; - DummyRequest.prototype.write= function(post_body){} - DummyRequest.prototype.write= function(post_body){ - this.emit('response',this.response); - } - DummyRequest.prototype.end= function(){ - this.response.emit('end'); - } - try { oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { - return new DummyRequest(); + 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 ) { @@ -574,28 +621,9 @@ vows.describe('OAuth').addBatch({ 'it should execute the callback, passing the HTTP Response code' : function(oa) { var op= oa._createClient; var callbackCalled = false; - var DummyResponse =function() { - this.statusCode= 302; - this.headers= {}; - } - DummyResponse.prototype= events.EventEmitter.prototype; - DummyResponse.prototype.setEncoding= function() {} - - var DummyRequest =function() { - this.response= new DummyResponse(); - } - DummyRequest.prototype= events.EventEmitter.prototype; - DummyRequest.prototype.write= function(post_body){} - DummyRequest.prototype.write= function(post_body){ - this.emit('response',this.response); - } - DummyRequest.prototype.end= function(){ - this.response.emit('end'); - } - try { oa._createClient= function( port, hostname, method, path, headers, sshEnabled ) { - return new DummyRequest(); + return new DummyRequest( new DummyResponse(302) ); } oa._performSecureRequest("token", "token_secret", 'POST', 'http://originalurl.com', {"scope": "foobar,1,2"}, null, null, function(error) { // callback From f981a7a1002fef458564e8bb52ea5edcff46c586 Mon Sep 17 00:00:00 2001 From: ciaranj Date: Thu, 23 Jun 2011 22:19:32 +0100 Subject: [PATCH 4/4] Updating readme --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index ce209be..dd4f09c 100644 --- a/Readme.md +++ b/Readme.md @@ -14,7 +14,7 @@ so that I can release fixes to the 0.8.x stream if problems come out. Change History ============== -* 0.9.1 - Added support for automatically following 302 redirects (Thanks neyric) Added support for OAuth Echo (Thanks Ryan LeFevre) +* 0.9.1 - Added support for automatically following 302 redirects (Thanks neyric) Added support for OAuth Echo (Thanks Ryan LeFevre). Improved handling of 2xx responses (Thanks Neil Mansilla). * 0.9.0 - Compatibility fixes to bring node-oauth up to speed with node.js 0.4x [thanks to Rasmus Andersson for starting the work ] * 0.8.4 - Fixed issue #14 (Parameter ordering ignored encodings). Added support for repeated parameter names. Implements issue #15 (Use native SHA1 if available, 10x speed improvement!). Fixed issue #16 (Should use POST when requesting access tokens.). Fixed Issue #17 (OAuth2 spec compliance). Implemented enhancement #13 (Adds support for PUT & DELETE http verbs). Fixes issue #18 (Complex/Composite url arguments [thanks novemberborn]) * 0.8.3 - Fixed an issue where the auth header code depended on the Array's toString method (Yohei Sasaki) Updated the getOAuthRequestToken method so we can access google's OAuth secured methods. Also re-implemented and fleshed out the test suite.