Permalink
Browse files

Added tests for 302 redirect functionality

  • Loading branch information...
1 parent 8050d82 commit 9fea0b54588fa232dadb82d52e6f7f470bd55e4d @ciaranj committed Apr 19, 2011
Showing with 106 additions and 3 deletions.
  1. +2 −1 Readme.md
  2. +1 −1 lib/oauth.js
  3. +1 −1 package.json
  4. +102 −0 tests/oauth.js
View
@@ -14,7 +14,8 @@ Please be aware that when moving from 0.8.x to 0.9.0 there are no major API chan
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)
* 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
@@ -310,7 +310,7 @@ exports.OAuth.prototype._performSecureRequest= function( oauth_token, oauth_toke
response.on('end', function () {
if( response.statusCode != 200 ) {
// Follow 302 redirects with Location HTTP header
- if(response.statusCode == 302 && response.headers.location) {
+ 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 {
View
@@ -1,6 +1,6 @@
{ "name" : "oauth"
, "description" : "Library for interacting with OAuth 1.0, 1.0A and 2. Provides simplified client access and allows for construction of more complex apis and OAuth providers."
-, "version" : "0.8.4"
+, "version" : "0.9.1"
, "directories" : { "lib" : "./lib" }
, "main" : "index.js"
, "author" : "Ciaran Jessup <ciaranj@gmail.com>"
View
@@ -1,5 +1,6 @@
var vows = require('vows'),
assert = require('assert'),
+ events = require('events'),
OAuth= require('../lib/oauth').OAuth;
vows.describe('OAuth').addBatch({
@@ -494,6 +495,107 @@ vows.describe('OAuth').addBatch({
}
}
}
+ },
+ 'Request With a Callback' : {
+ '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) {
+ var op= oa._createClient;
+ var psr= oa._performSecureRequest;
+ var responseCounter = 1;
+ var callbackCalled = false;
+ var DummyResponse =function() {
+ if( responseCounter == 1 ){
+ this.statusCode= 302;
+ this.headers= {location:"http://redirectto.com"};
+ responseCounter++;
+ }
+ else {
+ this.statusCode= 200;
+ }
+ }
+ 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();
+ }
+ 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;
+ 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();
+ }
+ oa._performSecureRequest("token", "token_secret", 'POST', 'http://originalurl.com', {"scope": "foobar,1,2"}, null, null, function(error) {
+ // callback
+ assert.equal(error.statusCode, 302);
+ callbackCalled= true;
+ });
+ assert.equal(callbackCalled, true)
+ }
+ finally {
+ oa._createClient= op;
+ }
+ }
+ }
+ }
}
}
}).export(module);

0 comments on commit 9fea0b5

Please sign in to comment.