Package detail

tiny-httptest

avoidwork212BSD-3-Clause4.0.13

Lightweight HTTP test framework

http, https, test, framework

readme

Tiny HTTP Test

tiny-httptest is a lightweight HTTP test framework that makes it easy to validate CORS is working, capture cookies & HTTP response headers (including etags) and reuse them for sequential tests.

Using the factory

import {httptest} from "tiny-httptest";

describe('HTTP Tests', () => {
 it("GET /somefile (Captured the etag)", function () {
  return httptest({url: `http://localhost:${port}/somefile`})
          .etags()
          .expectStatus(200)
          .end();
 });

 it("GET /somefile (Reused the ETag)", function () {
  return httptest({url: `http://localhost:${port}/somefile`})
          .etags()
          .expectStatus(304)
          .end();
 });

 it("GET /invalid-file", function () {
  return httptest({url: `http://localhost:${port}/invalid-file`})
          .expectStatus(404)
          .end();
 });
});

Using the Class

import {HTTPTest} from "tiny-httptest";
class MyTestRunner extends HTTPTest {}

Testing

Tiny HTTP Test has 100% code coverage with its tests. Run npm run test-setup after installing modules.

  Implicit proofs
    √ Starting test server
    √ GET / (captures cookie, CSRF token)
    √ HEAD / (reuses cookie)
    √ POST / (reuses cookie & CSRF token)
    √ POST / (reuses cookie & CSRF token + body)
    √ POST / (reuses cookie & CSRF token + body)
    √ GET / (CORS Pre-flight)
    √ GET / (CORS)
    √ GET /invalid (CORS)
    √ GET / (Basic Auth)
    √ GET https://google.com/ (HTTPS) (94ms)
    √ GET /assets/css/style.css
    √ GET /assets/css/style.css (ETag)
    √ Stopping test server

  Error proofs
    √ Starting test server
    √ GET https://invalid.local.dev/ (DNS error)
    √ INVALID / (Invalid HTTP method)
    √ GET /hello (Error thrown)
    √ GET /assets/css/style.css (Invalid 404)
    √ Stopping test server


  20 passing (160ms)

-------------------|---------|----------|---------|---------|-----------------------------------------------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-----------------------------------------------------------
All files          |     100 |    81.13 |     100 |     100 |                                                          
 tiny-httptest.cjs |     100 |    81.13 |     100 |     100 | 16-22,111,125-130,177,193,211-227,247,262,303,318-323,371
-------------------|---------|----------|---------|---------|-----------------------------------------------------------

Options

body

HTTP request body, defaults to null but can be String, Object or Array.

headers

HTTP request headers, defaults to {}.

method

HTTP request method, defaults to GET.

timeout

HTTP request timeout as milliseconds, defaults to 30000.

url

URL & port to request, defaults to http://localhost.

API

captureHeader(name)

Captures a header to be reused by another instance of TinyHTTPTest.

cookies([state = true])

Enables or disables cookie capture & reuse.

cors([hostname, success = true])

Sets request & response header expectations, defaults to request hostname if optional argument is not supplied.

If testing an error case, you must specify the second parameter as false to not expect CORS headers.

end()

Ends the request, Promise resolves with TinyHTTPTest instance or rejects with Error.

etags([state = true])

Enables or disables ETag capture & reuse.

expectBody([value = /\w+/])

Sets an expectation of the response body, default value is a RegExp which tests if there is a response body.

expectHeader(name, [value = /\w+/])

Sets an expectation of a response header, default value is a RegExp which tests if there is a header value.

expectJson()

Sets an expectation of response header content-type, supports correct and common incorrect header values.

expectStatus([value = 200])

Sets an expectation of response status code, default value is 200.

json([arg])

Sets request & response to JSON, sends arg if not undefined.

process()

Processes the response of the TinyHTTPTest instance.

reuseHeader(name)

Reuses a captured header from another instance of TinyHTTPTest.

send(arg)

Decorates arg as request body & sets request headers.

test(arg, value, err)

Validates that arg is equal to or passes value test, throws Error with err as message if invalid.

License

Copyright (c) 2024 Jason Mulligan Licensed under the BSD-3 license.

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

4.0.11

  • Fixing a copy/pasta error & getting the fifth CORS header on error 42791d8

4.0.10

20 January 2024

  • Bump rollup from 4.4.1 to 4.5.1 #33
  • Bump typescript from 5.2.2 to 5.3.2 #32
  • Bump eslint from 8.53.0 to 8.54.0 #30
  • Bump rollup from 4.4.0 to 4.4.1 #29
  • Bump rollup from 4.3.0 to 4.4.0 #28
  • Removing CORS headers expectations on errors b24b027
  • Generating CHANGELOG.md 512278f

4.0.9

10 November 2023

  • Fixing cors() such that it removes the expectation of the exposed allow value, updating README.md, removing ci workflow for github bcaa0ed
  • Generating CHANGELOG.md 1ccaeb1
  • Updating npm:test script 842ac8d

4.0.8

2 November 2023

  • Updating engines.node to lower the version to the real required version, updating ./github/workflows/ci.yml 8667b87
  • Version bump to release change to package.json 956e075
  • Generating CHANGELOG.md c92a0c0

4.0.7

10 October 2023

  • Setting extension of main to .cjs for deprecated tooling 1fa0529
  • Generating CHANGELOG.md f455cee
  • Setting CI to use node.js 20 only (for now) dbab594

4.0.6

6 October 2023

4.0.5

4 October 2023

  • Updating type definitions, version bump ae09501
  • Generating CHANGELOG.md 1ac7448

4.0.4

4 October 2023

  • Creating test-setup npm script 6de9b36
  • Generating CHANGELOG.md ae77e36

4.0.3

4 October 2023

  • Fixing an install regression by moving commands to prepare key 3a6c08f

4.0.2

4 October 2023

  • Updating tiny-coerce dependency b251089
  • Generating CHANGELOG.md b3ec286

4.0.1

4 October 2023

  • Creating constants & shared files 807b349
  • Cleaning up imports df31142
  • Removing a copy/pasta error from package.json, version bump 79fdd34

4.0.0

1 October 2023

  • Breaking change on output files (WIP) baec6b5
  • Removing a conditional statement that can't be reached with current design, renaming a method to request(), updating .eslintrc 5aa7fac
  • Updating README.md, generating new type definitions, exporting HTTPTest class for extending 9644363

3.0.4

21 February 2023

  • Fix https #10
  • Updating dependencies f10993a
  • Fixing regression affecting https testing 1c9c54d
  • Version bump to release, updating CHANGELOG.md 790bf8d

3.0.3

5 October 2022

  • Building a new version just to sync numbers 5d8b0a7
  • Updating CHANGELOG.md 6522d67

3.0.2

4 October 2022

  • Updating CHANGELOG.md 7eeeede
  • Version bump to deal with npmjs.com cache flaw 511930b

3.0.1

4 October 2022

  • Adding a CHANGELOG.md file c38dc3e
  • Fixing a bundling error, updating test to exit gracefully 7d180f6
  • Removing travis-ci.org badge from README.md a5e3f19

3.0.0

22 September 2022

  • Refactoring to es module syntax #9
  • Updating rollup configuration, version bump to release 5e6169e
  • Updating lint rules 30dfd06

2.0.0

12 October 2021

  • Bump acorn from 7.0.0 to 7.1.1 #6
  • Removing support for http2 tests (pointless), updating warning() to account for regular expressions, updating LICENCE & README.md, updating dev dependencies without changing the tooling 0bf3bb2
  • Removing unneeded grunt module 2b202d8

1.1.1

4 September 2019

  • webstorm @#$)(@#&%&)!!! afbae34
  • Updating http2 request logic for redirects, fixing a lint error ae620a7

1.1.0

7 March 2019

  • Adding second parameter to cors() to support testing a failure case 477b1f5
  • Updating copyright years 1da9321

1.0.22

25 July 2018

  • Fixing erroneous header value message, updating dependencies & tests f14f7a7

1.0.21

8 July 2018

  • Updating error message format df9b4d3

1.0.20

8 July 2018

  • Updating description & README 491c3ce

1.0.19

8 July 2018

  • Fixing a regression of options.path from when switching the parser f97e061

1.0.18

29 June 2018

  • Fix for new parser regression #5
  • Making the warning messages DRY 9abd666
  • Revert "Fixing regression from changing the parser" 9a8b26d
  • Updating to 1.0.17 code for debugging 39ad38f

1.0.17

27 June 2018

1.0.16

27 June 2018

1.0.15

27 June 2018

  • Updating logging, using template literals, updating travis-ci.org configuration, updating dependencies 1bc54bc
  • Minor tweaks db04fa1
  • Updating error messages ec06fcc

1.0.14

16 January 2018

  • Refactoring the code to be DRY, http2 tests have an interesting OOO response problem between body & headers 6e8295d
  • Adding http2 tests which fail atm a27f830
  • Upgrading dev dependency so test doesn't blow up feaf62b

1.0.13

14 January 2018

  • HTTP2 #4
  • Setting Authorization header 4f0d9a7

1.0.12

28 November 2017

  • Version bump to re-gen lock file 93c7dc1

1.0.11

28 November 2017

  • Updating README, version bump to release it d17a1a0

1.0.10

28 November 2017

1.0.9

15 October 2017

  • Adding a package-lock.json for npm@5 4d537f7
  • Adding an expectation for access-control-allow-credentials on CORS requests, updating tenso & tests 1bfb64c

1.0.8

27 May 2017

  • Updating CORS tests fbd8312
  • Fixing CORS header expectations e459bf3
  • Updating keywords in package.json 1cd6558

1.0.7

22 March 2017

  • Handling numeric assertions for number & string inputs b58d700

1.0.6

4 March 2017

  • Fixing test() for Function output 2cf97d4
  • Updating README intro 901502a

1.0.5

20 February 2017

  • Lowercasing header names for storage, updating a test 56e6063

1.0.4

20 February 2017

  • Adding an accept header via expectJson(), creating jsonMimetype constant for a common string 3732742

1.0.3

20 February 2017

  • Removing regex.maybeJsonBody & creating regex.notEmpty, replacing adhoc default RegExp values with regex.notEmpty, updating test descriptions, updating README 3f82c52

1.0.2

19 February 2017

  • Adding etags() to capture & reuse etag headers, adding a test for a head request which exercises expectBody() dd7a002

1.0.1

19 February 2017

  • Fixing the order of arguments for this.test() when testing body, fixes #2 #2
  • Ignoring .idea folder, fixes #1 #1
  • Updating signatures of process() & test() to return this c0a0476

1.0.0

19 February 2017

  • Adding a grunt build & eslint rules 03a7615
  • Adding first mocha test using tenso for base (tested with hippie) 3024dac
  • Fleshing out class (WIP) ab60ef6