route-parser icon indicating copy to clipboard operation
route-parser copied to clipboard

Doesn't correctly escape slashes

Open carmandrew opened this issue 7 years ago • 3 comments

const a = new RouteParser('/foo/:bar')
a.reverse({bar: 'hi/test'})
// Returns "/foo/hi/test"

a.reverse({bar: 'hi test'})
// Returns "/foo/hi%20test"

Either the first should return "/foo/hi%2ftest" (by calling EncodeURIComponent on each param) or the second should return "/foo/hi test"(so that the caller can encode the components). But with the current behavior, there is no way to encode a string like "foo hi/test" because there is no way to encode both the slash without double encoding the space (ie. into "%2520")

carmandrew avatar Jun 02 '17 23:06 carmandrew

Failing test case:

it('correctly escapes slashes when reversing routes', function () {
      var route = RouteParser('/things/:foo');
      assert.equal(route.reverse({ foo: 'foo/bar baz' }), '/things/foo%2fbar%20baz');
});

Currently, this returns:

  1) Route reverse correctly escapes slashes when reversing routes:
     AssertionError: expected '/things/foo/bar%20baz' to equal '/things/foo%2fbar%20baz'
      at Context.<anonymous> (test/test.js:185:14)

carmandrew avatar Jun 02 '17 23:06 carmandrew

?? Any help on this ??

AbhilashSrivastava avatar Nov 07 '17 12:11 AbhilashSrivastava

I recommend using this lib while this problem is not solved

https://github.com/pillarjs/path-to-regexp#compile-reverse-path-to-regexp

hiagolcm avatar Jun 15 '20 20:06 hiagolcm