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

Fixed #2575 #2738

Open
wants to merge 3 commits 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
43 changes: 36 additions & 7 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,46 @@ res.status = function status(code) {
* last: 'http://api.example.com/users?page=5'
* });
*
* @param {Object} links
* or
*
* res.links([
* {
* href: 'http://api.example.com/users?page=2',
* rel: 'next',
* title: 'next chapter',
* type: 'text/plain;charset=UTF-8'
* },
* {
* href: 'http://api.example.com/users?page=5',
* rel: 'last',
* title: 'the grand finale',
* type: 'video/webm'
* }
* ]);
* @param {Object|Array} links
* @return {ServerResponse}
* @public
*/

res.links = function(links){
var link = this.get('Link') || '';
if (link) link += ', ';
return this.set('Link', link + Object.keys(links).map(function(rel){
return '<' + links[rel] + '>; rel="' + rel + '"';
}).join(', '));
res.links = function (links) {
var link = this.get('Link') || '';
if (link) link += ', ';
if (Array.isArray(links)) {
link += Object.keys(links).map(function (i) {
return Object.keys(links[i]).map(function (item) {
if (item === 'href')
return '<' + links[i][item] + '>';
else
return item + '="' + links[i][item] + '"';
}).join('; ')
}).join(', ')
}
else {
link += Object.keys(links).map(function (rel) {
return '<' + links[rel] + '>; rel="' + rel + '"';
}).join(', ')
}
return this.set('Link', link);
};

/**
Expand Down
28 changes: 28 additions & 0 deletions test/res.links.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,33 @@ describe('res', function(){
.expect('Link', '<http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last", <http://api.example.com/users?page=1>; rel="prev"')
.expect(200, done);
})
it('should set Link header field with an Array', function (done) {
var app = express();

app.use(function (req, res) {
res.links([
{
href: 'http://api.example.com/users?page=2',
rel: 'next',
title: 'next chapter',
type: 'text/plain;charset=UTF-8'
},
{
href: 'http://api.example.com/users?page=5',
rel: 'last',
title: 'the grand finale',
type: 'video/webm'
}
]);

res.end();
});

request(app)
.get('/')
.expect('Link', '<http://api.example.com/users?page=2>; rel="next"; title="next chapter"; type="text/plain;charset=UTF-8",'
+ ' <http://api.example.com/users?page=5>; rel="last"; title="the grand finale"; type="video/webm"')
.expect(200, done);
})
})
})