Skip to content
Open
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
96 changes: 6 additions & 90 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,17 @@
import chalk from 'chalk';
import { EventEmitter } from 'events';
import http from 'http';
import { inherits } from 'util';
import { getHostname, getHref, printHostnames, printTestRequest } from './utils';
import Mocha from 'mocha';

const hostnames = {};
let testRequests = [];

/**
* Returns indent for pretty printing.
* @param {integer} size - indent size in spaces
* @return {string} the indent string
*/
function indent(size) {
let indentStr = '';
let _size = size;
while (_size > 0) {
indentStr += ' ';
_size--;
}
return indentStr;
}

/**
* Prints full requests URL seen in test.
*/
function printTestRequest() {
console.log(chalk.yellow(`${indent(6)} Live requests: `));

testRequests.forEach(request => {
console.log(chalk.yellow(`${indent(8)} * ${request}`));
});
}

/**
* Print requested hostnames summary.
*/
function printHostnames() {
console.log(chalk.yellow.bold(`${indent(2)} Hostnames requested: `));

if (Object.keys(hostnames).length === 0) {
console.log(chalk.yellow(`${indent(4)}none`));
return;
}

for (const key in hostnames) {
if (!hostnames[key]) return;
console.log(chalk.yellow(`${indent(4)}${key}: ${hostnames[key]}`));
}
}
const testRequests = [];

/**
* Patch mocha to display recording requests during individual tests and at
* the end of the test suite.
*/
function patchMocha() {
export function patchMocha() {
const _testRun = Mocha.Test.prototype.run;

Mocha.Test.prototype.run = function (fn) {
Expand All @@ -63,7 +20,7 @@ function patchMocha() {

if (testRequests.length) {
printTestRequest(testRequests);
testRequests = [];
testRequests.length = 0;
}
}

Expand All @@ -82,40 +39,6 @@ function patchMocha() {
};
}

/**
* Get the hostname from an HTTP options object.
* Supports multiple types of options.
* @param {object} httpOptions
* @return {string} the hostname or "Unknown" if not found.
*/
function getHostname(httpOptions) {
if (httpOptions.uri && httpOptions.uri.hostname) {
return httpOptions.uri.hostname;
} else if (httpOptions.hostname) {
return httpOptions.hostname;
} else if (httpOptions.host) {
return httpOptions.host;
}
return 'Unknown';
}

/**
* Get the href from an HTTP options objet.
* Supports multiple types of options.
* @param {object} httpOptions
* @return {string} the hostname or "Unknown" if not found.
*/
function getHref(httpOptions) {
if (httpOptions.uri && httpOptions.uri.href) {
return httpOptions.uri.href;
} else if (httpOptions.hostname && httpOptions.path) {
return httpOptions.hostname + httpOptions.path;
} else if (httpOptions.host && httpOptions.path) {
return httpOptions.host + httpOptions.path;
}
return 'Unknown';
}

/**
* Patch Node's HTTP client to record external HTTP calls.
* - All hostnames are stored in `hostname` with their count for the whole
Expand All @@ -124,7 +47,7 @@ function getHref(httpOptions) {
* recorded requests for individual tests.
* - Requests to localhost are ignored.
*/
function patchHttpClient() {
export function patchHttpClient() {
const _ClientRequest = http.ClientRequest;

function patchedHttpClient(options, done) {
Expand All @@ -146,18 +69,11 @@ function patchHttpClient() {
_ClientRequest.call(this, options, done);
}

if (http.ClientRequest) {
inherits(patchedHttpClient, _ClientRequest);
} else {
inherits(patchedHttpClient, EventEmitter);
}
inherits(patchedHttpClient, _ClientRequest || EventEmitter);

http.ClientRequest = patchedHttpClient;

http.request = function(options, done) {
return new http.ClientRequest(options, done);
};
}

patchHttpClient();
patchMocha();
5 changes: 5 additions & 0 deletions src/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { patchHttpClient, patchMocha } from './';


patchHttpClient();
patchMocha();
78 changes: 78 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import chalk from 'chalk';

/**
* Returns indent for pretty printing.
* @param {integer} size - indent size in spaces
* @return {string} the indent string
*/
export function indent(size) {
let indentStr = '';
let _size = size;
while (_size > 0) {
indentStr += ' ';
_size--;
}
return indentStr;
}

/**
* Prints full requests URL seen in test.
*/
export function printTestRequest(testRequests) {
console.log(chalk.yellow(`${indent(6)} Live requests: `));

testRequests.forEach(request => {
console.log(chalk.yellow(`${indent(8)} * ${request}`));
});
}

/**
* Print requested hostnames summary.
*/
export function printHostnames(hostnames) {
console.log(chalk.yellow.bold(`${indent(2)} Hostnames requested: `));

if (Object.keys(hostnames).length === 0) {
console.log(chalk.yellow(`${indent(4)}none`));
return;
}

for (const key in hostnames) {
if (!hostnames[key]) return;
console.log(chalk.yellow(`${indent(4)}${key}: ${hostnames[key]}`));
}
}

/**
* Get the hostname from an HTTP options object.
* Supports multiple types of options.
* @param {object} httpOptions
* @return {string} the hostname or "Unknown" if not found.
*/
export function getHostname(httpOptions) {
if (httpOptions.uri && httpOptions.uri.hostname) {
return httpOptions.uri.hostname;
} else if (httpOptions.hostname) {
return httpOptions.hostname;
} else if (httpOptions.host) {
return httpOptions.host;
}
return 'Unknown';
}

/**
* Get the href from an HTTP options objet.
* Supports multiple types of options.
* @param {object} httpOptions
* @return {string} the hostname or "Unknown" if not found.
*/
export function getHref(httpOptions) {
if (httpOptions.uri && httpOptions.uri.href) {
return httpOptions.uri.href;
} else if (httpOptions.hostname && httpOptions.path) {
return httpOptions.hostname + httpOptions.path;
} else if (httpOptions.host && httpOptions.path) {
return httpOptions.host + httpOptions.path;
}
return 'Unknown';
}
2 changes: 2 additions & 0 deletions test/scenarios/localhost.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('live requests', function() {
server.listen(8585);
});

after(server.close);

it('passes', function() {
assert(true);
});
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mocha from './utils/runner.js';

describe('Mocha live detect', function() {
describe('without live calls', function() {
mocha('./test/scenarios/no_live.js -R tap --require dist/index.js');
mocha('./test/scenarios/no_live.js -R tap --require dist/register.js');

it('does not log any live request', function() {
assert.include(res.out, 'Hostnames requested');
Expand All @@ -14,7 +14,7 @@ describe('Mocha live detect', function() {
});

describe('with local calls', function() {
mocha('./test/scenarios/localhost.js -R tap --require dist/index.js');
mocha('./test/scenarios/localhost.js -R tap --require dist/register.js');

it('does not log any live request', function() {
assert.include(res.out, 'Hostnames requested');
Expand All @@ -24,7 +24,7 @@ describe('Mocha live detect', function() {
});

describe('with live calls', function() {
mocha('./test/scenarios/live_requests.js -R tap --require dist/index.js');
mocha('./test/scenarios/live_requests.js -R tap --require dist/register.js');

it('logs live call in test', function() {
assert.include(res.out, 'Live requests');
Expand Down