npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stubnet

v1.0.0

Published

Library for stubbing client/server communication in integration tests.

Downloads

2

Readme

Stubnet

Stubnet is a testing library for creating fake TCP servers that can be connected to in automated tests, which will behave in a defined manner. Server behavior is defined as a sequential set of steps of data received and sent.

###Example Test (tap)

var test    = require('tap').test;
var stubnet = require('stubnet');
var net     = require('net');

test('basic connection', function (t) {

	stubnet()
		.listenTo({port: 50000, done: onReady})
		.expectConnection()
		.expectData('hello')
		.thenSend('hi there')
		.expectDisconnect()
		.start(onFinish);

	function onReady() {
		var socket = new net.Socket();
		socket.connect(50000, 'localhost', function () {
			t.pass('net.connect');
			socket.write('hello', function () {
				t.pass('net.send "hello"');
			});
			socket.on('data', function (data) {
				t.equal(data.toString(), 'hi there');
				socket.end(function () {
					t.pass('net.end');
				});
			});
		});
	}

	function onFinish(failed) {
		if (failed) {
			t.fail(failed);
		} else {
			t.pass('test complete');
		}
		t.end();
	}

});

##Usage

####stubnet()

Returns a Builder instance, a chainable class for defining the steps of the server process.

####builder.listenTo(options)

Note, this must be the first step that you define, as it informs the server where to listen for connections.

  • options

    • arguments: Optional array of arguments to pass to server.listen()
    • port: The port number to listen on. Required unless defined in the arguments option.
    • bind: The address to bind to (Defaults to 0.0.0.0).
    • secure: Optional. If defined, stubnet will create a tls server with the options defined.
    • pass: Callback to invoke when the server starts successfully and is ready for connections.
    • fail: Callback invoked if the server fails to start. Will receive the socket error as the first argument.
    • done: Callback invoked in both success and failure conditions. Will receive the error as the first argument if one occurred.

####builder.expectConnection([options | callback])

Note, you must define at least one expectConnection condition. Failure to do so will cause the server to abort with an 'Unexpected connection' message.

  • options (optional)

    • timeout: Number of milliseconds to wait for a connection before failing the test.
    • pass: Callback to invoke when the connection is received.
    • fail: Callback invoked if no connection is received before the timeout occurs.
    • done: Callback invoked in both success and failure conditions. Will receive an AssertionError as the first argument if the step failed
  • callback: A done callback function may optionally be provided in place of options.

####builder.expectData(data, [options | callback])

####builder.expectDataFrom(index, data, [options | callback])

The expectData step scan the socket data buffer until either all of the expected data is seen, or the buffer contains a mismatched value. Matched data is then removed from the data buffer.

  • index: If multiple connections are being made, expectDataFrom may be used to identify which connection (order of connecting, zero-based).

  • data may be any acceptable input for a Buffer, including strings, integer arrays, or pre-made buffers.

  • options (optional)

    • timeout: Number of milliseconds to wait for the data to fully arrive.
    • pass: Callback to invoke when the matching data is received.
    • fail: Callback invoked if the data does not match, or the complete data is not received before the timeout occurs. Receives an equality AssertionError as the first argument.
    • done: Callback invoked in both success and failure conditions. Will receive an AssertionError as the first argument if the step failed
  • callback: A done callback function may optionally be provided in place of options.

####builder.thenSend(data, [options | callback]) ####builder.thenSendTo(index, data, [options | callback])

When this step evaluates it will send the provided data to the socket. The step passes when the send buffer is fully drained to the system kernal.

  • index: If multiple connections are being made, thenSendTo may be used to identify which connection (order of connecting, zero-based).

  • data may be any acceptable input for a Buffer, including strings, integer arrays, or pre-made buffers.

  • options (optional)

    • pass: Callback to invoke when the data is sent.
    • fail: Callback invoked if an error occurred while sending the data. Will receive the error as the first argument.
    • done: Callback invoked in both success and failure conditions. Will receive the error as the first argument if one occurred.
  • callback: A done callback function may optionally be provided in place of options.

####builder.expectNoData([options | callback]) ####builder.expectNoDataFrom(index, [options | callback])

Step to confirm that no further data has been received from the socket before moving on to the next step.

  • index: If multiple connections are being made, expectNoDataFrom may be used to identify which connection (order of connecting, zero-based).

  • options (optional)

    • pass: Callback to invoke if the data buffer is empty.
    • fail: Callback invoked if the data buffer is NOT empty. Receives an equality AssertionError as the first argument.
    • done: Callback invoked in both success and failure conditions. Will receive an AssertionError as the first argument if the step failed
  • callback: A done callback function may optionally be provided in place of options.

####builder.expectDisconnect([options | callback]) ####builder.expectDisconnectBy(index, [options | callback])

Waits for the socket to close from the other side.

  • index: If multiple connections are being made, expectDisconnectBy may be used to identify which connection (order of connecting, zero-based).

  • options (optional)

    • timeout: Number of milliseconds to wait for the socket to close before failing the test.
    • pass: Callback to invoke when the connection is received.
    • fail: Callback invoked if no connection is received before the timeout occurs.
    • done: Callback invoked in both success and failure conditions. Will receive an AssertionError as the first argument if the step failed.
  • callback: A done callback function may optionally be provided in place of options.

####builder.thenClose([index],[options | callback])

Step to confirm that no further data has been received from the socket before moving on to the next step.

  • index: If multiple connections are being made, the connection index (order of connecting, zero-based) may be used to identify which connection to close.

  • options (optional)

    • pass: Callback to invoke when the connection closes.
    • fail: Callback invoked if the connection closure times out, or another error occurs.
    • done: Callback invoked in both success and failure conditions. Will receive the error as the first argument if one occurred.
  • callback: A done callback function may optionally be provided in place of options.

####builder.start([options | callback])

Start the server. This function returns a runner object.

  • options (optional)

    • pass: Callback to invoke when all steps have completed successfully.
    • fail: Callback invoked if any of the steps have failed. Receives the same error that was provided to that step's fail function.
    • done: Callback invoked in both success and failure conditions. Will receive the error as the first argument if one occurred.
  • callback: A done callback function may optionally be provided in place of options.

####runner.abort()

Aborts the run, stopping the server and triggering a failure condition on the current step.