Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create unit tests for sanitizePath #1352

Merged
merged 11 commits into from Aug 16, 2017
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();
});
});
}());
}
});
});