Skip to content

Commit

Permalink
Merge branch 'http_success_response_widening'
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranj committed Jun 23, 2011
2 parents dfe4c02 + f981a7a commit f261b9f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions lib/oauth.js
Expand Up @@ -350,16 +350,16 @@ 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);
}
else {
callback({ statusCode: response.statusCode, data: data }, data, response);
}
} else {
callback(null, data, response);
}
});
});
Expand Down
94 changes: 61 additions & 33 deletions tests/oauth.js
Expand Up @@ -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"),
Expand Down Expand Up @@ -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) {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f261b9f

Please sign in to comment.