Skip to content

Commit

Permalink
Fixed empty query string
Browse files Browse the repository at this point in the history
Cleaning up
Added String
Added String
Added test for missing postcode query
Cleaning up
  • Loading branch information
cblanc committed Feb 17, 2014
1 parent 7aa4df0 commit 9e23662
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
33 changes: 17 additions & 16 deletions app/controllers/postcodes_controller.js
@@ -1,5 +1,6 @@
var logger = require("commonlog-bunyan"), var logger = require("commonlog-bunyan"),
async = require("async"), async = require("async"),
S = require("string"),
Postcode = require("../models/postcode"); Postcode = require("../models/postcode");


exports.show = function (request, response, next) { exports.show = function (request, response, next) {
Expand All @@ -10,12 +11,12 @@ exports.show = function (request, response, next) {
return next(error); return next(error);
} }
if (address) { if (address) {
response.json(200, { return response.json(200, {
status: 200, status: 200,
result: Postcode.toJson(address) result: Postcode.toJson(address)
}); });
} else { } else {
response.jsonp(404, { return response.jsonp(404, {
status: 404, status: 404,
error: "Postcode not found" error: "Postcode not found"
}); });
Expand All @@ -33,12 +34,12 @@ exports.valid = function (request, response, next) {
} }


if (address) { if (address) {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: true result: true
}); });
} else { } else {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: false result: false
}); });
Expand All @@ -52,7 +53,7 @@ exports.random = function (request, response, next) {
return next(error); return next(error);
} }


response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: Postcode.toJson(address) result: Postcode.toJson(address)
}); });
Expand All @@ -65,7 +66,7 @@ exports.bulk = function (request, response, next) {
} else if (request.body.geolocations) { } else if (request.body.geolocations) {
return bulkGeocode(request, response, next); return bulkGeocode(request, response, next);
} else { } else {
response.jsonp(400, { return response.jsonp(400, {
status: 400, status: 400,
error: "Invalid JSON submitted. You need to submit a JSON object with an array of postcodes or geolocation objects" error: "Invalid JSON submitted. You need to submit a JSON object with an array of postcodes or geolocation objects"
}); });
Expand Down Expand Up @@ -175,8 +176,8 @@ exports.query = function (request, response, next) {
var searchTerm = request.query.q || request.query.query, var searchTerm = request.query.q || request.query.query,
limit = request.query.limit; limit = request.query.limit;


if (!searchTerm) { if (S(searchTerm).isEmpty()) {
response.jsonp(200, { return response.jsonp(400, {
status: 400, status: 400,
error: "No postcode query submitted. Remember to include query parameter" error: "No postcode query submitted. Remember to include query parameter"
}); });
Expand All @@ -185,12 +186,12 @@ exports.query = function (request, response, next) {
Postcode.search(searchTerm, {limit: limit}, function (error, results) { Postcode.search(searchTerm, {limit: limit}, function (error, results) {
if (error) return next(error); if (error) return next(error);
if (!results) { if (!results) {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: null result: null
}); });
} else { } else {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: results.map(function (elem) { result: results.map(function (elem) {
return Postcode.toJson(elem); return Postcode.toJson(elem);
Expand All @@ -207,12 +208,12 @@ exports.autocomplete = function (request, response, next) {
Postcode.search(searchTerm, {limit: limit}, function (error, results) { Postcode.search(searchTerm, {limit: limit}, function (error, results) {
if (error) return next(error); if (error) return next(error);
if (!results) { if (!results) {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: null result: null
}); });
} else { } else {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: results.map(function (elem) { result: results.map(function (elem) {
return elem.postcode; return elem.postcode;
Expand Down Expand Up @@ -265,12 +266,12 @@ exports.lonlat = function (request, response, next) {
Postcode.nearestPostcodes(params, function (error, results) { Postcode.nearestPostcodes(params, function (error, results) {
if (error) return next(error); if (error) return next(error);
if (!results) { if (!results) {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: null result: null
}); });
} else { } else {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: results.map(function (postcode) { result: results.map(function (postcode) {
return Postcode.toJson(postcode); return Postcode.toJson(postcode);
Expand All @@ -286,12 +287,12 @@ exports.showOutcode = function (request, response, next) {
Postcode.findOutcode(outcode, function (error, result) { Postcode.findOutcode(outcode, function (error, result) {
if (error) return next(error); if (error) return next(error);
if (!result) { if (!result) {
response.jsonp(404, { return response.jsonp(404, {
status: 404, status: 404,
result: null result: null
}); });
} else { } else {
response.jsonp(200, { return response.jsonp(200, {
status: 200, status: 200,
result: result result: result
}); });
Expand Down
1 change: 0 additions & 1 deletion config/routes.js
Expand Up @@ -7,7 +7,6 @@ module.exports = function (app) {
app.get("/", pagesController.home); app.get("/", pagesController.home);
app.get("/about", pagesController.about) app.get("/about", pagesController.about)
app.get("/docs", pagesController.documentation); app.get("/docs", pagesController.documentation);

app.get("/postcodes", postcodesController.query); app.get("/postcodes", postcodesController.query);
app.post("/postcodes", postcodesController.bulk); app.post("/postcodes", postcodesController.bulk);
app.get("/random/postcodes", postcodesController.random); app.get("/random/postcodes", postcodesController.random);
Expand Down
7 changes: 6 additions & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -43,7 +43,8 @@
"async": "~0.2.10", "async": "~0.2.10",
"postcode": "~0.2.1", "postcode": "~0.2.1",
"ospoint": "~0.2.0", "ospoint": "~0.2.0",
"bunyan-syslog": "~0.2.2" "bunyan-syslog": "~0.2.2",
"string": "~1.8.0"
}, },
"devDependencies": { "devDependencies": {
"mocha": "~1.17.0", "mocha": "~1.17.0",
Expand Down
14 changes: 13 additions & 1 deletion tests/integration_postcodes.js
Expand Up @@ -25,7 +25,7 @@ describe("Postcodes routes", function () {
helper.clearPostcodeDb(done); helper.clearPostcodeDb(done);
}); });


describe("GET /", function () { describe("GET /postcodes", function () {
var uri, limit; var uri, limit;


it ("should return a list of matching postcode objects", function (done) { it ("should return a list of matching postcode objects", function (done) {
Expand Down Expand Up @@ -127,6 +127,18 @@ describe("Postcodes routes", function () {
done(); 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 () { describe("Post /", function () {
Expand Down
2 changes: 0 additions & 2 deletions tests/unit_postcode_model.js
Expand Up @@ -111,15 +111,13 @@ describe("Postcode Model", function () {
it ("should return null if no matching outcode", function (done) { it ("should return null if no matching outcode", function (done) {
Postcode.findOutcode("BOGUSOUTCODE", function (error, result) { Postcode.findOutcode("BOGUSOUTCODE", function (error, result) {
if (error) throw error; if (error) throw error;
console.log(result);
assert.equal(result, null); assert.equal(result, null);
done(); done();
}); });
}); });
it ("should return null for a plausible but non-existent postcode", function (done) { it ("should return null for a plausible but non-existent postcode", function (done) {
Postcode.findOutcode("EJ12", function (error, result) { Postcode.findOutcode("EJ12", function (error, result) {
if (error) throw error; if (error) throw error;
console.log(result);
assert.equal(result, null); assert.equal(result, null);
done(); done();
}); });
Expand Down

0 comments on commit 9e23662

Please sign in to comment.