aboutsummaryrefslogtreecommitdiff
path: root/node_modules/express-validator/test/regexRouteTest.js
blob: 1638e94876f0c21df6294ca0b935a37ad90592aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var chai = require('chai');
var expect = chai.expect;
var request = require('supertest');

var app;
var errorMessage = 'Parameter is not a 3 digit integer';

function validation(req, res) {
  req.assert(0, errorMessage).len(3, 3).isInt();

  var errors = req.validationErrors();
  if (errors) {
    return res.send(errors);
  }
  res.send([req.params[0]]);
}

function fail(body) {
  expect(body).to.have.length(2);
  expect(body[0]).to.have.property('msg', errorMessage);
}

function pass(body) {
  expect(body[0]).to.equal('123');
}

function testRoute(path, test, done) {
  request(app)
    .get(path)
    .end(function(err, res) {
      test(res.body);
      done();
    });
}

// This before() is required in each set of tests in
// order to use a new validation function in each file
before(function() {
  delete require.cache[require.resolve('./helpers/app')];
  app = require('./helpers/app')(validation);
});

describe('Express routes can be defined using regular expressions', function() {
  it('should return a success when regex route is validated', function(done) {
    testRoute('/test123', pass, done);
  });

  it('should return an error when regex route is not validated', function(done) {
    testRoute('/test0123', fail, done);
  });
});