tiny-httptest

Lightweight HTTP compliant test framework

Usage no npm install needed!

<script type="module">
  import tinyHttptest from 'https://cdn.skypack.dev/tiny-httptest';
</script>

README

Tiny HTTP Test

Lightweight HTTP compliant test framework

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

build status

Example

const tinyhttptest = require("tiny-httptest");

// Simulating CORS request to localhost:8000

tinyhttptest({url:"http://localhost:8000", method: "OPTIONS"})
    .cors("http://not.localhost:8001")
    .end()
    .then(() => tinyhttptest({url:"http://localhost:8000"}).cors("http://not.localhost:8001").expectJson().end())
    .then(() => console.log("CORS is working"))
    .catch(e => console.error(`CORS is not working: ${e.message}`));

Configuration

body

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

headers

(Object) HTTP request headers, defaults to {}.

method

(String) HTTP request method, defaults to GET.

timeout

(Number) HTTP request timeout as milliseconds, defaults to 30000.

url

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

API

captureHeader(name) TinyHTTPTest

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

cookies([state = true]) TinyHTTPTest

Enables or disables cookie capture & reuse.

cors([hostname, success = true]) TinyHTTPTest

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() Promise

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

etags([state = true]) TinyHTTPTest

Enables or disables ETag capture & reuse.

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

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

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

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

expectJson() TinyHTTPTest

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

expectStatus([value = 200]) TinyHTTPTest

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

json([arg]) TinyHTTPTest

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

process TinyHTTPTest

Processes the response of the TinyHTTPTest instance.

reuseHeader(name) TinyHTTPTest

Reuses a captured header from another instance of TinyHTTPTest.

send(arg) TinyHTTPTest

Decorates arg as request body & sets request headers.

test(arg, value, err) TinyHTTPTest

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

License

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