Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tapes human readable based on content-type and content-enconding #18

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/bodyHumanReadable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var contentTypeParser = require('content-type');
var humanReadableContentTypes = [
'application/javascript',
'application/json',
'text/css',
'text/html',
'text/javascript',
'text/plain'
];

/**
* Returns whether a request's body is human readable
* @param {http.IncomingMessage} req
* @returns {Boolean}
*/

module.exports = function (res) {
var contentEncoding = res.headers['content-encoding'];
var contentType = res.headers['content-type'];
var identityEncoding = !contentEncoding || contentEncoding === 'identity';

if (!contentType) {
return false;
}
contentType = contentTypeParser.parse(contentType);

return identityEncoding && isContentTypeHumanReadable(contentType);
};

/**
* Returns whether a content-type is human readable based on a whitelist
* @param {content-type} contentType
* @returns {Boolean}
*/

function isContentTypeHumanReadable(contentType) {
return humanReadableContentTypes.indexOf(contentType.type) >= 0;
}
5 changes: 4 additions & 1 deletion lib/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var Promise = require('bluebird');
var buffer = require('./buffer');
var isBodyHumanReadable = require('./bodyHumanReadable');
var path = require('path');
var ejs = require('ejs');
var fs = require('fs');
Expand All @@ -28,7 +29,9 @@ var render = ejs.compile(fs.readFileSync(path.resolve(__dirname, '../src/tape.ej

module.exports = function (req, res, filename) {
return buffer(res).then(function (body) {
return render({ req: req, res: res, body: body });
var bodyEncoding = isBodyHumanReadable(res) ? 'utf-8' : 'base64';

return render({ req: req, res: res, body: body, bodyEncoding: bodyEncoding });
}).then(function (data) {
return write(filename, data);
}).then(function () {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
},
"dependencies": {
"bluebird": "~3.3.4",
"content-type": "~1.0.2",
"debug": "~2.1.2",
"ejs": "~2.3.1",
"incoming-message-hash": "~3.2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/tape.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (req, res) {
res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));

<% body.forEach(function (data) { -%>
res.write(new Buffer(<%- JSON.stringify(data.toString('base64')) %>, "base64"));
res.write(new Buffer(<%- JSON.stringify(data.toString(bodyEncoding)) %>, '<%- bodyEncoding %>'));
<% }); -%>
res.end();

Expand Down
103 changes: 103 additions & 0 deletions test/bodyHumanReadable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the MIT license. Please see LICENSE file in the project root for terms.

/* eslint-env mocha */

var subject = require('../lib/bodyHumanReadable');
var createServer = require('./helpers/server');
var assert = require('assert');
var http = require('http');

describe('bodyHumanReadable', function () {
var server, req;

afterEach(function (done) {
server.teardown(done);
});

function makeRequest(requestHandler, test) {
server = createServer(function () {
req = http.request({
host: server.addr,
port: server.port
});
req.setHeader('User-Agent', 'My User Agent/1.0');
req.setHeader('Connection', 'close');

req.on('response', test);
req.end();
}, requestHandler);
}

describe("when content-type is human readable", function () {
it("returns true when content-encoding is not set", function (done) {
function requestHandler(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.end('<html></html>');
}

makeRequest(requestHandler, function (response) {
assert(subject(response));
done();
});
});

it("returns true when content-encoding is set to identity", function (done) {
function requestHandler(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.setHeader('Content-Encoding', 'identity');
response.end('<html></html>');
}

makeRequest(requestHandler, function (response) {
assert(subject(response));
done();
});
});

it("returns false when content-encoding is set to gzip", function (done) {
function requestHandler(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.setHeader('Content-Encoding', 'gzip');
response.end('ZIP');
}

makeRequest(requestHandler, function (response) {
assert(!subject(response));
done();
});
});
});

describe("when content-type is not human readable", function () {
it("returns false", function (done) {
function requestHandler(request, response) {
response.statusCode = 200;
response.setHeader('Content-Type', 'image/gif');
response.end('GIF');
}

makeRequest(requestHandler, function (response) {
assert(!subject(response));
done();
});
});
});

describe("when there's no content-type", function () {
it('returns false', function (done) {
function requestHandler(request, response) {
response.statusCode = 304;
response.end('OK');
}

makeRequest(requestHandler, function (response) {
assert(!subject(response));
done();
});
});
});
});
22 changes: 14 additions & 8 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ function read(file) {
}

/**
* node >= 1.5.0 sends the content-length whenever possible
* @see https://github.com/nodejs/node/pull/1062
* Returns a fixture based on a sufix and node version
* @type {String} sufix
* @returns {String}
*/

if (semver.gte(process.version, '1.5.0')) {
module.exports = read('v1.5.0');
} else {
module.exports = read('v0.10.x');
}
module.exports = function (sufix) {
/**
* node >= 1.5.0 sends the content-length whenever possible
* @see https://github.com/nodejs/node/pull/1062
*/
if (semver.gte(process.version, '1.5.0')) {
return read('v1.5.0-' + sufix);
} else {
return read('v0.10.x-' + sufix);
}
};
25 changes: 25 additions & 0 deletions test/fixtures/v0.10.x-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var path = require("path");

/**
* GET /
*
* host: {addr}:{port}
* user-agent: My User Agent/1.0
* connection: close
*/

module.exports = function (req, res) {
res.statusCode = 201;

res.setHeader("content-type", "image/gif");
res.setHeader("date", "Sat, 26 Oct 1985 08:20:00 GMT");
res.setHeader("connection", "close");
res.setHeader("transfer-encoding", "chunked");

res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));

res.write(new Buffer("R0lG", 'base64'));
res.end();

return __filename;
};
2 changes: 1 addition & 1 deletion test/fixtures/v0.10.x.js → test/fixtures/v0.10.x-utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (req, res) {

res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));

res.write(new Buffer("T0s=", "base64"));
res.write(new Buffer("OK", 'utf-8'));
res.end();

return __filename;
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/v1.5.0-base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var path = require("path");

/**
* GET /
*
* host: {addr}:{port}
* user-agent: My User Agent/1.0
* connection: close
*/

module.exports = function (req, res) {
res.statusCode = 201;

res.setHeader("content-type", "image/gif");
res.setHeader("date", "Sat, 26 Oct 1985 08:20:00 GMT");
res.setHeader("connection", "close");
res.setHeader("content-length", "3");

res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));

res.write(new Buffer("R0lG", 'base64'));
res.end();

return __filename;
};
2 changes: 1 addition & 1 deletion test/fixtures/v1.5.0.js → test/fixtures/v1.5.0-utf8.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (req, res) {

res.setHeader("x-yakbak-tape", path.basename(__filename, ".js"));

res.write(new Buffer("T0s=", "base64"));
res.write(new Buffer("OK", 'utf-8'));
res.end();

return __filename;
Expand Down
25 changes: 14 additions & 11 deletions test/helpers/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ var http = require('http');
/**
* Creates a test HTTP server.
* @param {Function} done
* @param {Function} reqHandler: an optional callback to customize how to handle the request
* @returns {http.Server}
*/

module.exports = function createServer(cb) {
module.exports = function createServer(done, reqHandler) {

var server = http.createServer(function (req, res) {
res.statusCode = 201;

res.setHeader('Content-Type', 'text/html');
res.setHeader('Date', 'Sat, 26 Oct 1985 08:20:00 GMT');

req.resume(); // consume the request body, if any

req.on('end', function () {
res.end('OK');
if (reqHandler) {
reqHandler(req, res);
} else {
res.statusCode = 201;

res.setHeader('Content-Type', 'text/html');
res.setHeader('Date', 'Sat, 26 Oct 1985 08:20:00 GMT');
res.end('OK');
}
});

}).on('listening', function () {
this.addr = 'localhost';
this.port = this.address().port;
Expand All @@ -36,10 +39,10 @@ module.exports = function createServer(cb) {
this.requests.push(req);
});

server.teardown = function (done) {
this.close(done);
server.teardown = function (doneT) {
this.close(doneT);
};

return server.listen(cb);
return server.listen(done);

};
Loading