Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@mocks-server/main
Advanced tools
Mocks server with extensible fixtures groupables in predefined behaviors. Behavior can be changed using CLI or REST API
Mocks server with extensible fixtures groupables in predefined behaviors. Behavior can be changed using built-in CLI or REST API.
This package provides a server that simulates API behaviors. As input, it needs "fixtures", which are responses for specific uris, and "features", which are sets of "fixtures".
It also provide a built-in CLI and a REST API which allows to change the currently used "feature" in any moment simply making an http request.
npm i @mocks-server/main --save-dev
Add an script to your package.json
file, including the path to your mocks folder:
"scripts": {
"mocks-server" : "mocks-server --features=./mocks"
}
Now, you can start the mocks server CLI simply typing:
npm run mocks-server
The server includes a REST API that allows to change dinamically the current feature, change delay time, etc.
Available api resources are:
GET
/mocks/features
Returns an array containing all available features.GET
/mocks/features/current
Returns current feature.PUT
/mocks/features/current
Set current feature.
{ "name": "feature-name" }
GET
/mocks/settings
Return current server settings.
{ "delay": 0 }
PUT
/mocks/settings
Change current server settings.
{ "delay": 3000 }
The interactive CLI can be instantiated and started programmatically:
const { Cli } = require("@mocks-server/main");
const startMyCli = () => {
const cli = new Cli({
port: 3200,
log: "debug",
watch: false
});
return cli.start();
};
startMyCli().catch(err => {
console.log("Error starting CLI", err);
});
Cli
([options][,customQuitMethod])For first argument options, please read the options chapter of this documentation. Available methods of an instance are:
start
()
Inits the server in case it was stopped, adds the watch listeners, and renders main menu.initServer
()
Inits the server in case it was stopped, adds the watch listeners.stopListeningServerWatch
()
When server watch is active, the main menu will be displayed on file changes. This behavior can be deactivated using this method. This is useful when this CLI is loaded as a submenu of another CLI, for example.The server can be instantiated and started programmatically:
const { Server } = require("@mocks-server/main");
const startMyServer = () => {
const server = new Server(path.resolve(__dirname, "mocks"), {
port: 3200,
log: "debug",
watch: false
});
return server.start();
};
startMyServer().then(server => {
console.log("Server started", server);
});
Server
(featuresFolder [,options])First argument is mandatory, and has to be a path to a folder containing "features". All files in the folder will be loaded recursively, including subfolders. For second argument options, please read the options chapter of this documentation.
Available methods of an instance are:
start
(). Starts the server.stop
(). Stops the server.restart
(). Stops the server, initializes it again (reloading features files), and starts it again.switchWatch
(state <Boolean>
). Enable or disable features files watch, depending of the received "state" value.Available getters are:
features
. Returns loaded features object.watchEnabled
. Current state of the features files watcher.error
. When server has returned an error, or an error ocurred loading features, it is available in this property.events
. Returns server events object. A "watch-reload" event is emitted when the server watch detects changes in any features file, and restarts the server.The mocks server can be used as a global dependency as well:
npm i @mocks-server/main -g
Now, you can start the built-in command line interface from anywhere, providing a path to a features folder:
mocks-server --features=./path-to-features
<Number>
Por number for the Server to be listening.<String>
Host for the server. Default is "0.0.0.0" (Listen to any local host).<String>
Logs level. Can be one of "silly", "debug", "verbose", "info", "warn", "error".<Boolean>
Watch features folder, and restart server on changes. Default is true
.<String>
Selected feature when server is started.<Number
Responses delay time in milliseconds.Path as <String>
Path to a folder containing features to be used by the server.<Boolean>
Load features recursively. Watch is not affected by this option, it is always recursive.<Boolean>
Start interactive CLI. Default is true
.The Mocks server handles two main concepts for defining mocks:
Each feature consists in a set of "fixtures", which are server responses for specific uris.
Features are extensibles, so, you can have a "base" feature, which defines the standard behavior of the mocks server and responses for all api uris, and change this behavior creating new features that changes only responses for certain "uris". All features are extensible as well.
For creating a Feature, you have to use the mocks-server "Feature" class, providing an array of "fixtures" to it:
// Features file 1
const { Feature } = require("@mocks-server/main");
const fixtures = require("./fixtures");
const myFeature = new Feature([fixtures.uri_1_fixture, fixtures.uri_2_fixture]);
module.exports = {
myFeature
};
Now, when loaded, the server will have available a "myFeature" feature, which contains two fixtures. You can add more features extending the first one and changing only the response for "uri_2", for example:
// Features file 2
const { myFeature } = require("./features");
const fixtures = require("./fixtures");
const myFeature2 = myFeature.extend([fixtures.uri_2_different_fixture]);
module.exports = {
myFeature2
};
Now, server will have available "myFeature" and "myFeature2" features. And "myFeature2" will send a different response only for "uri_2" (supossing that "uri_2_fixture" and "uri_2_different_fixture" were defined with the same uri)
A "fixture" defines the response for an specific uri. It has to be an object containing properties:
uri as <String>
Uri of the resource. It can contains expressions for matching dynamic uris. Read the route-parser documentation for further info about how to use dynamic routing.<String>
Method of the request. Defines to which method will response this fixture. Valid values are http request methods, such as "GET", "POST", "PUT", etc.<Object>
Defines the response that the Mocks Server will send to the request:
<Number>
Status code to send.<Object>
Json object to send as body in the response.<Function>
Response can be defined as a function too. The function will receive the express request
, response
and next
arguments, so you are free to handle the server request as you need.// Fixtures file
const uri_1_fixture = {
url: "/api/foo-uri",
method: "GET",
response: {
status: 200,
body: [
{
name: "foo-name"
}
]
}
};
const uri_2_fixture = {
url: "/api/foo-uri-2/:id",
method: "PUT",
response: {
status: 204
}
};
const uri_2_different_fixture = {
url: "/api/foo-uri-2/:id",
method: "PUT",
response: (req, res) => {
res.status(404);
res.send({
message: `${req.params.id} was not found`
});
}
};
module.exports = {
uri_1_fixture,
uri_2_fixture,
uri_2_different_fixture
};
Contributors are welcome. Please read the contributing guidelines and code of conduct.
FAQs
Mock Server supporting multiple route variants and mocks
The npm package @mocks-server/main receives a total of 27,726 weekly downloads. As such, @mocks-server/main popularity was classified as popular.
We found that @mocks-server/main demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.