11

with supertest, I can test the redirection code 302

var request = require('supertest');
var app = require('../server').app;

describe('test route', function(){
  it('return 302', function(done){
    request(app)
      .get('/fail_id')
      .expect(302, done);
  });
  it('redirect to /');
});

how I can test the url objetive to redirect ?

JuanPablo
  • 23,792
  • 39
  • 118
  • 164

2 Answers2

12

@JuanPablo's answer is on the right path (pun intended), but it will match any location with / anywhere.

You want to make sure that there is nothing following the / by using the line-end char $, and that the chars previous to the / are what you expect. A quick-and-dirty example follows:

it('redirect to /', function(done){
  request(app)
    .get('/fail_id')
    .expect('Location', /\.com\/$/, done);
});
Zachary Ryan Smith
  • 2,688
  • 1
  • 20
  • 30
7
  it('redirect to /', function(done){
    request(app)
      .get('/fail_id')
      .expect('Location', /\//, done);
  });
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
JuanPablo
  • 23,792
  • 39
  • 118
  • 164