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

fixes #156 - do not append unexpected trailing slashes to routes with… #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion dev/src/pattern_lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,18 @@
if (! TOKENS.OS.trail) {
TOKENS.OS.trail = new RegExp('(?:'+ TOKENS.OS.id +')+$');
}

// we need to replace the query separator with something else
// because .replace(TOKENS.OS.rRestore, '/') would place incorrect
// trailing slash and trailing slashes have meaning
if (! TOKENS.OS.fixQueryOptional) {
TOKENS.OS.fixQueryOptional = new RegExp('(:\\?|\\{\\?)');
}
return pattern
.replace(TOKENS.OS.fixQueryOptional, '__TRAILING_QUERY__$1')
.replace(TOKENS.OS.rgx, TOKENS.OS.save)
.replace(PARAMS_REGEXP, replaceFn)
.replace(TOKENS.OS.trail, '') // remove trailing
.replace('__TRAILING_QUERY__', '') // remove saving token
.replace(TOKENS.OS.rRestore, '/'); // add slash between segments
}

Expand Down
27 changes: 26 additions & 1 deletion dev/tests/spec/interpolate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,36 @@ describe('Route.interpolate()', function(){
}).toThrow( 'Generated string doesn\'t validate against `Route.rules`.' );
});

it('should replace query segments', function(){
it('should replace required query segments', function(){
var a = crossroads.addRoute('/{foo}/{?query}');
expect( a.interpolate({foo: 'lorem', query: {some: 'test'}}) ).toEqual( '/lorem/?some=test' );
expect( a.interpolate({foo: 'dolor-sit', query: {multiple: 'params', works: 'fine'}}) ).toEqual( '/dolor-sit/?multiple=params&works=fine' );
expect( a.interpolate({foo: 'amet', query: {multiple: ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet/?multiple=paramsWith&multiple=sameName&works=fine2' );
expect( a.interpolate({foo: 'amet2', query: {"multiple[]": ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet2/?multiple[]=paramsWith&multiple[]=sameName&works=fine2' );
});

it('should replace optional query segments', function(){
var a = crossroads.addRoute('/{foo}/:?query:');
expect( a.interpolate({foo: 'lorem', query: {some: 'test'}}) ).toEqual( '/lorem/?some=test' );
expect( a.interpolate({foo: 'dolor-sit', query: {multiple: 'params', works: 'fine'}}) ).toEqual( '/dolor-sit/?multiple=params&works=fine' );
expect( a.interpolate({foo: 'amet', query: {multiple: ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet/?multiple=paramsWith&multiple=sameName&works=fine2' );
expect( a.interpolate({foo: 'amet2', query: {"multiple[]": ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet2/?multiple[]=paramsWith&multiple[]=sameName&works=fine2' );
});

it('should replace optional query segments without last slash', function(){
var a = crossroads.addRoute('/{foo}:?query:');
expect( a.interpolate({foo: 'lorem', query: {some: 'test'}, x:'x', y:'y', z:'z'}) ).toEqual( '/lorem?some=test' );
expect( a.interpolate({foo: 'dolor-sit', query: {multiple: 'params', works: 'fine'}}) ).toEqual( '/dolor-sit?multiple=params&works=fine' );
expect( a.interpolate({foo: 'amet', query: {multiple: ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet?multiple=paramsWith&multiple=sameName&works=fine2' );
expect( a.interpolate({foo: 'amet2', query: {"multiple[]": ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet2?multiple[]=paramsWith&multiple[]=sameName&works=fine2' );
});

it('should replace required query segments without last slash', function(){
var a = crossroads.addRoute('/{foo}{?query}');
expect( a.interpolate({foo: 'lorem', query: {some: 'test'}, x:'x', y:'y', z:'z'}) ).toEqual( '/lorem?some=test' );
expect( a.interpolate({foo: 'dolor-sit', query: {multiple: 'params', works: 'fine'}}) ).toEqual( '/dolor-sit?multiple=params&works=fine' );
expect( a.interpolate({foo: 'amet', query: {multiple: ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet?multiple=paramsWith&multiple=sameName&works=fine2' );
expect( a.interpolate({foo: 'amet2', query: {"multiple[]": ['paramsWith', 'sameName'], works: 'fine2'}}) ).toEqual( '/amet2?multiple[]=paramsWith&multiple[]=sameName&works=fine2' );
});

});