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
balexandre patch 1684 #1702
Closed
Closed
balexandre patch 1684 #1702
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,143 @@ var mockRes = { | |
setHeader: function() {}, | ||
send: function() {} | ||
}; | ||
|
||
var mockResponse = function respond(req, res, next) { | ||
res.send(200); | ||
}; | ||
///--- Tests | ||
|
||
test('render route', function(t) { | ||
var server = restify.createServer(); | ||
server.get({ name: 'countries', path: '/countries' }, mockResponse); | ||
server.get({ name: 'country', path: '/countries/:name' }, mockResponse); | ||
server.get( | ||
{ name: 'cities', path: '/countries/:name/states/:state/cities' }, | ||
mockResponse | ||
); | ||
|
||
var countries = server.router.render('countries', {}); | ||
t.equal(countries, '/countries'); | ||
|
||
var country = server.router.render('country', { name: 'Australia' }); | ||
t.equal(country, '/countries/Australia'); | ||
|
||
var cities = server.router.render('cities', { | ||
name: 'Australia', | ||
state: 'New South Wales' | ||
}); | ||
t.equal(cities, '/countries/Australia/states/New%20South%20Wales/cities'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('render route (missing params)', function(t) { | ||
var server = restify.createServer(); | ||
server.get( | ||
{ name: 'cities', path: '/countries/:name/states/:state/cities' }, | ||
mockResponse | ||
); | ||
|
||
try { | ||
server.router.render('cities', { name: 'Australia' }); | ||
} catch (ex) { | ||
t.equal(ex, 'Error: Route <cities> is missing parameter <state>'); | ||
} | ||
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. we should throw if we catch branch doesn't run |
||
|
||
t.end(); | ||
}); | ||
|
||
test('GH #704: render route (special charaters)', function(t) { | ||
var server = restify.createServer(); | ||
server.get({ name: 'my-route', path: '/countries/:name' }, mockResponse); | ||
|
||
var link = server.router.render('my-route', { name: 'Australia' }); | ||
t.equal(link, '/countries/Australia'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('GH #704: render route (with sub-regex param)', function(t) { | ||
var server = restify.createServer(); | ||
server.get( | ||
{ | ||
name: 'my-route', | ||
path: '/countries/:code([A-Z]{2,3})' | ||
}, | ||
mockResponse | ||
); | ||
|
||
var link = server.router.render('my-route', { code: 'FR' }); | ||
t.equal(link, '/countries/FR'); | ||
|
||
link = server.router.render('my-route', { code: '111' }); | ||
t.equal(link, '/countries/111'); | ||
t.end(); | ||
}); | ||
|
||
test('GH-796: render route (with multiple sub-regex param)', function(t) { | ||
var server = restify.createServer(); | ||
server.get( | ||
{ | ||
name: 'my-route', | ||
path: '/countries/:code([A-Z]{2,3})/:area([0-9]+)' | ||
}, | ||
mockResponse | ||
); | ||
|
||
var link = server.router.render('my-route', { code: '111', area: 42 }); | ||
t.equal(link, '/countries/111/42'); | ||
t.end(); | ||
}); | ||
|
||
test('render route (with encode)', function(t) { | ||
var server = restify.createServer(); | ||
server.get({ name: 'my-route', path: '/countries/:name' }, mockResponse); | ||
|
||
var link = server.router.render('my-route', { name: 'Trinidad & Tobago' }); | ||
t.equal(link, '/countries/Trinidad%20%26%20Tobago'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('render route (query string)', function(t) { | ||
var server = restify.createServer(); | ||
server.get({ name: 'country', path: '/countries/:name' }, mockResponse); | ||
|
||
var country1 = server.router.render( | ||
'country', | ||
{ | ||
name: 'Australia' | ||
}, | ||
{ | ||
state: 'New South Wales', | ||
'cities/towns': 5 | ||
} | ||
); | ||
|
||
t.equal( | ||
country1, | ||
'/countries/Australia?state=New%20South%20Wales&cities%2Ftowns=5' | ||
); | ||
|
||
var country2 = server.router.render( | ||
'country', | ||
{ | ||
name: 'Australia' | ||
}, | ||
{ | ||
state: 'NSW & VIC', | ||
'cities&towns': 5 | ||
} | ||
); | ||
|
||
t.equal( | ||
country2, | ||
'/countries/Australia?state=NSW%20%26%20VIC&cities%26towns=5' | ||
); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('mounts a route', function(t) { | ||
function handler(req, res, next) { | ||
res.send('Hello world'); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We could add an example here, the new API site would render it to the website.
Like: