Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 487
JuanPablo Avatar asked Dec 05 '14 18:12

JuanPablo


2 Answers

@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);
});
like image 54
Zachary Ryan Smith Avatar answered Oct 07 '22 23:10

Zachary Ryan Smith


  it('redirect to /', function(done){
    request(app)
      .get('/fail_id')
      .expect('Location', /\//, done);
  });
like image 33
JuanPablo Avatar answered Oct 07 '22 23:10

JuanPablo