3.0.0 • Published 4 months ago

http-test-server v3.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
4 months ago

http-test-server

Create a simple http server for tests

Installation

Download node at nodejs.org and install it, if you haven't already.

npm install http-test-server --save

Usage

/* eslint-disable import/no-extraneous-dependencies */

import test from "tape";
import got from "got";
import httpTestServer from "http-test-server";

test("simple GET", async (t) => {
  t.plan(3);

  const server = await httpTestServer((req, res) => {
    t.equal(req.url, "/foo");
    t.equal(req.method, "GET");
    res.end("beep boop");
  });

  const { body } = await got(`${server.baseUrl}/foo`);
  t.equal(body, "beep boop");

  await server.shutdown();
  t.end();
});

test("simple POST", async (t) => {
  t.plan(5);

  const server = await httpTestServer((req, res) => {
    t.equal(req.url, "/foo");
    t.equal(req.method, "POST");
    t.equal(req.body.toString(), "heja");
    res.statusCode = 201;
    res.end("beep boop");
  });

  const { body, statusCode } = await got(`${server.baseUrl}/foo`, {
    body: "heja",
    method: "post",
  });

  t.equal(body, "beep boop");
  t.equal(statusCode, 201);

  await server.shutdown();
  t.end();
});

Tests

npm install
npm test

Dependencies

Dev Dependencies

  • @types/node: TypeScript definitions for node
  • @types/stream-to-promise: TypeScript definitions for stream-to-promise
  • @types/tape: TypeScript definitions for tape
  • got: Human-friendly and powerful HTTP request library for Node.js
  • package-json-to-readme: Generate a README.md from package.json contents
  • tape: tap-producing test harness for node and browsers
  • ts-node: TypeScript execution environment and REPL for node.js, with source map support
  • typescript: TypeScript is a language for application scale JavaScript development

License

MIT

Generated by package-json-to-readme

3.0.0

4 months ago

2.0.1

8 years ago

2.0.0

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago