Skip to content

Commit

Permalink
Started media tests for #19
Browse files Browse the repository at this point in the history
  • Loading branch information
basicallydan committed Dec 4, 2015
1 parent db1ad43 commit f78ec62
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/fluent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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) {
Expand Down Expand Up @@ -166,6 +172,7 @@ function Interfake(o) {
});
} else {
responseBody = specifiedResponse.body;
responseImage = specifiedResponse.image;
responseStatusCode = specifiedResponse.code;
responseHeaders = specifiedResponse.headers;

Expand All @@ -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);
Expand Down
Binary file added tests/assets/10x10-imagejpg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/10x10-imagepng.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/20x20-image-redjpg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/20x20-image-redpng.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions tests/media.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
});
});
});

0 comments on commit f78ec62

Please sign in to comment.