Socket
Book a DemoInstallSign in
Socket

bench-warmer

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

bench-warmer

A nearly-complete test bench for the NodeJS HTTP interface

unpublished
latest
Source
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

Bench Warmer

A nearly-complete test bench for the NodeJS HTTP interface.

Build Status

NPM Package

Rule The First of Unit Testing: Thou shalt not create any network sockets

Rule The Second of Unit Testing: GOTO Rule The First of Unit Testing

Bench.Server

Create an interface for testing server request handlers, like an Express app.

const app = Express();
const server = Bench.createServer(app)

// Attach some body-parser middleware (https://github.com/expressjs/body-parser)
app.use(BodyParser.raw());

app.get('/', (req, res) => res.send('hello!'));
app.post('/', (req, res) => {

  res.send('hello!');
});

server.request({ path: '/' })
  .spread((req, res) => {
    /* do unit tests here! */
  })
  .then(() => server.request({
    path: '/', method: 'POST'
  }, (req) => {
    req.write('this is a test!');
    req.end();
  }))
  .spread((req, res) => {
    expect(res.statusCode).to.equal(200);
  });

Bench.createServer(onRequest) returns an instance of Bench.Server. Calling #request(params, before) on the Server instance returns an extension of Promise with a #spread() helper. The Promise resolves with the IncomingMessage (request) and ServerResponse instances that were passed through the code under test for assertion testing.

Mocha handles promises in test cases, allowing the result of a request to be returned for asynchronous control:

describe('An HTTP test', function() {
  const app = Express();
  const server = Bench.createServer(app)

  it('does something', function() {
    return server.request({/* params */})
      .spread((req, res) => {
        expect(res.statusCode).to.equal(200);
      });
  });
});

Bench.Wire

The Wire class provides a simulated, observable channel between client and server interfaces. It can be used to test client code in isolation, or to simulate interactions between client and server modules without any network resources.

const client = new Client(/* ... */);
const app = Express();
const wire = Bench.createWire(app);

/*
 * Client libraries must provide an interface to inject a `Wire`
 * instance as their `#request()` provider. This assumes that
 * `client._provider` was a reference to something like `HTTP`.
 */
client._provider = wire;

/*
 * The Wire instance emits events to tap client request and server
 * response events for testing
 */
wire.once('request', (req) => {
  expect(req.path).to.equal('something')
});

wire.once('response', (res) => {
  expect(res.statusCode).to.equal(200)
});

/*
 * Call the client interface's methods to make and capture
 * request/response interactions.
 */
client.makeSomeRequest(/* parameters */)

Bench.Reader

The Reader class is a helper for aggregating data from Readable streams for testing.

/*
 * A Wire instance can be used to unit-test client interactions without any
 * reciprocal server code.
 */
const wire = Bench.createWire((req, res) => {
  Bench.createReader(req, (reader) => {
    expect(reader.body).to.equal('content');

    res.write('stuff');
    res.end();
  })
});
const client = new Client(/* ... */);

client._provider = wire;

// ...

FAQs

Package last updated on 04 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts