New API route to retrieve a screenshot

This commit is contained in:
Gaël Métais
2015-02-05 17:27:21 +01:00
parent 55dc387d3c
commit efa4151850
4 changed files with 114 additions and 7 deletions
+19 -5
View File
@@ -27,7 +27,7 @@ var ApiController = function(app) {
url: req.body.url,
waitForResponse: req.body.waitForResponse !== false && req.body.waitForResponse !== 'false' && req.body.waitForResponse !== 0,
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) {
// Replace the empty promise created earlier with Q.resolve()
screenshotPromise = screenshot.toThumbnail(640)
screenshotPromise = screenshot.toThumbnail(600)
// Read screenshot
.then(function(screenshotBuffer) {
@@ -88,11 +88,9 @@ var ApiController = function(app) {
data.screenshotBuffer = screenshotBuffer;
// 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
.then(screenshot.deleteTmpFile);
@@ -104,6 +102,7 @@ var ApiController = function(app) {
// Save results
.then(function() {
delete data.params.options.screenshot;
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;
@@ -106,6 +106,15 @@ function ResultsDatastore() {
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;
+39 -2
View File
@@ -16,6 +16,7 @@ describe('api', function() {
var syncRunResultUrl;
var asyncRunId;
var screenshotUrl;
it('should refuse a query with an invalid key', function(done) {
@@ -94,7 +95,8 @@ describe('api', function() {
url: serverUrl + '/api/runs',
body: {
url: wwwUrl + '/simple-page.html',
waitForResponse: true
waitForResponse: true,
screenshot: true
},
json: true,
headers: {
@@ -165,7 +167,9 @@ describe('api', function() {
body.should.not.have.a.property('screenshotBuffer');
// Check if the screenshot url is here
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();
@@ -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
View File
@@ -1,6 +1,9 @@
var should = require('chai').should();
var resultsDatastore = require('../../lib/server/datastores/resultsDatastore');
var fs = require('fs');
var path = require('path');
describe('resultsDatastore', function() {
var datastore = new resultsDatastore();
@@ -71,4 +74,48 @@ describe('resultsDatastore', function() {
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);
});
});
});