1.0.3 • Published 5 years ago

@samzh72/httpunit v1.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

httpunit

Programable HTTP server for micro-service unit testing

Getting Started

httpunit can be used for mocking any micro-service by setting up your own server and route handler programmally. With the ability of programming the server, unit test codes could expect any response from server as your wish.

httpunit listens on a pre-configured admin port after launched, from which unit tests could calls to the admin API to create, delete servers and set expect response for specified routes.

After the server created and route handler successfully set by admin APIs, the code to be tested could then issue requests to httpunit. The expected responses will send back to code to be tested.

httpunit works in two modes: mock mode or proxy mode.

Mock mode

In mock mode, httpunit acts as the micro-service you are going to mock. The code to be tested connect to httpunit just like connect to the real micro-service.

npm.io

Proxy mode

httpunit could act as an HTTP/HTTPS proxy as well. httpunit could modify the response from target service for any need of unit test. For example, simulate HTTP 404 for a specific resource even it exists in the real world.

The real micro-services work behind the proxy, and the unit test could mock only specific APIs.

As well, everything is programmable.

Installing

npm install -D @samzh72/httpunit

Launching server:

var hu = require('httpunit');
hu.serve({
    mockOptions: {
        port: 8000
    },
    proxyOptions: {
        port: 8001
    }
});

Both mockOptions and proxyOptions are optional. Mock mode or proxy mode will be launched only when the option specified.

Mock APIs

httpunit provides very simple APIs to create, delete mock servers and set handlers for testing routes.

Create server

A mock server will be created on a specified port and bind to a specified IP. But there're no route handlers installed for this server yet. Any requests to this server will get HTTP 404 response.

Request:

pathmethod
/serverPOST

Request body:

{
    port: 9000,
    host: '127.0.0.1'
}
fieldoptionalcomments
portnowhere the mock-service will listen on
hostyeswhich ip to be bind, default bind to 0.0.0.0

Response:

{
    serverId: 12345678
}
fieldcomments
serverIdused for identifying the mock server in further API

Delete server

Mock server could be shut down. But it's not always necessary, especially while httpunit running in process of unit test.

Request:

pathmethod
/server/:serverIdDELETE

No body in response, status code indicates result of the API call.

Install route handler

Define the behavior of mock server for the specific route.

Request:

pathmethod
/:serverId/handlerPUT

Request body:

{
    path: '/abc/:id',
    method: 'GET',
    response: {
        delay: 200,
        status: 200,
        headers: {
            'X-any-header': 'any header value'
        },
        body: 'any string or object',
        cookies: [
            {
                name: 'any-cookie-name',
                value: 'cookie-value',
                signed: false,
                domain: 'google.com',
                httpOnly: false,
                expires: '2019-05-11T15:25:37.000Z',
                maxAge: 2000,
                path: '/',
                secure: false,
                sameSite: 'strict'
            }
        ]
    }
}
fieldoptionalcomments
pathnoroute path to be installed, string starts with '/'
methodnoHTTP method
responsenoresponse behavior
delayyesundefined: response immediately
0: never response, request will be rejected
others: number in milliseconds, response will be sent only after this delay
statusnoresponse status code
headersyesarray of headers to be appended to response. header field will be converted into lower case in response
bodyyesresponse body, could be string or JSON object
cookiesyescookies to be appended to response
namenocookie name
valuenocookie value
signeyestrue for signed cookie, sign secret used in httpunit is 'secret'
domainyesit's optional, but in most case it should be there
httpOnlyyestrue for httpOnly cookies
expiresyesGMT time, 0 for session cookie
maxAgeyesmax-age in milliseconds
pathyesdefault to '/'
secureyestrue to be used with HTTPS only
sameSiteyes'strict' or 'lax'

No body in response, status code indicates result of the API call.

Proxy APIs

TBD