瀏覽代碼

Fix API not returning anything if bad URL

Gaël Métais 10 年之前
父節點
當前提交
079ec94197
共有 2 個文件被更改,包括 49 次插入1 次删除
  1. 5 1
      lib/server/controllers/apiController.js
  2. 44 0
      test/api/apiTest.js

+ 5 - 1
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);

+ 44 - 0
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);