Skip to content

Commit

Permalink
Added typeof check to JSONP callback
Browse files Browse the repository at this point in the history
  • Loading branch information
mbjordan committed Mar 17, 2015
1 parent 69169f9 commit 1c70c8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/formatters/jsonp.js
Expand Up @@ -25,7 +25,8 @@ function formatJSONP(req, res, body) {
var cb = req.query.callback || req.query.jsonp;
var data;
if (cb) {
data = cb + '(' + JSON.stringify(body) + ');';
data = 'typeof ' + cb + ' === \'function\' && ' +
cb + '(' + JSON.stringify(body) + ');';
} else {
data = JSON.stringify(body);
}
Expand Down
15 changes: 15 additions & 0 deletions test/client.test.js
Expand Up @@ -100,6 +100,7 @@ before(function (callback) {
});

SERVER.use(restify.acceptParser(['json', 'text/plain']));
SERVER.use(restify.jsonp()); // Added for GH-776
SERVER.use(restify.dateParser());
SERVER.use(restify.authorizationParser());
SERVER.use(restify.queryParser());
Expand Down Expand Up @@ -195,6 +196,20 @@ test('GET json', function (t) {
});
});

test('GH-776 GET jsonp', function (t) {
// Using variables here to keep lines under 80 chars
var jsonpUrl = '/json/jsonp?callback=testCallback';
var expectedResult = 'typeof testCallback === \'function\' && ' +
'testCallback({"hello":"jsonp"});';

JSON_CLIENT.get(jsonpUrl, function (err, req, res) {
t.ifError(err);
t.ok(req);
t.ok(res);
t.equal(res.body, expectedResult);
t.end();
});
});

test('GH-388 GET json, but really HTML', function (t) {
JSON_CLIENT.get('/json/boom', function (err, req, res, obj) {
Expand Down

0 comments on commit 1c70c8a

Please sign in to comment.