-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Support for ? in paths #214
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
require('./helper'); | ||
var Path = require('../modules/helpers/Path'); | ||
|
||
describe('Path.extractParamNames', function () { | ||
describe('when a pattern contains no dynamic segments', function () { | ||
it('returns an empty array', function () { | ||
expect(Path.extractParamNames('a/b/c')).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('when a pattern contains :a and :b dynamic segments', function () { | ||
it('returns the correct names', function () { | ||
expect(Path.extractParamNames('/comments/:a/:b/edit')).toEqual([ 'a', 'b' ]); | ||
}); | ||
}); | ||
|
||
describe('when a pattern has a *', function () { | ||
it('uses the name "splat"', function () { | ||
expect(Path.extractParamNames('/files/*.jpg')).toEqual([ 'splat' ]); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Path.extractParams', function () { | ||
describe('when a pattern does not have dynamic segments', function () { | ||
var pattern = 'a/b/c'; | ||
|
@@ -19,11 +39,11 @@ describe('Path.extractParams', function () { | |
}); | ||
|
||
describe('when a pattern has dynamic segments', function () { | ||
var pattern = 'comments/:id/edit'; | ||
var pattern = 'comments/:id.:ext/edit'; | ||
|
||
describe('and the path matches', function () { | ||
it('returns an object with the params', function () { | ||
expect(Path.extractParams(pattern, 'comments/abc/edit')).toEqual({ id: 'abc' }); | ||
expect(Path.extractParams(pattern, 'comments/abc.js/edit')).toEqual({ id: 'abc', ext: 'js' }); | ||
}); | ||
}); | ||
|
||
|
@@ -35,7 +55,7 @@ describe('Path.extractParams', function () { | |
|
||
describe('and the path matches with a segment containing a .', function () { | ||
it('returns an object with the params', function () { | ||
expect(Path.extractParams(pattern, 'comments/foo.bar/edit')).toEqual({ id: 'foo.bar' }); | ||
expect(Path.extractParams(pattern, 'comments/foo.bar/edit')).toEqual({ id: 'foo', ext: 'bar' }); | ||
}); | ||
}); | ||
}); | ||
|
@@ -73,38 +93,37 @@ describe('Path.extractParams', function () { | |
}); | ||
|
||
describe('when a pattern has a *', function () { | ||
var pattern = '/files/*.jpg'; | ||
|
||
describe('and the path matches', function () { | ||
it('returns an object with the params', function () { | ||
expect(Path.extractParams(pattern, '/files/my/photo.jpg')).toEqual({ splat: 'my/photo' }); | ||
expect(Path.extractParams('/files/*', '/files/my/photo.jpg')).toEqual({ splat: 'my/photo.jpg' }); | ||
expect(Path.extractParams('/files/*', '/files/my/photo.jpg.zip')).toEqual({ splat: 'my/photo.jpg.zip' }); | ||
expect(Path.extractParams('/files/*.jpg', '/files/my/photo.jpg')).toEqual({ splat: 'my/photo' }); | ||
}); | ||
}); | ||
|
||
describe('and the path does not match', function () { | ||
it('returns null', function () { | ||
expect(Path.extractParams(pattern, '/files/my/photo.png')).toBe(null); | ||
expect(Path.extractParams('/files/*.jpg', '/files/my/photo.png')).toBe(null); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Path.extractParamNames', function () { | ||
describe('when a pattern contains no dynamic segments', function () { | ||
it('returns an empty array', function () { | ||
expect(Path.extractParamNames('a/b/c')).toEqual([]); | ||
}); | ||
}); | ||
describe('when a pattern has a ?', function () { | ||
var pattern = '/archive/?:name?'; | ||
|
||
describe('when a pattern contains :a and :b dynamic segments', function () { | ||
it('returns the correct names', function () { | ||
expect(Path.extractParamNames('/comments/:a/:b/edit')).toEqual([ 'a', 'b' ]); | ||
describe('and the path matches', function () { | ||
it('returns an object with the params', function () { | ||
expect(Path.extractParams(pattern, '/archive')).toEqual({ name: undefined }); | ||
expect(Path.extractParams(pattern, '/archive/')).toEqual({ name: undefined }); | ||
expect(Path.extractParams(pattern, '/archive/foo')).toEqual({ name: 'foo' }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about /archivefoo? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, good catch. Added. |
||
expect(Path.extractParams(pattern, '/archivefoo')).toEqual({ name: 'foo' }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when a pattern has a *', function () { | ||
it('uses the name "splat"', function () { | ||
expect(Path.extractParamNames('/files/*.jpg')).toEqual([ 'splat' ]); | ||
describe('and the path does not match', function () { | ||
it('returns null', function () { | ||
expect(Path.extractParams(pattern, '/archiv')).toBe(null); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
@@ -151,12 +170,22 @@ describe('Path.injectParams', function () { | |
}); | ||
}); | ||
}); | ||
|
||
describe('when a pattern has multiple splats', function () { | ||
it('returns the correct path', function () { | ||
expect(Path.injectParams('/a/*/c/*', { splat: [ 'b', 'd' ] })).toEqual('/a/b/c/d'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would I use splats instead of params? |
||
}); | ||
}); | ||
}); | ||
|
||
describe('Path.extractQuery', function () { | ||
describe('when the path contains a query string', function () { | ||
it('returns the parsed query object', function () { | ||
expect(Path.extractQuery('/a/b/c?id=def&show=true')).toEqual({ id: 'def', show: 'true' }); | ||
expect(Path.extractQuery('/?id=def&show=true')).toEqual({ id: 'def', show: 'true' }); | ||
}); | ||
|
||
it('properly handles arrays', function () { | ||
expect(Path.extractQuery('/?id%5B%5D=a&id%5B%5D=b')).toEqual({ id: [ 'a', 'b' ] }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used Namely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. express, hapi, mach all use qs instead of querystring. Also, PHP, Rails, etc. Core node is the outlier here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
}); | ||
}); | ||
|
||
|
@@ -177,6 +206,10 @@ describe('Path.withQuery', function () { | |
it('appends the query string', function () { | ||
expect(Path.withQuery('/a/b/c', { id: 'def' })).toEqual('/a/b/c?id=def'); | ||
}); | ||
|
||
it('merges two query strings', function () { | ||
expect(Path.withQuery('/path?a=b', { c: [ 'd', 'e' ]})).toEqual('/path?a=b&c%5B0%5D=d&c%5B1%5D=e'); | ||
}); | ||
}); | ||
|
||
describe('Path.normalize', function () { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
/comments/:id/edit
, can we still havecomments/foo.bar/edit
or does the.
mess it up now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
:id
does not matchfoo.bar
.