Comparing version 0.0.4 to 0.0.5
@@ -113,1 +113,37 @@ "use strict"; | ||
}; | ||
exports.jsonp = function (req, res) { | ||
var p = parse(req.url), | ||
code = 200, | ||
callback = '', | ||
query = qs.parse(p.query), | ||
content = { "echo": true }, | ||
json = ''; | ||
if (query.callback) { | ||
callback = query.callback; | ||
delete query.callback; | ||
} | ||
if (req.body) { | ||
query = req.body; | ||
} | ||
if (Object.keys(query).length) { | ||
content = query; | ||
} | ||
if (!callback) { | ||
code = 403; | ||
json = JSON.stringify('{ "echo": "error, no callback" }'); | ||
} else { | ||
json = callback + '(' + JSON.stringify(content) + ');'; | ||
} | ||
res.writeHead(code, { | ||
'Content-type': 'application/json' | ||
}); | ||
res.end(json); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"author": "Dav Glass <davglass@gmail.com>", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"dependencies": { | ||
@@ -8,0 +8,0 @@ }, |
@@ -31,2 +31,3 @@ EchoEcho | ||
* `json` - Send query parameters or POST parameters and get them back as JSON | ||
* `jsonp` - Send query parameters or POST parameters and get them back as JSON (pass a GET param of `callback=[yourcallback]` | ||
@@ -33,0 +34,0 @@ Using in Your Server |
@@ -308,2 +308,72 @@ var vows = require('vows'), | ||
}, | ||
"and get default jsonp": { | ||
topic: function() { | ||
fetch({ | ||
method: 'GET', | ||
path: '/foo/bar/baz/echo/jsonp?callback=baz' | ||
}, this.callback); | ||
}, | ||
"with query body": function(topic) { | ||
assert.equal(topic.code, 200); | ||
assert.equal(topic.body, 'baz({"echo":true});'); | ||
} | ||
}, | ||
"and post default jsonp": { | ||
topic: function() { | ||
fetch({ | ||
method: 'POST', | ||
path: '/foo/bar/baz/echo/jsonp?callback=foo' | ||
}, this.callback); | ||
}, | ||
"with query body": function(topic) { | ||
assert.equal(topic.code, 200); | ||
assert.equal(topic.body, 'foo({"echo":true});'); | ||
} | ||
}, | ||
"and get custom jsonp": { | ||
topic: function() { | ||
var self = this, | ||
url = '/foo/bar/baz/echo/jsonp?callback=yo&foo=bar&baz=world'; | ||
fetch({ | ||
method: 'GET', | ||
path: url | ||
}, function(err, data) { | ||
var q = parse(url); | ||
var payload = qs.parse(q.query); | ||
var callback = payload.callback; | ||
delete payload.callback; | ||
data.expected = callback + '(' + JSON.stringify(payload) + ');'; | ||
self.callback(err, data); | ||
}); | ||
}, | ||
"with query body": function(topic) { | ||
assert.equal(topic.code, 200); | ||
assert.equal(topic.headers['content-type'], 'application/json'); | ||
assert.equal(topic.body, topic.expected); | ||
} | ||
}, | ||
"and post custom jsonp": { | ||
topic: function() { | ||
var self = this, | ||
url = '/foo/bar/baz/echo/jsonp?callback=yoyo&foo=bar&baz=world&do=not'; | ||
fetch({ | ||
method: 'POST', | ||
path: url | ||
}, function(err, data) { | ||
var q = parse(url); | ||
var payload = qs.parse(q.query); | ||
var callback = payload.callback; | ||
delete payload.callback; | ||
data.expected = callback + '(' + JSON.stringify(payload) + ');'; | ||
self.callback(err, data); | ||
}); | ||
}, | ||
"with query body": function(topic) { | ||
assert.equal(topic.code, 200); | ||
assert.equal(topic.headers['content-type'], 'application/json'); | ||
assert.equal(topic.body, topic.expected); | ||
} | ||
}, | ||
"and delay 3 seconds": { | ||
@@ -310,0 +380,0 @@ topic: function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22616
557
93
22