Skip to content

Commit

Permalink
Adding tests (#4)
Browse files Browse the repository at this point in the history
Adding tests using mocha, chai, and sinon.
Using nyc for test coverage.
  • Loading branch information
capellini authored and kapouer committed Jan 16, 2019
1 parent e772892 commit e04e3bb
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.nyc_output/
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"type": "git",
"url": "git://github.com/kapouer/express-urlrewrite"
},
"scripts": {
"test": "NODE_ENV=test nyc mocha"
},
"version": "1.2.0",
"keywords": [
"express",
Expand All @@ -20,5 +23,12 @@
"dependencies": {
"debug": "*",
"path-to-regexp": "^1.0.3"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0",
"nyc": "^13.1.0",
"sinon": "^7.2.2",
"sinon-chai": "^3.3.0"
}
}
197 changes: 197 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
var chai = require('chai');
var sinon = require('sinon');
var sinonChai = require('sinon-chai');

chai.use(sinonChai);
var { expect } = chai;
var rewrite = require('../index');

describe('rewrite tests', function () {
var rewriteFunc;
var req;
var res;
var next;

beforeEach(function () {
req = {
url: '/',
params: {},
query: {}
};
res = {};
next = sinon.fake();
});

afterEach(function () {
sinon.restore();
});

function testNegativeCase(rewriteFunc, req, res, next) {
req.url = '/otherPath';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/otherPath');
expect(next).to.have.been.calledWith();
}

describe('rewriting using a regular expression', function () {
beforeEach(function () {
rewriteFunc = rewrite(/^\/i(\w+)/, '/items/$1');
});

it('rewrites if path matches', function () {
req.url = '/i123';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/items/123');
expect(next).to.have.been.calledWith();
});

it('does not rewrite if path does not match', function () {
testNegativeCase(rewriteFunc, req, res, next);
});
});

describe('rewriting using route parameters (capture group)', function () {
beforeEach(function () {
rewriteFunc = rewrite('/:src..:dst', '/commits/$1/to/$2');
});

it('rewrites if path matches', function () {
req.url = '/foo..bar';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/commits/foo/to/bar');
expect(next).to.have.been.calledWith();
});

it('does not rewrite if path does not match', function () {
testNegativeCase(rewriteFunc, req, res, next);
});
});

describe('rewriting using route parameters (capture group)', function () {
beforeEach(function () {
rewriteFunc = rewrite('/:src..:dst', '/commits/:src/to/:dst');
});

it('rewrites if path matches', function () {
req.url = '/foo..bar';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/commits/foo/to/bar');
expect(next).to.have.been.calledWith();
});


it('does not rewrite if path does not match', function () {
testNegativeCase(rewriteFunc, req, res, next);
});
});

describe('rewriting using route parameters (named parameters)', function () {
beforeEach(function () {
rewriteFunc = rewrite('/:src..:dst', '/commits/:src/to/:dst');
});

it('rewrites if path matches', function () {
req.url = '/foo..bar';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/commits/foo/to/bar');
expect(next).to.have.been.calledWith();
});

it('does not rewrite if path does not match', function () {
testNegativeCase(rewriteFunc, req, res, next);
});
});

describe('using the wildcard * to soak up several segments', function () {
beforeEach(function () {
rewriteFunc = rewrite('/js/*', '/public/assets/js/$1');
});

it('rewrites if path matches', function () {
req.url = '/js/vendor/jquery.js';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/public/assets/js/vendor/jquery.js');
expect(next).to.have.been.calledWith();
});

it('does not rewrite if path does not match', function () {
testNegativeCase(rewriteFunc, req, res, next);
});
});

describe('rewriting the url using the original query string', function () {
beforeEach(function () {
rewriteFunc = rewrite('/file\\?param=:param', '/file/:param');
});

it('rewrites if path matches', function () {
req.url = '/file?param=file1';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/file/file1');
expect(next).to.have.been.calledWith();
});

it('does not rewrite if path does not match', function () {
testNegativeCase(rewriteFunc, req, res, next);
});
});

describe('adding query parameters to the query object output', function () {
beforeEach(() => {
rewriteFunc = rewrite('/path', '/anotherpath?param=some');
});

it('adds to query parameters if path matches', () => {
req.url = '/path';

rewriteFunc(req, res, next);

expect(req.url).to.equal('/anotherpath?param=some');
expect(req.query).to.deep.equal({ param: 'some' });
expect(next).to.have.been.calledWith();
});

it('does not add to query parameters if path does not match', () => {
testNegativeCase(rewriteFunc, req, res, next);
expect(req.query).to.deep.equal({});
});
});

it('can be used with route middleware (capture group)', function () {
rewriteFunc = rewrite('/rewritten/$1');

req.url = '/route/route1';
req.params = ['', 'route1'];

rewriteFunc(req, res, next);

expect(req.url).to.equal('/rewritten/route1');
expect(next).to.have.been.calledWith('route');
});

it('can be used with route middleware (named parameters)', function () {
rewriteFunc = rewrite('/rewritten/:var');

req.url = '/route/route1';
req.params = { var: 'route1' };

rewriteFunc(req, res, next);

expect(req.url).to.equal('/rewritten/route1');
expect(next).to.have.been.calledWith('route');
});
});

0 comments on commit e04e3bb

Please sign in to comment.