
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
http-mockserver
Advanced tools
$ npm install --save http-mockserver
const { mockServer } = require('http-mockserver');
// Static mock
mockServer.addMock({
port: 8080,
method: 'GET',
uri: '/my/url',
response: {
body: 'Hello world'
}
});
// Dynamic mock
let counter = 0;
mockServer.addMock({
port: 8080,
method: 'GET',
uri: '/my/other/url',
handler: function (req, res) {
counter++;
res.send(`Counter: ${counter}`);
}
});
To mock an endpoint, the mock object should follow one of these structures:
4000
/users/peter
GET
{data: "hello"}
404
(default: 200){"Content-Type": "application/json"}
4000
/users/peter
GET
function(req, res) {...}
The following methods are available on both mockServer
and mockClient
Mock and endpoint of a port and route, with a specific response (static mock) or a handler function (dynamic mock).
Arguments:
Remove all mocks and clear the request log
Returns a mockserver instance with the same API interface, but the methods do not require a port to be specified.
Arguments:
Example:
const { mockServer } = require('http-mockserver');
const backendService = mockServer.create(8888);
backendService.addMock({
uri: '/some/url/to/mock',
method: 'GET',
response: {
body: 'Hello world!'
}
});
$ curl localhost:8888/some/url/to/mock
"Hello world!"
Returns request log. If port is specified, request will be filtered by this
Arguments:
Returns a list of request logs that the predicate returns truthy for. The predicate is invoked with three arguments: (requestLog, index, requestLogs).
Arguments:
The following methods are available only available on mockServer
Start mockserver on the specified port.
Arguments:
Stop mockserver
The following methods are available only available on mockClient
.
You only need to use the client if you are communicating with a mockServer that was started from commandline or by a separate Node process with mockServer.start().
Set hostname and port of mockserver. This is necessary if you start MockServer on another port than the default (port 3000).
Arguments:
localhost:3000
)If you need to interact with the mockserver from other languages that Node.js, you can start it as a stand-alone process, and add mocks by interacting with the REST api.
To start mockServer from the command-line run http-mockserver
.
You can also use the CLI tool to start a mockserver with some preconfigured mocks, and load them on startup with http-mockserver --mocks ./mock-folder
$ http-mockserver --help
Usage: http-mockserver [options]
Options
--port MockServer port, default: 3000
--mocks Path to mock config files
FAQs
Testing made easy with mocked http servers
The npm package http-mockserver receives a total of 388 weekly downloads. As such, http-mockserver popularity was classified as not popular.
We found that http-mockserver demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.