4.0.2 • Published 5 years ago

@zemke/http-mock v4.0.2

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

http-mock npm version

Serve JSON mocks conveniently.

Usage

// Start server on port 3333.
const httpMock = require('@zemke/http-mock')(3333);

// When http://localhost:3333/api/tournament is requested
// respond with contents of the JSON file under the given path.
httpMock.add('/api/tournament', __dirname + '/mocks/api_tournament.json');

Examples

httpMock.add("/api/heythere/1", {hello: "world"});
httpMock.add("/api/blah/1", __dirname + '/1.json');
httpMock.add(/\/api\/else\/(.+)/, __dirname + '/$1.json');
httpMock.add("/api/something", (req, payload) => payload);

API

add(urlMatcher: string | RegExp, mock: string | object | Function<http.IncomingMessage, object>)

Add a new mock for the given URL pattern. Performs a replace, if the URL pattern already exists.

urlMatcher — May be a string to match the exact path or a regular expression.

mock — A path to a JSON file or inline JSON object or a handler function with params http.IncomingMessage and the POST payload. If urlMatcher is regular expression, you can do replace operations here (see example above).

clean()

Remove all mocks.

close(Function<>)

Close the server with an optional callback method when the closing errors.

Examples

httpMock.add(/\/api\/procedures\/(.+)/, __dirname + '/mocks/$1.json');
4.0.1

5 years ago

4.0.0

5 years ago

4.0.2

5 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago