Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
spur-mockserver
Advanced tools
A Node.js library with tools to allow for the creation of mock web servers for testing with mocks and real web servers.
A Node.js library with tools to allow for the creation of mock web servers for testing with mocks and real web servers.
The Spur Framework is a collection of commonly used Node.JS libraries used to create common application types with shared libraries.
Visit NPMJS.org for a full list of Spur Framework libraries >>
Dependencies:
$ npm install --save spur-ioc spur-config spur-common spur-web
Module:
$ npm install spur-mockserver --save
This example will only show the files that show unique configurations to a mock server. For a fully detailed example, please view the example here.
src/injector.js
const path = require('path');
const spur = require('spur-ioc');
const spurCommon = require('spur-common');
const spurWeb = require('spur-web');
const spurMockserver = require('spur-mockserver');
const spurConfig = require('spur-config');
const registerConfig = require('spur-common/registerConfig');
module.exports = function () {
const ioc = spur.create('spur-mockserver-example');
registerConfig(ioc, path.join(__dirname, './config'));
ioc.merge(spurCommon());
ioc.merge(spurWeb());
ioc.merge(spurMockserver());
ioc.registerFolders(__dirname, [
'mocks/'
'runtime/'
]);
return ioc;
};
src/runtime/WebServer.js
module.exports = function (MockWebServer) {
class WebServer extends MockWebServer {
}
return new WebServer();
}
src/mocks/UserMockEndpoint.js
By creating files with the ending name of *MockEndPoint.js
, it will self register as a mock controller. You can have multiple MockEndpoint files, as long as they don't share the same endpoint urls.
You can also have multiple request handlers in the same MockEndpoint and change which loads dynamically.
module.exports = function (MockEndpoint) {
class UserMockEndpoint extends MockEndpoint {
method() {
return MockEndpoint.METHODS.GET;
}
url() {
return '/user/:id';
}
default(req, res, next) {
const userId = parseInt(req.params.id);
const user = {
id: userId,
name: "User Name",
statusCode: 200
};
res.status(user.statusCode).json(user);
}
}
return new UserMockEndpoint();
};
start.js
const injector = require('./src/injector');
injector().inject(function (WebServer) {
// Starts the web server by loading all the default methods in the MockEndPoints
WebServer.startWithDefaults();
});
While you can have a standalone mock server, sometimes it's needed to use mock endpoints in an actual application. This scenario could be due to the need to be able to work on parts of the REST API that are defined and mock out parts that are not completely defined.
The following examples show how you mix them by adding a few calls to your web server app based on BaseWebServer defined in Spur-Web. For it's full configuration, take a look at the documentation in Spur-Web, but the following are a highlight of the dependency configuration needed to make it work.
src/injector.js
const path = require('path');
const spur = require('spur-ioc');
const spurCommon = require('spur-common');
const spurWeb = require('spur-web');
const spurMockserver = require('spur-mockserver');
const spurConfig = require('spur-config');
const registerConfig = require('spur-common/registerConfig');
module.exports = function () {
const ioc = spur.create('spur-mockserver-example');
// ... other dependencies
ioc.merge(spurMockserver());
ioc.registerFolders(__dirname, [
'mocks'
'runtime'
]);
return ioc;
};
src/runtime/WebServer.js
module.exports = function (BaseWebServer, config) {
class MockWebServer extends BaseWebServer {
constructor() {
super();
this.useDefaults = config.useMockDefaults;
}
registerMiddleware() {
super.registerMiddleware();
this.registerMockEndpoints();
}
registerMockEndpoints() {
Logger.log(`Attempting to register mock-endpoints - Use Defaults (${this.useDefaults})`);
MockEndpointRegistration.register(this.app, this, this.useDefaults);
}
setUseDefaults(useDefaults = false) {
this.useDefaults = useDefaults;
}
startWithDefaults() {
this.setUseDefaults(true)
return this.start();
}
}
return new MockWebServer();
};
The /v3/fixtures
middleware handler enables the customization of fixtures returned by mock endpoints from web browser clients, e.g. with XMLHttpRequest
or fetch
, addressing use cases of web UI developers and manual QA.
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/v3/fixtures');
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.send(JSON.stringify({
fixtures: [{
endpoint: 'MyMockEndpoint',
method: 'myMethod'
}]
}));
If the mock server is running on localhost
and the mock endpoint MyMockEndpoint
is registered and has a method myMethod
, the POST
request succeeds and successive requests that are handled by MyMockEndpoint
will run myMethod
to generate its response.
const xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/v3/fixtures');
xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
xhr.send(JSON.stringify({
fixtures: [{
endpoint: 'MyMockEndpoint',
json: { 'example':[1, 2, 3] }
}]
}));
If the mock server is running on localhost
and the mock endpoint MyMockEndpoint
is registered, the POST
request succeeds and successive requests that are handled by MyMockEndpoint
will respond with an HTTP status of 200
and the JSON {"example":[1, 2, 3]}
.
Please send in pull requests and they will be reviewed in a timely manner. Please review this generic guide to submitting a good pull requests. The only things we ask in addition are the following:
The majority of the settings are controlled using an EditorConfig configuration file. To use it please download a plugin for your editor of choice.
To run the test suite, first install the dependancies, then run npm test
$ npm install
$ npm test
FAQs
A Node.js library with tools to allow for the creation of mock web servers for testing with mocks and real web servers.
The npm package spur-mockserver receives a total of 1 weekly downloads. As such, spur-mockserver popularity was classified as not popular.
We found that spur-mockserver demonstrated a not healthy version release cadence and project activity because the last version was released 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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.