JuanPablo
JuanPablo

Reputation: 24794

supertest: test the redirection url

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 ?

Upvotes: 11

Views: 6622

Answers (2)

Zachary Ryan Smith
Zachary Ryan Smith

Reputation: 2768

@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);
});

Upvotes: 12

JuanPablo
JuanPablo

Reputation: 24794

  it('redirect to /', function(done){
    request(app)
      .get('/fail_id')
      .expect('Location', /\//, done);
  });

Upvotes: 7

Related Questions