浏览代码

New async run launch option in API: return only a portion of the response

Gaël Métais 10 年之前
父节点
当前提交
e26fd5ed6f
共有 2 个文件被更改,包括 49 次插入2 次删除
  1. 20 2
      lib/server/controllers/apiController.js
  2. 29 0
      test/api/apiTest.js

+ 20 - 2
lib/server/controllers/apiController.js

@@ -23,7 +23,8 @@ var ApiController = function(app) {
             runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
             runId: (Date.now()*1000 + Math.round(Math.random()*1000)).toString(36),
             params: {
             params: {
                 url: req.body.url,
                 url: req.body.url,
-                waitForResponse: req.body.waitForResponse !== false
+                waitForResponse: req.body.waitForResponse !== false,
+                partialResult: req.body.partialResult || null
             }
             }
         };
         };
 
 
@@ -63,7 +64,24 @@ var ApiController = function(app) {
                             
                             
                             // Send result if the user was waiting
                             // Send result if the user was waiting
                             if (run.params.waitForResponse) {
                             if (run.params.waitForResponse) {
-                                res.redirect(302, '/api/results/' + run.runId);
+
+                                // If the user only wants a portion of the result (partialResult option)
+                                switch(run.params.partialResult) {
+                                    case 'generalScores': 
+                                        res.redirect(302, '/api/results/' + run.runId + '/generalScores');
+                                        break;
+                                    case 'rules': 
+                                        res.redirect(302, '/api/results/' + run.runId + '/rules');
+                                        break;
+                                    case 'javascriptExecutionTree':
+                                        res.redirect(302, '/api/results/' + run.runId + '/javascriptExecutionTree');
+                                        break;
+                                    case 'phantomas':
+                                        res.redirect(302, '/api/results/' + run.runId + '/toolsResults/phantomas');
+                                        break;
+                                    default:
+                                        res.redirect(302, '/api/results/' + run.runId);
+                                }
                             }
                             }
                             
                             
                         })
                         })

+ 29 - 0
test/api/apiTest.js

@@ -69,6 +69,35 @@ describe('api', function() {
         });
         });
     });
     });
 
 
+    it('should return the rules only', function(done) {
+        this.timeout(15000);
+
+        request({
+            method: 'POST',
+            url: serverUrl + '/api/runs',
+            body: {
+                url: wwwUrl + '/simple-page.html',
+                waitForResponse: true,
+                partialResult: 'rules'
+            },
+            json: true,
+            headers: {
+                'X-Api-Key': Object.keys(config.authorizedKeys)[0]
+            }
+        }, function(error, response, body) {
+            if (!error && response.statusCode === 302) {
+
+                console.log(response.headers.location);
+                response.headers.should.have.a.property('location').that.is.a('string');
+                response.headers.location.should.contain('/rules');
+
+                done();
+            } else {
+                done(error || response.statusCode);
+            }
+        });
+    });
+
 
 
     it('should retrieve the results for the synchronous run', function(done) {
     it('should retrieve the results for the synchronous run', function(done) {
         this.timeout(15000);
         this.timeout(15000);