Skip to content

Commit

Permalink
Added .file to make images or whatever downloadable.
Browse files Browse the repository at this point in the history
  • Loading branch information
basicallydan committed Dec 4, 2015
1 parent 5af1071 commit fd4b5d7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
19 changes: 19 additions & 0 deletions examples-javascript/fluent-image-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var Interfake = require('..');
var request = require('request'); // Let's use request for this example

var interfake = new Interfake({ debug: true });
interfake.get('/jay-peg').status(200).file('./examples-javascript/assets/20x20-image-redjpg.jpg');
interfake.get('/pee-en-gee').status(200).file('./examples-javascript/assets/20x20-image-redpng.png');
interfake.listen(3030); // The server will listen on port 3030

request('http://localhost:3030/jay-peg', function (error, response, body) {
console.log(response.statusCode); // prints 200
console.log(response.headers['content-disposition']); // prints attachment; 20x20-image-redjpg.jpg
console.log(response.headers['content-type']); // prints image/jpeg
});

request('http://localhost:3030/pee-en-gee', function (error, response, body) {
console.log(response.statusCode); // prints 200
console.log(response.headers['content-disposition']); // prints attachment; 20x20-image-redpng.png
console.log(response.headers['content-type']); // prints image/png
});
5 changes: 5 additions & 0 deletions lib/fluent.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ function FluentInterface(server, o) {
route.setResponseImage(image);
return this;
},
file: function (file) {
debug('Setting file to', file);
route.setResponseFile(file);
return this;
},
echo: function (echo) {
if (typeof echo === 'undefined') {
echo = true;
Expand Down
5 changes: 5 additions & 0 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ Route.prototype.setResponseImage = function (image) {
this.response.image = image;
};

Route.prototype.setResponseFile = function (file) {
this.debug('Now returning file called', file);
this.response.file = file;
};

Route.prototype.setProxyURL = function (url) {
this.debug('The route is now a proxy of', this.request.method, url);
this.response.proxy = url;
Expand Down
7 changes: 6 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function Interfake(o) {
url: req.path,
query: req.query
};
var rootPathRegex, responseBody, responseImage, responseStatusCode, responseHeaders, expectedRoute, expectedRoutes;
var rootPathRegex, responseBody, responseImage, responseFile, responseStatusCode, responseHeaders, expectedRoute, expectedRoutes;
var proxyQueryStrings;
var sendResponse = function () {
var responseBodyLength = 0;
Expand All @@ -102,6 +102,10 @@ function Interfake(o) {

debug('Sending response now');

if (responseFile) {
res.status(responseStatusCode).attachment(path.resolve(process.cwd(), responseFile));
responseImage = responseFile;
}
if (responseImage) {
res.status(responseStatusCode).sendFile(path.resolve(process.cwd(), responseImage));
} else {
Expand Down Expand Up @@ -173,6 +177,7 @@ function Interfake(o) {
} else {
responseBody = specifiedResponse.body;
responseImage = specifiedResponse.image;
responseFile = specifiedResponse.file;
responseStatusCode = specifiedResponse.code;
responseHeaders = specifiedResponse.headers;

Expand Down
24 changes: 24 additions & 0 deletions tests/media.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,29 @@ describe('Interfake File Response Tests', function() {
});
});
});

describe('#file()', function() {
it('should respond with a jpg image as a file rather than as an image', function(done) {
interfake.get('/image').file('./tests/assets/10x10-imagejpg.jpg');
interfake.listen(3000);

request({
url: 'http://localhost:3000/image',
json: true
}, function(error, response, body) {
assert.equal(response.statusCode, 200);
assert.equal(response.headers['content-type'], 'image/jpeg');
fs.readFile('./tests/assets/10x10-imagejpg.jpg', function(err, correctData) {
if (err) throw err;
assert.equal(correctData, body);
fs.readFile('./tests/assets/20x20-image-redpng.png', function(err, incorrectData) {
if (err) throw err;
assert.notEqual(incorrectData, body);
done();
});
});
});
});
});
});
});

0 comments on commit fd4b5d7

Please sign in to comment.