Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added method to lookup outcodes

Added method to lookup outcodes
Added outcode integration tests
  • Loading branch information...
commit fdd81ecad7ca9b99fefb2e55a0d522b9ba6021bb 1 parent 5716102
@cblanc cblanc authored
View
19 app/controllers/postcodes_controller.js
@@ -281,10 +281,21 @@ exports.lonlat = function (request, response, next) {
}
exports.showOutcode = function (request, response, next) {
- // var outcode = request.params.outward_code;
+ var outcode = request.params.outcode;
- // if (invalid_outcode) {
-
- // }
+ Postcode.findOutcode(outcode, function (error, result) {
+ if (error) return next(error);
+ if (!result) {
+ response.jsonp(404, {
+ status: 404,
+ result: null
+ });
+ } else {
+ response.jsonp(200, {
+ status: 200,
+ result: result
+ });
+ }
+ });
}
View
2  config/routes.js
@@ -16,5 +16,5 @@ module.exports = function (app) {
app.get("/postcodes/:postcode", postcodesController.show);
app.get("/postcodes/:postcode/validate", postcodesController.valid);
app.get("/postcodes/:postcode/autocomplete", postcodesController.autocomplete);
- // app.get("/outcodes/:outward_code", postcodesController.showOutcode);
+ app.get("/outcodes/:outcode", postcodesController.showOutcode);
}
View
68 tests/integration_postcodes.js
@@ -18,6 +18,7 @@ describe("Postcodes routes", function () {
beforeEach(function () {
testPostcode = helper.randomPostcode();
+ testOutcode = helper.randomOutcode();
});
after(function (done) {
@@ -327,6 +328,73 @@ describe("Postcodes routes", function () {
});
});
+ describe("/outcodes/:outcode", function (done) {
+ it ("should return correct geolocation data for a given outcode", function (done) {
+ var path = ["/outcodes/", encodeURI(testOutcode)].join("");
+ request(app)
+ .get(path)
+ .expect("Content-Type", /json/)
+ .expect(200)
+ .end(function (error, response) {
+ if (error) throw error;
+ assert.equal(response.body.status, 200);
+ assert.equal(response.body.result.outcode, testOutcode);
+ assert.property(response.body.result, "longitude");
+ assert.property(response.body.result, "latitude");
+ assert.property(response.body.result, "northings");
+ assert.property(response.body.result, "eastings");
+ done();
+ });
+ });
+ it ("should be case insensitive", function (done) {
+ var path = ["/outcodes/", encodeURI(testOutcode.toLowerCase())].join("");
+ request(app)
+ .get(path)
+ .expect("Content-Type", /json/)
+ .expect(200)
+ .end(function (error, response) {
+ if (error) throw error;
+ assert.equal(response.body.status, 200);
+ assert.equal(response.body.result.outcode, testOutcode);
+ assert.property(response.body.result, "longitude");
+ assert.property(response.body.result, "latitude");
+ assert.property(response.body.result, "northings");
+ assert.property(response.body.result, "eastings");
+ done();
+ });
+ });
+ it ("should be space insensitive", function (done) {
+ var path = ["/outcodes/", encodeURI(testOutcode+" ")].join("");
+ request(app)
+ .get(path)
+ .expect("Content-Type", /json/)
+ .expect(200)
+ .end(function (error, response) {
+ if (error) throw error;
+ assert.equal(response.body.status, 200);
+ assert.equal(response.body.result.outcode, testOutcode);
+ assert.property(response.body.result, "longitude");
+ assert.property(response.body.result, "latitude");
+ assert.property(response.body.result, "northings");
+ assert.property(response.body.result, "eastings");
+ done();
+ });
+ });
+ it ("should return 404 for an outcode which does not exist", function (done) {
+ var path = ["/outcodes/", encodeURI("DEFINITELYBOGUS")].join("");
+ request(app)
+ .get(path)
+ .expect("Content-Type", /json/)
+ .expect(404)
+ .end(function (error, response) {
+ if (error) throw error;
+ assert.equal(response.body.status, 404);
+ assert.isNull(response.body.result);
+ done();
+ });
+ });
+ });
+
describe("/:postcode/validate", function () {
it ("should return true if postcode found", function (done) {
var path = ["/postcodes/", encodeURI(testPostcode), "/validate"].join("");

0 comments on commit fdd81ec

Please sign in to comment.
Something went wrong with that request. Please try again.