Permalink
Browse files

Updated documentation to reflect new jsonp behaviour

Added json renderer catchall to handle json/jsonp requests
Added method to extract JSONP object body
Added tests for jsonp
Added tests for jsonp
  • Loading branch information...
1 parent 68f960c commit d90c797a23333a562ff7e8babac4835a9fd06f2c @cblanc cblanc committed Feb 27, 2014
@@ -23,10 +23,12 @@ block content
h4 http://api.postcodes.io
h3#Responses Responses
- p JSON(P) only (for the moment).
+ p JSON(P) only.
p Each response comes with an appropriate HTTP Status code. These include 200 for success, 400 for bad request, 404 for not found and 500 for server error. The HTTP Status code is also included in the response body.
+ p JSONP requests do not come with an appropriate HTTP Status code because of silent errors. All JSONP requests return 200 and error is accomplished via the response body.
+
h3#Authentication Authentication
p Postcodes.io does not require any authentication.
@@ -35,7 +37,9 @@ block content
p To check for errors, simply examine the HTTP response code. 200 response indicates success while any other code will provide important information about why an error occured.
- p The HTTP Status code is also available as a 'code' property in the result body in case of JSONP requests.
+ p Alternatively you can examine HTTP Status code in the 'status' property in the result body.
+
+ p Since all JSONP requests return 200, you need use the latter method to check for errors.
h3#JSONP JSONP
p JSONP requests are supported. Simply appended the URI with a callback name. E.g.
View
@@ -7,8 +7,11 @@ var pagesController = require(path.join(__dirname, "../app/controllers/pages_con
var jsonResponder = function (request, response, next) {
var jsonResponse = response.jsonApiResponse;
if (!jsonResponse) return next();
- response.status(jsonResponse.status);
- response.jsonp(jsonResponse);
+ if (request.query.callback) {
+ response.jsonp(200, jsonResponse);
+ } else {
+ response.json(jsonResponse.status, jsonResponse);
+ }
}
module.exports = function (app) {
View
@@ -97,6 +97,12 @@ function lookupRandomPostcode(callback) {
});
}
+function jsonpResponseBody (response) {
+ // Rough regex to extract json object
+ var result = response.text.match(/\(.*\)/);
+ return JSON.parse(result[0].slice(1, result[0].length - 1));
+}
+
function isPostcodeObject(o) {
assert.notProperty(o, "id");
assert.property(o, "nhs_ha");
@@ -161,6 +167,7 @@ module.exports = {
seedPostcodeDb: seedPostcodeDb,
clearPostcodeDb: clearPostcodeDb,
isPostcodeObject: isPostcodeObject,
+ jsonpResponseBody: jsonpResponseBody,
getCustomRelation: getCustomRelation,
isRawPostcodeObject: isRawPostcodeObject,
lookupRandomPostcode: lookupRandomPostcode,
View
@@ -0,0 +1,22 @@
+var path = require("path"),
+ app = require(path.join(__dirname, "../server")),
+ request = require("supertest"),
+ helper = require(path.join(__dirname, "helper")),
+ assert = require("chai").assert;
+
+describe("Utils with JSONP", function () {
+ describe("Ping", function () {
+ it ("should pong", function (done) {
+ request(app)
+ .get("/ping?callback=foo")
+ .expect(200)
+ .expect("Content-Type", /text\/javascript/)
+ .end(function (error, response) {
+ response.body = helper.jsonpResponseBody(response);
+ if (error) throw error;
+ assert.equal(response.body.result, "pong");
+ done();
+ });
+ });
+ });
+});
Oops, something went wrong.

0 comments on commit d90c797

Please sign in to comment.