diff --git a/Makefile b/Makefile index be7c339..7723a39 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,6 @@ # Run all tests # test: - @@node_modules/.bin/vows tests/* --spec + @@node_modules/.bin/vows tests/*tests.js --spec .PHONY: test install diff --git a/Readme.md b/Readme.md index fda2e22..8913210 100644 --- a/Readme.md +++ b/Readme.md @@ -78,7 +78,8 @@ describe('OAuth2',function(){ Change History ============== - +* 0.9.14 + - OAuth2: Extend 'successful' token responses to include anything in the 2xx range. * 0.9.13 - OAuth2: Fixes the "createCredentials() is deprecated, use tls.createSecureContext instead" message. (thank you AJ ONeal) * 0.9.12 diff --git a/lib/oauth2.js b/lib/oauth2.js index 54c5056..94ed662 100644 --- a/lib/oauth2.js +++ b/lib/oauth2.js @@ -119,7 +119,7 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_ function passBackControl( response, result ) { if(!callbackCalled) { callbackCalled=true; - if( response.statusCode != 200 && (response.statusCode != 301) && (response.statusCode != 302) ) { + if( !(response.statusCode >= 200 && response.statusCode <= 299) && (response.statusCode != 301) && (response.statusCode != 302) ) { callback({ statusCode: response.statusCode, data: result }); } else { callback(null, result, response); @@ -129,7 +129,8 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_ var result= ""; - var request = http_library.request(options, function (response) { + var request = http_library.request(options); + request.on('response', function (response) { response.on("data", function (chunk) { result+= chunk }); diff --git a/package.json b/package.json index f08d704..1a55533 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "oauth" , "description" : "Library for interacting with OAuth 1.0, 1.0A, 2 and Echo. Provides simplified client access and allows for construction of more complex apis and OAuth providers." -, "version" : "0.9.13" +, "version" : "0.9.14" , "directories" : { "lib" : "./lib" } , "main" : "index.js" , "author" : "Ciaran Jessup " diff --git a/tests/oauth2.js b/tests/oauth2tests.js similarity index 91% rename from tests/oauth2.js rename to tests/oauth2tests.js index ab9bb96..134f85b 100644 --- a/tests/oauth2.js +++ b/tests/oauth2tests.js @@ -1,5 +1,7 @@ var vows = require('vows'), assert = require('assert'), + DummyResponse= require('./shared').DummyResponse, + DummyRequest= require('./shared').DummyRequest, https = require('https'), OAuth2= require('../lib/oauth2').OAuth2, url = require('url'); @@ -7,6 +9,34 @@ var vows = require('vows'), vows.describe('OAuth2').addBatch({ 'Given an OAuth2 instance with clientId and clientSecret, ': { topic: new OAuth2("clientId", "clientSecret"), + 'When dealing with the response from the OP': { + 'we should treat a 201 response as a success': function(oa) { + var callbackCalled= false; + var http_library= { + request: function() { + return new DummyRequest(new DummyResponse(201)); + } + }; + oa._executeRequest( http_library, {}, null, function(err, result, response) { + callbackCalled= true; + assert.equal(err, null); + }); + assert.ok(callbackCalled); + }, + 'we should treat a 200 response as a success': function(oa) { + var callbackCalled= false; + var http_library= { + request: function() { + return new DummyRequest(new DummyResponse(200)); + } + }; + oa._executeRequest( http_library, {}, null, function(err, result, response) { + callbackCalled= true; + assert.equal(err, null); + }); + assert.ok(callbackCalled); + } + }, 'When handling the access token response': { 'we should correctly extract the token if received as form-data': function (oa) { oa._request= function( method, url, fo, bar, bleh, callback) { diff --git a/tests/oauth.js b/tests/oauthtests.js similarity index 98% rename from tests/oauth.js rename to tests/oauthtests.js index e88b754..d36bfed 100644 --- a/tests/oauth.js +++ b/tests/oauthtests.js @@ -1,29 +1,12 @@ var vows = require('vows'), assert = require('assert'), + DummyResponse= require('./shared').DummyResponse, + DummyRequest= require('./shared').DummyRequest, events = require('events'), OAuth= require('../lib/oauth').OAuth, OAuthEcho= require('../lib/oauth').OAuthEcho, crypto = require('crypto'); -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'); -} - //Valid RSA keypair used to test RSA-SHA1 signature method var RsaPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\n" + "MIICXQIBAAKBgQDizE4gQP5nPQhzof/Vp2U2DDY3UY/Gxha2CwKW0URe7McxtnmE\n" + diff --git a/tests/sha1.js b/tests/sha1tests.js similarity index 100% rename from tests/sha1.js rename to tests/sha1tests.js diff --git a/tests/shared.js b/tests/shared.js new file mode 100644 index 0000000..f4c8094 --- /dev/null +++ b/tests/shared.js @@ -0,0 +1,26 @@ +var events = require('events'); + +exports.DummyResponse = function( statusCode ) { + this.statusCode= statusCode; + this.headers= {}; +} +exports.DummyResponse.prototype= events.EventEmitter.prototype; +exports.DummyResponse.prototype.setEncoding= function() {} + +exports.DummyRequest =function( response ) { + this.response= response; + this.responseSent= false; +} +exports.DummyRequest.prototype= events.EventEmitter.prototype; +exports.DummyRequest.prototype.write= function(post_body){} +exports.DummyRequest.prototype.write= function(post_body){ + this.responseSent= true; + this.emit('response',this.response); +} +exports.DummyRequest.prototype.end= function(){ + if(!this.responseSent) { + this.responseSent= true; + this.emit('response',this.response); + } + this.response.emit('end'); +} \ No newline at end of file