Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'http_success_response_widening'

  • Loading branch information...
commit f261b9f4c0fd0c2a66ad4dbd607f81d9ab6c93b8 2 parents dfe4c02 + f981a7a
@ciaranj ciaranj authored
Showing with 65 additions and 37 deletions.
  1. +1 −1  Readme.md
  2. +3 −3 lib/oauth.js
  3. +61 −33 tests/oauth.js
View
2  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.
View
6 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 ) {
+ 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);
}
});
});
View
94 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
Please sign in to comment.
Something went wrong with that request. Please try again.