All Projects β†’ lukechilds β†’ Create Test Server

lukechilds / Create Test Server

Licence: mit
Creates a minimal Express server for testing

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Create Test Server

Telegraf-Test
Telegraf Test - Simple Test ToolKit of Telegram Bots
Stars: ✭ 22 (-81.2%)
Mutual labels:  test, tests
node-bogota
πŸš€ Run tape tests concurrently with tap-spec output
Stars: ✭ 15 (-87.18%)
Mutual labels:  test, tests
dojos
Alguns desafios para os participantes dos grupos de estudo
Stars: ✭ 33 (-71.79%)
Mutual labels:  test, tests
PixelTest
Fast, modern, simple iOS snapshot testing written purely in Swift.
Stars: ✭ 56 (-52.14%)
Mutual labels:  test, tests
Capture Stream
Capture stream output.
Stars: ✭ 10 (-91.45%)
Mutual labels:  test, tests
playwright-test
Run unit tests with several runners or benchmark inside real browsers with playwright.
Stars: ✭ 81 (-30.77%)
Mutual labels:  test, tests
flyway-junit5-extensions
Flyway JUnit 5 Extension to clean / migrate your database in tests.
Stars: ✭ 14 (-88.03%)
Mutual labels:  test, tests
7182
Curso 7182 - Refatorando para testes de unidade
Stars: ✭ 21 (-82.05%)
Mutual labels:  test, tests
Blink Java
Simplified pure Java http server
Stars: ✭ 10 (-91.45%)
Mutual labels:  server, test
Javatech
β˜•οΈ 汇总 Java εΌ€ε‘δΈ­εΈΈθ§ηš„δΈ»ζ΅ζŠ€ζœ―ηš„εΊ”η”¨γ€η‰Ήζ€§γ€εŽŸη†γ€‚
Stars: ✭ 310 (+164.96%)
Mutual labels:  server, test
mutode
Mutation testing for JavaScript and Node.js
Stars: ✭ 61 (-47.86%)
Mutual labels:  test, tests
Start Server And Test
Starts server, waits for URL, then runs test command; when the tests end, shuts down server
Stars: ✭ 879 (+651.28%)
Mutual labels:  server, test
phpunit-injector
Injects services from a PSR-11 dependency injection container to PHPUnit test cases
Stars: ✭ 62 (-47.01%)
Mutual labels:  test, tests
unittest expander
A library that provides flexible and easy-to-use tools to parameterize Python unit tests, especially those based on unittest.TestCase.
Stars: ✭ 12 (-89.74%)
Mutual labels:  test, tests
arduino-ci-script
Bash script for continuous integration of Arduino projects
Stars: ✭ 25 (-78.63%)
Mutual labels:  test, tests
BDTest
BDTest - A Testing Framework for .NET
Stars: ✭ 58 (-50.43%)
Mutual labels:  test, tests
Should.js
BDD style assertions for node.js -- test framework agnostic
Stars: ✭ 1,908 (+1530.77%)
Mutual labels:  test, tests
Study
A simple, progressive, client/server AB testing library πŸ“š
Stars: ✭ 293 (+150.43%)
Mutual labels:  server, test
Test Pack
A Symfony Pack for functional testing
Stars: ✭ 865 (+639.32%)
Mutual labels:  test, tests
Cypress
Fast, easy and reliable testing for anything that runs in a browser.
Stars: ✭ 35,145 (+29938.46%)
Mutual labels:  test, tests

create-test-server

Creates a minimal Express server for testing

Build Status Coverage Status npm npm

Inspired by the createServer() helper function in the Got tests.

A simple interface for creating a preconfigured Express instance listening for both HTTP and HTTPS traffic.

Ports are chosen at random for HTTP/HTTPS. A self signed certificate is automatically generated, along with an associated CA certificate for you to validate against.

Created because mocking is dirty and can break between Node.js releases. Why mock HTTP requests when you can test locally against a real server in a few lines code?

Install

npm install --save-dev create-test-server

Usage

const createTestServer = require('create-test-server');

const server = await createTestServer();
console.log(server.url);
// http://localhost:5486
console.log(server.sslUrl);
// https://localhost:5487

// This is just an Express route
// You could use any Express middleware too
server.get('/foo', (req, res) => {
  res.send('bar');
});

// You can return a body directly too
server.get('/foo', () => 'bar');
server.get('/foo', 'bar');

// server.url + '/foo' and server.sslUrl + '/foo' will respond with 'bar'

The following Content-Type headers will be parsed and exposed via req.body:

  • JSON (application/json)
  • Text (text/plain)
  • URL-encoded form (application/x-www-form-urlencoded)
  • Buffer (application/octet-stream)

You can change body parsing behaviour with the bodyParser option.

createTestServer() has a Promise based API that pairs well with a modern asynchronous test runner such as AVA.

You can create a separate server per test:

import test from 'ava';
import got from 'got';
import createTestServer from 'create-test-server';

test(async t => {
  const server = await createTestServer();
  server.get('/foo', 'bar');

  const response = await got(`${server.url}/foo`);
  t.is(response.body, 'bar');

  await server.close();
});

Or share a server across multiple tests:

let server;

test.before(async () => {
  server = await createTestServer();
  server.get('/foo', 'bar');
});

test(async t => {
  const response = await got(`${server.url}/foo`);
  t.is(response.body, 'bar');
});

test(async t => {
  const response = await got(`${server.url}/foo`);
  t.is(response.statusCode, 200);
});

test.after(async () => {
	await server.close();
});

You can also make properly authenticated SSL requests by setting a common name for the server certificate and validating against the provided CA certificate:

test(async t => {
  const server = await createTestServer({ certificate: 'foobar.com' });
  server.get('/foo', 'bar');

  const response = await got(`${server.sslUrl}/foo`, {
    ca: server.caCert,
    headers: { host: 'foobar.com' }
  });
  t.is(response.body, 'bar');

  await server.close();
});

You can still make an SSL connection without messing about with certificates if your client supports unauthorised SSL requests:

test(async t => {
  const server = await createTestServer();
  server.get('/foo', 'bar');

  const response = await got(`${server.sslUrl}/foo`, {
    rejectUnauthorized: false
  });
  t.is(response.body, 'bar');

  await server.close();
});

You can also easily stop/restart the server. Notice how a new port is used when we listen again:

const server = await createTestServer();
console.log(server.port);
// 56711

await server.close();
console.log(server.port);
// undefined

await server.listen();
console.log(server.port);
// 56804

API

createTestServer([options])

Returns a Promise which resolves to an (already listening) server.

options

Type: object

options.certificate

Type: string, object
Default: undefined

SSL certificate options to be passed to createCert().

options.bodyParser

Type: object | boolean
Default: undefined

Body parser options object to be passed to body-parser methods.

If set to false then all body parsing middleware will be disabled.

server

Express instance resolved from createTestServer()

This is just a normal Express instance with a few extra properties.

server.url

Type: string, undefined

The url you can reach the HTTP server on.

e.g: 'http://localhost:5486'

undefined while the server is not listening.

server.port

Type: number, undefined

The port number you can reach the HTTP server on.

e.g: 5486

undefined while the server is not listening.

server.sslUrl

Type: string, undefined

The url you can reach the HTTPS server on.

e.g: 'https://localhost:5487'

undefined while the server is not listening.

server.sslPort

Type: number, undefined

The port number you can reach the HTTPS server on.

e.g: 5487

undefined while the server is not listening.

server.caCert

Type: string

The CA certificate to validate the server certificate against.

server.http

Type: http.server

The underlying HTTP server instance.

server.https

Type: https.server

The underlying HTTPS server instance.

server.listen()

Type: function

Returns a Promise that resolves when both the HTTP and HTTPS servers are listening.

Once the servers are listening, server.url and server.sslUrl will be updated.

Please note, this function doesn't take a port argument, it uses a new randomised port each time. Also, you don't need to manually call this after creating a server, it will start listening automatically.

server.close()

Type: function

Returns a Promise that resolves when both the HTTP and HTTPS servers have stopped listening.

Once the servers have stopped listening, server.url and server.sslUrl will be set to undefined.

Related

License

MIT Β© Luke Childs

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].