1 | var expect = require('chai').expect;
|
2 | var pathRewriter = require('../lib/path-rewriter');
|
3 |
|
4 | describe('Path rewriting', function() {
|
5 | var rewriter;
|
6 | var result;
|
7 |
|
8 | describe('Configuration and usage', function() {
|
9 | beforeEach(function() {
|
10 | var config = {
|
11 | '^/api/old': '/api/new',
|
12 | '^/remove': '',
|
13 | 'invalid': 'path/new',
|
14 | '/valid': '/path/new',
|
15 | '/some/specific/path': '/awe/some/specific/path',
|
16 | '/some': '/awe/some'
|
17 | };
|
18 | rewriter = pathRewriter.create(config);
|
19 | });
|
20 |
|
21 | it('should rewrite path', function() {
|
22 | result = rewriter('/api/old/index.json');
|
23 | expect(result).to.equal('/api/new/index.json');
|
24 | });
|
25 |
|
26 | it('should remove path', function() {
|
27 | result = rewriter('/remove/old/index.json');
|
28 | expect(result).to.equal('/old/index.json');
|
29 | });
|
30 |
|
31 | it('should leave path intact', function() {
|
32 | result = rewriter('/foo/bar/index.json');
|
33 | expect(result).to.equal('/foo/bar/index.json');
|
34 | });
|
35 |
|
36 | it('should not rewrite path when config-key does not match url with test(regex)', function() {
|
37 | result = rewriter('/invalid/bar/foo.json');
|
38 | expect(result).to.equal('/path/new/bar/foo.json');
|
39 | expect(result).to.not.equal('/invalid/new/bar/foo.json');
|
40 | });
|
41 |
|
42 | it('should rewrite path when config-key does match url with test(regex)', function() {
|
43 | result = rewriter('/valid/foo/bar.json');
|
44 | expect(result).to.equal('/path/new/foo/bar.json');
|
45 | });
|
46 |
|
47 | it('should return first match when similar paths are configured', function() {
|
48 | result = rewriter('/some/specific/path/bar.json');
|
49 | expect(result).to.equal('/awe/some/specific/path/bar.json');
|
50 | });
|
51 |
|
52 | });
|
53 |
|
54 | describe('Invalid configuration', function() {
|
55 | var badFn;
|
56 |
|
57 | beforeEach(function() {
|
58 | badFn = function(config) {
|
59 | return function() {
|
60 | pathRewriter.create(config);
|
61 | };
|
62 | };
|
63 | });
|
64 |
|
65 | it('should return undefined when no config is provided', function() {
|
66 | expect((badFn())()).to.equal(undefined);
|
67 | expect((badFn(null)())).to.equal(undefined);
|
68 | expect((badFn(undefined)())).to.equal(undefined);
|
69 | });
|
70 |
|
71 | it('should throw when bad config is provided', function() {
|
72 | expect(badFn(123)).to.throw(Error);
|
73 | expect(badFn('abc')).to.throw(Error);
|
74 | expect(badFn(function() {})).to.throw(Error);
|
75 | expect(badFn([])).to.throw(Error);
|
76 | expect(badFn([1,2,3])).to.throw(Error);
|
77 | });
|
78 |
|
79 | it('should not throw when empty Object config is provided', function() {
|
80 | expect(badFn({})).to.not.throw(Error);
|
81 | });
|
82 |
|
83 | });
|
84 | });
|
85 |
|