From 12714cfce5048c65b4256df660766e863578b90a Mon Sep 17 00:00:00 2001 From: William Blankenship Date: Wed, 16 Aug 2017 14:29:58 -0700 Subject: [PATCH] fix: create unit tests for sanitizePath plugin (#1352) * Remove unused utils.santizePath implementation * Add test for sanitizePath plugin --- lib/utils.js | 26 --------------------- test/plugins/plugins.test.js | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index a9ca38a84..9ac85cf61 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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 @@ -83,7 +58,6 @@ function mergeQs(obj1, obj2) { ///--- Exports module.exports = { - sanitizePath: sanitizePath, shallowCopy: shallowCopy, mergeQs: mergeQs }; diff --git a/test/plugins/plugins.test.js b/test/plugins/plugins.test.js index 4e5d91080..3775fadbe 100644 --- a/test/plugins/plugins.test.js +++ b/test/plugins/plugins.test.js @@ -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'); @@ -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(); + }); + }); + }()); + } + }); });