Skip to content

Commit

Permalink
fix: create unit tests for sanitizePath plugin (#1352)
Browse files Browse the repository at this point in the history
* Remove unused utils.santizePath implementation
* Add test for sanitizePath plugin
  • Loading branch information
William Blankenship committed Aug 16, 2017
1 parent 004d685 commit 12714cf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
26 changes: 0 additions & 26 deletions lib/utils.js
Expand Up @@ -2,31 +2,6 @@

'use strict';

var assert = require('assert-plus');


/**
* Cleans up sloppy URL paths, like /foo////bar/// to /foo/bar.
* @public
* @method sanitizePath
* @param {String} path the HTTP resource path.
* @returns {String} Cleaned up form of path.
*/
function sanitizePath(path) {
assert.ok(path);

// Be nice like apache and strip out any //my//foo//bar///blah
path = path.replace(/\/\/+/g, '/');

// Kill a trailing '/'
if (path.lastIndexOf('/') === (path.length - 1) && path.length > 1) {
path = path.substr(0, path.length - 1);
}

return (path);
}


/**
* Return a shallow copy of the given object;
* @public
Expand Down Expand Up @@ -83,7 +58,6 @@ function mergeQs(obj1, obj2) {
///--- Exports

module.exports = {
sanitizePath: sanitizePath,
shallowCopy: shallowCopy,
mergeQs: mergeQs
};
44 changes: 44 additions & 0 deletions test/plugins/plugins.test.js
Expand Up @@ -6,6 +6,7 @@
var assert = require('chai').assert;
var restify = require('../../lib/index.js');
var restifyClients = require('restify-clients');
var sanitizePath = require('../../lib/plugins/pre/prePath.js');

// local files
var helper = require('../lib/helper');
Expand Down Expand Up @@ -258,4 +259,47 @@ describe('all other plugins', function () {
});
});
});

describe('sanitizePath', function () {
// Ensure it santizies potential edge cases correctly
var tests = {
input: [
'////foo////', //excess padding on both ends
'bar/foo/', // trailing slash
'bar/foo/////', // multiple trailing slashes
'foo////bar', // multiple slashes inbetween
'////foo', // multiple at beginning
'/foo/bar' // don't mutate
],
output: [
'/foo',
'bar/foo',
'bar/foo',
'foo/bar',
'/foo',
'/foo/bar'
],
description: [
'should clean excess padding on both ends',
'should clean trailing slash',
'should clean multiple trailing slashes',
'should clean multiple slashes inbetween',
'should clean multiple at beginning',
'dont mutate correct urls'
]
};

for (var i = 0; i < tests.input.length; i++) {
(function () {
var index = i;
it(tests.description[index], function (done) {
var req = { url: tests.input[index] };
sanitizePath()(req, null, function () {
assert.equal(req.url, tests.output[index]);
done();
});
});
}());
}
});
});

0 comments on commit 12714cf

Please sign in to comment.