Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates redirect handling #76

Merged
merged 16 commits into from
Sep 24, 2012
6 changes: 6 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

0.9.4 / 2012-09-20
==================

* fix `Buffer` responses [TooTallNate]
* fix `res.type` when a "type" param is present [TooTallNate]

0.9.3 / 2012-09-18
==================

Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "superagent",
"repo": "visionmedia/superagent",
"description": "awesome http requests",
"version": "0.9.3",
"version": "0.9.4",
"keywords": ["http", "ajax", "request", "agent"],
"main": "lib/superagent.js"
}
65 changes: 53 additions & 12 deletions lib/node/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*!
* superagent
* Copyright (c) 2011 TJ Holowaychuk <[email protected]>
Expand Down Expand Up @@ -126,6 +125,9 @@ function Request(method, url) {
this.writable = true;
this._redirects = 0;
this.redirects(5);
this._redirectData = false;
this._redirectAuth = false;
this._buffer = true;
this.attachments = [];
this.cookies = '';
this._redirectList = [];
Expand Down Expand Up @@ -173,6 +175,33 @@ Request.prototype.redirects = function(n){
return this;
};

/**
* Set if redirects should forward data.
*
* @param {boolean} bool
* @return {Request} for chaining
* @api public
*/

Request.prototype.redirectData = function(bool){
this._redirectData = bool;
return this;
};

/**
* Set the if redirects should forward Auth,
* when redirected in the same domain.
*
* @param {boolean} bool
* @return {Request} for chaining
* @api public
*/

Request.prototype.redirectAuth = function(bool){
this._redirectAuth = bool;
return this;
};

/**
* Return a new `Part` for this request.
*
Expand Down Expand Up @@ -429,20 +458,34 @@ Request.prototype.clearTimeout = function(){

Request.prototype.redirect = function(res){
var url = res.headers.location;
var auth = this.req._headers.authorization;
var reqHost = this.req._headers.host;
var seeOther = 303 == res.statusCode;
var idempotent = 'HEAD' == this.method || 'GET' == this.method;

if (!~url.indexOf('://')) {
if (0 != url.indexOf('//')) {
url = '//' + this.host + url;
}
url = this.protocol + url;
}

delete this.req;
this.method = 'HEAD' == this.method
? this.method
: 'GET';
this._data = null;
this.url = url;

delete this.req;

if (this._redirectAuth && ~url.indexOf(reqHost)) {
this.set('Authorization', auth);
}

if (!seeOther && !idempotent && this._redirectData) {
this.send(this._data);
} else {
this.method = 'HEAD' == this.method
? this.method
: 'GET';
this._data = null;
}

this._redirectList.push(url);
this.emit('redirect', res);
this.end(this._callback);
Expand Down Expand Up @@ -653,12 +696,10 @@ Request.prototype.end = function(fn){

// buffered response
if (buffer) {
res.text = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ res.text += chunk; });
var parse = 'text' == type
? exports.parse.text
: exports.parse[utils.type(res.headers['content-type'] || '')];

// parser
var parse = exports.parse[utils.type(res.headers['content-type'] || '')];
if (parse) {
parse(res, function(err, obj){
// TODO: handle error
Expand Down
1 change: 1 addition & 0 deletions lib/node/parsers/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

exports['application/x-www-form-urlencoded'] = require('./urlencoded');
exports['application/json'] = require('./json');
exports.text = require('./text');
6 changes: 3 additions & 3 deletions lib/node/parsers/json.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

module.exports = function(res, fn){
var buf = '';
res.text = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ buf += chunk; });
res.on('data', function(chunk){ res.text += chunk; });
res.on('end', function(){
try {
fn(null, JSON.parse(buf));
fn(null, JSON.parse(res.text));
} catch (err) {
fn(err);
}
Expand Down
7 changes: 7 additions & 0 deletions lib/node/parsers/text.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

module.exports = function(res, fn){
res.text = '';
res.setEncoding('utf8');
res.on('data', function(chunk){ res.text += chunk; });
res.on('end', fn);
};
6 changes: 3 additions & 3 deletions lib/node/parsers/urlencoded.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
var qs = require('qs');

module.exports = function(res, fn){
var buf = '';
res.text = '';
res.setEncoding('ascii');
res.on('data', function(chunk){ buf += chunk; });
res.on('data', function(chunk){ res.text += chunk; });
res.on('end', function(){
try {
fn(null, qs.parse(buf));
fn(null, qs.parse(res.text));
} catch (err) {
fn(err);
}
Expand Down
18 changes: 3 additions & 15 deletions lib/superagent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@
? function(s) { return s.trim(); }
: function(s) { return s.replace(/(^\s*|\s*$)/g, ''); };

/**
* Check if `obj` is a function.
*
* @param {Mixed} obj
* @return {Boolean}
* @api private
*/

function isFunction(obj) {
return 'function' == typeof obj;
}

/**
* Check if `obj` is an object.
*
Expand All @@ -67,7 +55,7 @@
*/

function isObject(obj) {
return null != obj && 'object' == typeof obj;
return obj === Object(obj);
}

/**
Expand Down Expand Up @@ -668,7 +656,7 @@

request.get = function(url, data, fn){
var req = request('GET', url);
if (isFunction(data)) fn = data, data = null;
if ('function' == typeof data) fn = data, data = null;
if (data) req.query(data);
if (fn) req.end(fn);
return req;
Expand All @@ -686,7 +674,7 @@

request.head = function(url, data, fn){
var req = request('HEAD', url);
if (isFunction(data)) fn = data, data = null;
if ('function' == typeof data) fn = data, data = null;
if (data) req.send(data);
if (fn) req.end(fn);
return req;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "superagent",
"version": "0.9.3",
"version": "0.9.4",
"description": "elegant & feature rich browser / node HTTP with a fluent API",
"keywords": [
"http",
Expand Down
2 changes: 1 addition & 1 deletion test/node/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ describe('exports', function(){

it('should expose .parse', function(){
Object.keys(request.parse)
.should.eql(['application/x-www-form-urlencoded', 'application/json']);
.should.eql(['application/x-www-form-urlencoded', 'application/json', 'text']);
})
})
60 changes: 57 additions & 3 deletions test/node/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var EventEmitter = require('events').EventEmitter
, assert = require('assert')
, app = express()
, should = require('should');

app.use(express.bodyParser());
app.get('/', function(req, res){
res.redirect('/movies');
});
Expand All @@ -21,11 +21,25 @@ app.get('/movies/all', function(req, res){
app.get('/movies/all/0', function(req, res){
res.send('first movie page');
});

app.post('/movie', function(req, res){
res.redirect('/movies/all/0');
});

app.post('/addmovie', function(req, res){
res.redirect('/movies/add');
});
app.post('/movies/add', function(req, res){
res.send('movie '+req.body.name+' added');
});
app.get('/private', function(req, res){
res.redirect('/authed');
});
app.get('/authed', function(req, res){
if (req.get('authorization')) {
res.send('authed');
} else {
res.send('not authed');
}
});
app.get('/tobi', function(req, res){
res.send('tobi');
});
Expand Down Expand Up @@ -118,5 +132,45 @@ describe('request', function(){
done();
});
})
it('should redirect as POST if user specified', function(done){
var redirects = [];
request
.post('http://localhost:3003/addmovie')
.send({ name: 'Methos' })
.redirectData(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just noticed this, we shouldn't ever do this I dont wan't to buffer data and this will not support streams etc

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this patch should have been more focused, this is a completely separate thing, I just didn't read very well :D

.redirects(2)
.on('redirect', function(res){
redirects.push(res.headers.location);
})
.end(function(res){
var arr = [];
arr.push('//localhost:3003/movies/add');
redirects.should.eql(arr);
res.text.should.equal('movie Methos added');
done();
});
})
})

describe('with Authorization', function(){
it('should not redirect Authorization header',function(done){
request
.get('http://localhost:3003/private')
.set('Authorization', 'Basic dXNlcm5hbWU6cGFzcw==')
.end(function(res){
res.text.should.equal('not authed');
done();
});
})
it('should redirect Authorization header if specified',function(done){
request
.get('http://localhost:3003/private')
.set('Authorization', 'Basic dXNlcm5hbWU6cGFzcw==')
.redirectAuth(true)
.end(function(res){
res.text.should.equal('authed');
done();
});
})
})
})