From 079ec94197774fe54787d6fc5d917c0eae0be7d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Tue, 6 Jan 2015 16:41:17 +0100 Subject: [PATCH] Fix API not returning anything if bad URL --- lib/server/controllers/apiController.js | 6 +++- test/api/apiTest.js | 44 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/server/controllers/apiController.js b/lib/server/controllers/apiController.js index 161fb0e..ac452bc 100644 --- a/lib/server/controllers/apiController.js +++ b/lib/server/controllers/apiController.js @@ -88,14 +88,18 @@ var ApiController = function(app) { .fail(function(err) { debug('Saving results to resultsDatastore failed:'); debug(err); + + res.status(500).send('Saving results failed'); }); }).fail(function(err) { - console.error('Test failed for %s', run.params.url); + console.error('Test failed for URL: %s', run.params.url); console.error(err.toString()); runsDatastore.markAsFailed(run.runId, err.toString()); + + res.status(400).send('Bad request'); }).finally(function() { queue.remove(run.runId); diff --git a/test/api/apiTest.js b/test/api/apiTest.js index f44db3a..bb20128 100644 --- a/test/api/apiTest.js +++ b/test/api/apiTest.js @@ -41,6 +41,50 @@ describe('api', function() { }); }); + it('should fail without an URL when asynchronous', function(done) { + this.timeout(15000); + + request({ + method: 'POST', + url: serverUrl + '/api/runs', + body: { + url: '' + }, + json: true, + headers: { + 'X-Api-Key': Object.keys(config.authorizedKeys)[0] + } + }, function(error, response, body) { + if (!error && response.statusCode === 400) { + done(); + } else { + done(error || response.statusCode); + } + }); + }); + + it('should fail without an URL when synchronous', function(done) { + this.timeout(15000); + + request({ + method: 'POST', + url: serverUrl + '/api/runs', + body: { + url: '', + waitForResponse: true + }, + json: true, + headers: { + 'X-Api-Key': Object.keys(config.authorizedKeys)[0] + } + }, function(error, response, body) { + if (!error && response.statusCode === 400) { + done(); + } else { + done(error || response.statusCode); + } + }); + }); it('should launch a synchronous run', function(done) { this.timeout(15000);