Jelajahi Sumber

New API route to retrieve a screenshot

Gaël Métais 10 tahun lalu
induk
melakukan
efa4151850

+ 19 - 5
lib/server/controllers/apiController.js

@@ -27,7 +27,7 @@ var ApiController = function(app) {
                 url: req.body.url,
                 url: req.body.url,
                 waitForResponse: req.body.waitForResponse !== false && req.body.waitForResponse !== 'false' && req.body.waitForResponse !== 0,
                 waitForResponse: req.body.waitForResponse !== false && req.body.waitForResponse !== 'false' && req.body.waitForResponse !== 0,
                 partialResult: req.body.partialResult || null,
                 partialResult: req.body.partialResult || null,
-                screenshot: req.body.screenshot !== false && req.body.screenshot !== 'false' && req.body.screenshot !== 0
+                screenshot: req.body.screenshot || false
             }
             }
         };
         };
 
 
@@ -78,7 +78,7 @@ var ApiController = function(app) {
             if (run.params.screenshot) {
             if (run.params.screenshot) {
                 
                 
                 // Replace the empty promise created earlier with Q.resolve()
                 // Replace the empty promise created earlier with Q.resolve()
-                screenshotPromise = screenshot.toThumbnail(640)
+                screenshotPromise = screenshot.toThumbnail(600)
                 
                 
                     // Read screenshot
                     // Read screenshot
                     .then(function(screenshotBuffer) {
                     .then(function(screenshotBuffer) {
@@ -88,11 +88,9 @@ var ApiController = function(app) {
                             data.screenshotBuffer = screenshotBuffer;
                             data.screenshotBuffer = screenshotBuffer;
 
 
                             // Official path to get the image
                             // Official path to get the image
-                            data.screenshotUrl = '/result/' + data.runId + '/screenshot.jpg';
+                            data.screenshotUrl = '/api/results/' + data.runId + '/screenshot.jpg';
                         }
                         }
 
 
-                        delete data.params.options.screenshot;
-
                     })
                     })
                     // Delete screenshot temporary file
                     // Delete screenshot temporary file
                     .then(screenshot.deleteTmpFile);
                     .then(screenshot.deleteTmpFile);
@@ -104,6 +102,7 @@ var ApiController = function(app) {
 
 
                 // Save results
                 // Save results
                 .then(function() {
                 .then(function() {
+                    delete data.params.options.screenshot;
                     return resultsDatastore.saveResult(data);
                     return resultsDatastore.saveResult(data);
                 })
                 })
 
 
@@ -270,6 +269,21 @@ var ApiController = function(app) {
             });
             });
     }
     }
 
 
+    // Retrive one result by id
+    app.get('/api/results/:id/screenshot.jpg', function(req, res) {
+        var runId = req.params.id;
+
+        resultsDatastore.getScreenshot(runId)
+            .then(function(screenshotBuffer) {
+                
+                res.setHeader('Content-Type', 'image/jpeg');
+                res.send(screenshotBuffer);
+
+            }).fail(function() {
+                res.status(404).send('Not found');
+            });
+    });
+
 };
 };
 
 
 module.exports = ApiController;
 module.exports = ApiController;

+ 9 - 0
lib/server/datastores/resultsDatastore.js

@@ -106,6 +106,15 @@ function ResultsDatastore() {
 
 
         return deferred;
         return deferred;
     }
     }
+
+    this.getScreenshot = function(runId) {
+
+        var screenshotFilePath = path.join(resultsDir, runId, resultScreenshotName);
+
+        debug('Getting screenshot (runID = %s) from disk...', runId);
+        
+        return Q.nfcall(fs.readFile, screenshotFilePath);
+    };
 }
 }
 
 
 module.exports = ResultsDatastore;
 module.exports = ResultsDatastore;

+ 39 - 2
test/api/apiTest.js

@@ -16,6 +16,7 @@ describe('api', function() {
 
 
     var syncRunResultUrl;
     var syncRunResultUrl;
     var asyncRunId;
     var asyncRunId;
+    var screenshotUrl;
 
 
 
 
     it('should refuse a query with an invalid key', function(done) {
     it('should refuse a query with an invalid key', function(done) {
@@ -94,7 +95,8 @@ describe('api', function() {
             url: serverUrl + '/api/runs',
             url: serverUrl + '/api/runs',
             body: {
             body: {
                 url: wwwUrl + '/simple-page.html',
                 url: wwwUrl + '/simple-page.html',
-                waitForResponse: true
+                waitForResponse: true,
+                screenshot: true
             },
             },
             json: true,
             json: true,
             headers: {
             headers: {
@@ -165,7 +167,9 @@ describe('api', function() {
                 body.should.not.have.a.property('screenshotBuffer');
                 body.should.not.have.a.property('screenshotBuffer');
                 // Check if the screenshot url is here
                 // Check if the screenshot url is here
                 body.should.have.a.property('screenshotUrl');
                 body.should.have.a.property('screenshotUrl');
-                body.screenshotUrl.should.have.string('/result/' + body.runId + '/screenshot.jpg');
+                body.screenshotUrl.should.equal('/api/results/' + body.runId + '/screenshot.jpg');
+
+                screenshotUrl = body.screenshotUrl;
 
 
                 done();
                 done();
 
 
@@ -523,4 +527,37 @@ describe('api', function() {
         });
         });
     });
     });
 
 
+
+    it('should retrieve the screenshot', function(done) {
+        this.timeout(5000);
+
+        request({
+            method: 'GET',
+            url: serverUrl + screenshotUrl
+        }, function(error, response, body) {
+            if (!error && response.statusCode === 200) {
+                response.headers['content-type'].should.equal('image/jpeg');
+                done();
+            } else {
+                done(error || response.statusCode);
+            }
+        });
+    });
+
+
+    it('should fail on a unexistant screenshot', function(done) {
+        this.timeout(5000);
+
+        request({
+            method: 'GET',
+            url: serverUrl + '/api/results/000000/screenshot.jpg'
+        }, function(error, response, body) {
+            if (!error && response.statusCode === 404) {
+                done();
+            } else {
+                done(error || response.statusCode);
+            }
+        });
+    });
+
 });
 });

+ 47 - 0
test/api/resultsDatastoreTest.js

@@ -1,6 +1,9 @@
 var should = require('chai').should();
 var should = require('chai').should();
 var resultsDatastore = require('../../lib/server/datastores/resultsDatastore');
 var resultsDatastore = require('../../lib/server/datastores/resultsDatastore');
 
 
+var fs = require('fs');
+var path = require('path');
+
 describe('resultsDatastore', function() {
 describe('resultsDatastore', function() {
     
     
     var datastore = new resultsDatastore();
     var datastore = new resultsDatastore();
@@ -71,4 +74,48 @@ describe('resultsDatastore', function() {
                 done();
                 done();
             });
             });
     });
     });
+
+
+    var testId3 = '555555';
+    var testData3 = {
+        runId: testId3,
+        other: {
+            foo: 'foo',
+            bar: 2
+        },
+        screenshotBuffer: fs.readFileSync(path.join(__dirname, '../fixtures/logo-large.png'))
+    };
+
+    it('should store a test with a screenshot', function(done) {
+
+        datastore.saveResult(testData3).then(function() {
+            done();
+        }).fail(function(err) {
+            done(err);
+        });
+    });
+
+    it('should have a normal result', function(done) {
+        datastore.getResult(testId3)
+            .then(function(results) {
+
+                results.should.not.have.a.property('screenshot');
+
+                done();
+            })
+            .fail(function(err) {
+                done(err);
+            });
+    });
+
+    it('should retrieve the saved image', function() {
+        datastore.getScreenshot(testId3)
+            .then(function(imageBuffer) {
+                imageBuffer.should.be.an.instanceof(Buffer);
+                done();
+            })
+            .fail(function(err) {
+                done(err);
+            });
+    });
 });
 });