Fixed redirect handling. Includes tests #42

Open
wants to merge 2 commits into
from
View
5 lib/test.js
@@ -120,7 +120,10 @@ Test.prototype.expect = function(a, b, c){
Test.prototype.end = function(fn){
var self = this;
var end = Request.prototype.end;
- end.call(this, function(res){
+ end.call(this, function(err, res){
@tj
Sloth member
tj added a line comment Dec 6, 2012

we should check this error, ignoring it wont be good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if(err) {
+ return self.emit('error', err);
+ }
self.assert(res, fn);
});
return this;
View
65 test/supertest.js
@@ -3,7 +3,8 @@ var request = require('..')
, https = require('https')
, fs = require('fs')
, path = require('path')
- , express = require('express');
+ , express = require('express')
+ , should = require('should');
describe('request(app)', function(){
it('should fire up the app on an ephemeral port', function(done){
@@ -119,6 +120,68 @@ describe('request(app)', function(){
.expect(302, done);
})
+ it('should redirect many times', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.redirect('/redirect1');
+ });
+
+ app.get('/redirect1', function(req, res){
+ res.redirect('/redirect2');
+ });
+
+ app.get('/redirect2', function(req, res){
+ res.redirect('/redirect3');
+ });
+
+ app.get('/redirect3', function(req, res){
+ res.send(200, 'Redirected 3 times');
+ });
+
+ request(app)
+ .get('/')
+ .redirects(5)
+ .end(function(err, res) {
+ res.should.have.status(200);
+ res.redirects.should.have.length(3);
+ res.text.should.equal('Redirected 3 times');
+ done()
+ });
+ })
+
+ it('should throw an error during redirects', function(done){
+ var app = express();
+
+ app.get('/', function(req, res){
+ res.redirect('/redirect1');
+ });
+
+ app.get('/redirect1', function(req, res){
+ res.redirect('/redirect2');
+ });
+
+ app.get('/redirect2', function(req, res){
+ setTimeout(function() {
+ res.send(200, 'Should not see me!');
+ }, 200);
+ });
+
+ var timeout = 10;
+
+ request(app)
+ .get('/')
+ .timeout(timeout)
+ .redirects(5)
+ .on('error', function(err) {
+ err.timeout.should.equal(timeout);
+ done();
+ })
+ .end(function(err, res) {
+ should.fail('There was supposed to be an error');
+ });
+ })
+
describe('.expect(status[, fn])', function(){
it('should assert the response status', function(done){
var app = express();