Permalink
Browse files

Fixed empty query string

Cleaning up
Added String
Added String
Added test for missing postcode query
Cleaning up
  • Loading branch information...
1 parent 7aa4df0 commit 9e236624c9fbe4141bc7f53a6cc54397560b62e6 @cblanc cblanc committed Feb 17, 2014
View
33 app/controllers/postcodes_controller.js
@@ -1,5 +1,6 @@
var logger = require("commonlog-bunyan"),
async = require("async"),
+ S = require("string"),
Postcode = require("../models/postcode");
exports.show = function (request, response, next) {
@@ -10,12 +11,12 @@ exports.show = function (request, response, next) {
return next(error);
}
if (address) {
- response.json(200, {
+ return response.json(200, {
status: 200,
result: Postcode.toJson(address)
});
} else {
- response.jsonp(404, {
+ return response.jsonp(404, {
status: 404,
error: "Postcode not found"
});
@@ -33,12 +34,12 @@ exports.valid = function (request, response, next) {
}
if (address) {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: true
});
} else {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: false
});
@@ -52,7 +53,7 @@ exports.random = function (request, response, next) {
return next(error);
}
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: Postcode.toJson(address)
});
@@ -65,7 +66,7 @@ exports.bulk = function (request, response, next) {
} else if (request.body.geolocations) {
return bulkGeocode(request, response, next);
} else {
- response.jsonp(400, {
+ return response.jsonp(400, {
status: 400,
error: "Invalid JSON submitted. You need to submit a JSON object with an array of postcodes or geolocation objects"
});
@@ -175,8 +176,8 @@ exports.query = function (request, response, next) {
var searchTerm = request.query.q || request.query.query,
limit = request.query.limit;
- if (!searchTerm) {
- response.jsonp(200, {
+ if (S(searchTerm).isEmpty()) {
+ return response.jsonp(400, {
status: 400,
error: "No postcode query submitted. Remember to include query parameter"
});
@@ -185,12 +186,12 @@ exports.query = function (request, response, next) {
Postcode.search(searchTerm, {limit: limit}, function (error, results) {
if (error) return next(error);
if (!results) {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: null
});
} else {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: results.map(function (elem) {
return Postcode.toJson(elem);
@@ -207,12 +208,12 @@ exports.autocomplete = function (request, response, next) {
Postcode.search(searchTerm, {limit: limit}, function (error, results) {
if (error) return next(error);
if (!results) {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: null
});
} else {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: results.map(function (elem) {
return elem.postcode;
@@ -265,12 +266,12 @@ exports.lonlat = function (request, response, next) {
Postcode.nearestPostcodes(params, function (error, results) {
if (error) return next(error);
if (!results) {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: null
});
} else {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: results.map(function (postcode) {
return Postcode.toJson(postcode);
@@ -286,12 +287,12 @@ exports.showOutcode = function (request, response, next) {
Postcode.findOutcode(outcode, function (error, result) {
if (error) return next(error);
if (!result) {
- response.jsonp(404, {
+ return response.jsonp(404, {
status: 404,
result: null
});
} else {
- response.jsonp(200, {
+ return response.jsonp(200, {
status: 200,
result: result
});
View
1 config/routes.js
@@ -7,7 +7,6 @@ module.exports = function (app) {
app.get("/", pagesController.home);
app.get("/about", pagesController.about)
app.get("/docs", pagesController.documentation);
-
app.get("/postcodes", postcodesController.query);
app.post("/postcodes", postcodesController.bulk);
app.get("/random/postcodes", postcodesController.random);
View
7 npm-shrinkwrap.json
@@ -1,6 +1,6 @@
{
"name": "postcodes.io",
- "version": "1.1.0",
+ "version": "1.1.1",
"dependencies": {
"pg": {
"version": "2.11.1",
@@ -403,6 +403,11 @@
"from": "assert-plus@0.1.4"
}
}
+ },
+ "string": {
+ "version": "1.8.0",
+ "from": "string@",
+ "resolved": "https://registry.npmjs.org/string/-/string-1.8.0.tgz"
}
}
}
View
3 package.json
@@ -43,7 +43,8 @@
"async": "~0.2.10",
"postcode": "~0.2.1",
"ospoint": "~0.2.0",
- "bunyan-syslog": "~0.2.2"
+ "bunyan-syslog": "~0.2.2",
+ "string": "~1.8.0"
},
"devDependencies": {
"mocha": "~1.17.0",
View
14 tests/integration_postcodes.js
@@ -25,7 +25,7 @@ describe("Postcodes routes", function () {
helper.clearPostcodeDb(done);
});
- describe("GET /", function () {
+ describe("GET /postcodes", function () {
var uri, limit;
it ("should return a list of matching postcode objects", function (done) {
@@ -127,6 +127,18 @@ describe("Postcodes routes", function () {
done();
});
});
+ it ("should return 400 if no postcode submitted", function (done) {
+ uri = encodeURI("/postcodes?q=");
+ request(app)
+ .get(uri)
+ .expect("Content-Type", /json/)
+ .expect(400)
+ .end(function (error, response) {
+ if (error) throw error;
+ assert.equal(response.body.status, 400);
+ done();
+ });
+ });
});
describe("Post /", function () {
View
2 tests/unit_postcode_model.js
@@ -111,15 +111,13 @@ describe("Postcode Model", function () {
it ("should return null if no matching outcode", function (done) {
Postcode.findOutcode("BOGUSOUTCODE", function (error, result) {
if (error) throw error;
- console.log(result);
assert.equal(result, null);
done();
});
});
it ("should return null for a plausible but non-existent postcode", function (done) {
Postcode.findOutcode("EJ12", function (error, result) {
if (error) throw error;
- console.log(result);
assert.equal(result, null);
done();
});

0 comments on commit 9e23662

Please sign in to comment.