Supertest: check redirect URL

with supertest, I can check the 302 redirect code

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 can i check the redirect url?

+5
source share
2 answers
  it('redirect to /', function(done){ request(app) .get('/fail_id') .expect('Location', /\//, done); }); 
+4
source

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

You want to make sure that there is nothing after / using the end of the string char $ and that the characters preceding / are what you expect. Below is a quick and dirty example:

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

Source: https://habr.com/ru/post/1208454/


All Articles