
Security News
Feross on Risky Business Weekly Podcast: npm’s Ongoing Supply Chain Attacks
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
mock-n-roll
Advanced tools
Configurable server for proxing and mocking REST.
mock-n-roll
.Below a short recorded demo, it should you give first impression about tool.
Let's imagine that you have REST service which implements following call:
GET https://jsonplaceholder.typicode.com/posts/1 200
Response:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
And you would like to change response body and status code for it as well.
Something like this:
GET https://jsonplaceholder.typicode.com/posts/1 404
Response:
{
"msg": "Requested post wasn't found!"
}
As mentioned above you can use nock but in some cases you need a proxy that additionaly can change responses.
Mock-n-roll works as usual proxy that redirects all not mocked requests to target, and otherwise if mock found it will be used to return appropriate response to caller.
First of all you need to define configuration file for your proxy server.
// mock-n-roll.conf.js - default name for config file
module.exports = {
httpPort: 8080,
httpsPort: 8443,
prefix: '',
target: 'https://jsonplaceholder.typicode.com',
options: {
key: fs.readFileSync(`${__dirname}/ssl.key`, 'utf-8'),
cert: fs.readFileSync(`${__dirname}/ssl.cert`, 'utf-8')
}
};
/api/v1
).By default config file will be searched in current working directory from which node process was started.
Easily you can start server using existed runner:
./bin/mock-n-roll --config ./my-custom-config.js --debug
A little bit about runner options:
mock-n-roll.conf.js
will be used.After you should see something like this:
$ ./bin/mock-n-roll -c ./example/mock-n-roll.conf.js
info: Mock-n-Roll started...
info: HTTP : 8080
info: HTTPS : 8443
info: Target: https://jsonplaceholder.typicode.com
info: Prefix: [none]
Now you mock-n-roll
server is started and you can send requests to 'localhost' and it will redirect it to 'jsonplaceholder.typicode.com'.
Use /mocks
route to configure your mocks.
POST - add new mock, or override existing.
POST http://localhost:8080/mocks 201
Payload:
{
"path": "/posts/1",
"code": 404,
"body": {
"msg": "Requested post wasn't found!"
}
}
GET - returns all mocked routes.
GET http://localhost:8080/mocks 200
Response:
{
"/posts/1": {
"body": {
"msg": "Requested post was not found!"
},
"code": 404
}
};
DELETE - delete all existing mocked routes.
DELETE http://localhost:8080/mocks 204
For more convinent usage you can use existing REST client to access mock-n-roll server.
const MocknRollClient = require('mock-n-roll').client;
const client = new MocknRollClient({ host: 'localhost', port: 8080 });
const mock = {
path: '/posts/1',
code: 404,
body: {
msg: 'Requested post was not found!'
}
};
client.add(mock, (error, response, body) => {
// do something in callback
});
client.get((error, response, body) => {
// do something in callback
});
client.delete((error, response, body) => {
// do something in callback
});
Also you could look at the tests, it should give you additional information how you could run and use mock-n-roll.
Copyright 2016
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
FAQs
Configurable server for proxing and mocking REST
The npm package mock-n-roll receives a total of 0 weekly downloads. As such, mock-n-roll popularity was classified as not popular.
We found that mock-n-roll 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
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.