Skip to content

Commit

Permalink
support array callback
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Jul 20, 2014
1 parent d236dba commit 628d5a9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -42,7 +42,7 @@ test-travis: install
test-all: install jshint test cov

autod: install
@./node_modules/.bin/autod -w --prefix "~"
@./node_modules/.bin/autod -w --prefix "~" -e demo.js
@$(MAKE) install

contributors: install
Expand Down
3 changes: 1 addition & 2 deletions index.js
Expand Up @@ -24,8 +24,7 @@ function jsonp(app, options) {

Object.defineProperty(app.context, 'jsonp', {
set: function (obj) {
var cb = this.query[callback];
if (typeof cb !== 'string' || cb === '') {
if (!this.query[callback]) {
return this.body = obj;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -21,6 +21,7 @@
"istanbul-harmony": "*",
"jshint": "*",
"koa": "~0.8.1",
"koa-qs": "~1.0.0",
"mocha": "*",
"should": "~4.0.4",
"supertest": "~0.13.0"
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.js
Expand Up @@ -16,6 +16,7 @@

var should = require('should');
var koa = require('koa');
var qs = require('koa-qs');
var request = require('supertest');
var jsonp = require('../');

Expand Down Expand Up @@ -63,6 +64,22 @@ describe('index.test.js', function () {
.expect(200, done);
});

it('should send jsonp response with array callback', function (done) {
var app = koa();
qs(app);
jsonp(app);
app.use(function* () {
this.jsonp = {foo: 'bar'};
});

request(app.listen())
.get('/foo.json?callback=fn&callback=cb')
.expect('Content-Type', 'application/javascript')
.expect('X-Content-Type-Options', 'nosniff')
.expect('/**/ typeof fn === \'function\' && fn({"foo":"bar"});')
.expect(200, done);
});

it('should send number response', function (done) {
var app = koa();
jsonp(app);
Expand Down

0 comments on commit 628d5a9

Please sign in to comment.