diff --git a/lib/fluent.js b/lib/fluent.js index 4f61e7d..1dca406 100644 --- a/lib/fluent.js +++ b/lib/fluent.js @@ -99,6 +99,11 @@ function FluentInterface(server, o) { route.setResponseBody(body); return this; }, + image: function (image) { + debug('Setting image to', image); + route.setResponseImage(image); + return this; + }, echo: function (echo) { if (typeof echo === 'undefined') { echo = true; diff --git a/lib/route.js b/lib/route.js index e476469..606f5dc 100644 --- a/lib/route.js +++ b/lib/route.js @@ -96,6 +96,11 @@ Route.prototype.setResponseBody = function (body) { this.response.body = body; }; +Route.prototype.setResponseImage = function (image) { + this.debug('Now returning image called', image); + this.response.image = image; +}; + Route.prototype.setProxyURL = function (url) { this.debug('The route is now a proxy of', this.request.method, url); this.response.proxy = url; diff --git a/lib/server.js b/lib/server.js index 8e24f89..bd40a1b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -78,10 +78,11 @@ function Interfake(o) { url: req.path, query: req.query }; - var rootPathRegex, responseBody, responseStatusCode, responseHeaders, expectedRoute, expectedRoutes; + var rootPathRegex, responseBody, responseImage, responseStatusCode, responseHeaders, expectedRoute, expectedRoutes; var proxyQueryStrings; var sendResponse = function () { var responseBodyLength = 0; + if (responseBody) { responseBodyLength = JSON.stringify(responseBody).length; responseBody = JSON.parse(JSON.stringify(responseBody)); @@ -101,7 +102,12 @@ function Interfake(o) { debug('Sending response now'); - res.status(responseStatusCode).send(responseBody); + if (responseImage) { + res.status(responseStatusCode).sendFile(path.resolve(process.cwd(), responseImage)); + } else { + res.setHeader('Content-Type', 'application/json'); + res.status(responseStatusCode).send(responseBody); + } }; if (o.path.length > 1) { @@ -166,6 +172,7 @@ function Interfake(o) { }); } else { responseBody = specifiedResponse.body; + responseImage = specifiedResponse.image; responseStatusCode = specifiedResponse.code; responseHeaders = specifiedResponse.headers; @@ -180,8 +187,6 @@ function Interfake(o) { debug(req.method, 'request to', req.url, 'will be delayed by', responseDelay, 'millis'); debug('Expected route is', expectedRoute); - res.setHeader('Content-Type', 'application/json'); - setTimeout(function() { var modification; debug('Expected response is', specifiedResponse); diff --git a/tests/assets/10x10-imagejpg.jpg b/tests/assets/10x10-imagejpg.jpg new file mode 100644 index 0000000..3330778 Binary files /dev/null and b/tests/assets/10x10-imagejpg.jpg differ diff --git a/tests/assets/10x10-imagepng.png b/tests/assets/10x10-imagepng.png new file mode 100644 index 0000000..4c7ee04 Binary files /dev/null and b/tests/assets/10x10-imagepng.png differ diff --git a/tests/assets/20x20-image-redjpg.jpg b/tests/assets/20x20-image-redjpg.jpg new file mode 100644 index 0000000..af82214 Binary files /dev/null and b/tests/assets/20x20-image-redjpg.jpg differ diff --git a/tests/assets/20x20-image-redpng.png b/tests/assets/20x20-image-redpng.png new file mode 100644 index 0000000..38b9033 Binary files /dev/null and b/tests/assets/20x20-image-redpng.png differ diff --git a/tests/media.test.js b/tests/media.test.js new file mode 100644 index 0000000..ee2efcc --- /dev/null +++ b/tests/media.test.js @@ -0,0 +1,57 @@ +/* globals describe, beforeEach, afterEach, it */ +var assert = require('assert'); +var request = require('request'); +var Q = require('q'); +var fs = require('fs'); + +request = request.defaults({ + json: true +}); + +var get = Q.denodeify(request.get); +var post = Q.denodeify(request.post); +var put = Q.denodeify(request.put); +var del = Q.denodeify(request.del); +var patch = Q.denodeify(request.patch); + +// The thing we're testing +var Interfake = require('..'); +var interfake; + +describe('Interfake File Response Tests', function() { + beforeEach(function() { + interfake = new Interfake(); + }); + afterEach(function() { + if (interfake) { + interfake.stop(); + } + }); + + // Testing the fluent interface + describe('#get()', function() { + describe('#image()', function() { + it('should respond with the same image', function(done) { + interfake.get('/image').image('./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-redjpg.jpg', function(err, incorrectData) { + if (err) throw err; + assert.notEqual(incorrectData, body); + done(); + }); + }); + }); + }); + }); + }); +}); \ No newline at end of file