Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Permit routes with slashes #55

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/url_template.dart
Expand Up @@ -4,6 +4,7 @@ import 'url_matcher.dart';

final _specialChars = new RegExp(r'[\\()$^.+[\]{}|]');
final _paramPattern = r'([^/?]+)';
final _paramWithSlashesPattern = r'([^?]+)';

/**
* A reversible URL template class that can match/parse and reverse URL
Expand Down Expand Up @@ -52,7 +53,7 @@ class UrlTemplate implements UrlMatcher {
replaceAllMapped(_specialChars, (m) => r'\' + m.group(0));
_fields = <String>[];
_chunks = [];
var exp = new RegExp(r':(\w+)');
var exp = new RegExp(r':(\w+\*?)');
StringBuffer sb = new StringBuffer('^');
int start = 0;
exp.allMatches(template).forEach((Match m) {
Expand All @@ -62,7 +63,11 @@ class UrlTemplate implements UrlMatcher {
_chunks.add(txt);
_chunks.add((Map params) => params != null ? params[paramName] : null);
sb.write(txt);
sb.write(_paramPattern);
if (paramName.endsWith(r'*')) {
sb.write(_paramWithSlashesPattern);
} else {
sb.write(_paramPattern);
}
start = m.end;
});
if (start != template.length) {
Expand Down
22 changes: 21 additions & 1 deletion test/url_template_test.dart
Expand Up @@ -91,5 +91,25 @@ main() {
'c': 'baz',
}), '/foo/bar/baz/tail');
});

test('should conditionally allow slashes in parameters', () {
var tmpl = new UrlTemplate('/foo/:bar');
expect(tmpl.match('/foo/123/456'),
new UrlMatch('/foo/123', '/456', {
'bar': '123'
}));

tmpl = new UrlTemplate('/foo/:bar*');
expect(tmpl.match('/foo/123/456'),
new UrlMatch('/foo/123/456', '', {
'bar*': '123/456'
}));

tmpl = new UrlTemplate('/foo/:bar*/baz');
expect(tmpl.match('/foo/123/456/baz'),
new UrlMatch('/foo/123/456/baz', '', {
'bar*': '123/456'
}));
});
});
}
}