![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
HTTP mocking service
Rodo can be used to create a real mocked API with an specific port and host.
The main difference with other HTTP mocking libraries like nock
is that Rodo creates a real HTTP server instead of overriding the behavior of Node HTTP objects.
npm install rodo
Writing a new mocked endpoint can be as easy as:
// Mocking API
const assert = require('assert');
const rodo = require('rodo');
const mockServer = rodo(8000);
const myCall = mockServer
.get('/foo')
.reply({ bar: 'baz' })
.withHeader('content-type', 'application/json');
// Action
const res = await fetch('http://localhost:8000/foo');
const json = await res.json();
// Assertions
assert.equal(json.bar, 'baz');
assert.equal(myCall.calls.length, 1);
To get started, you first need to instantiate the Rodo server doing the following:
const rodo = require('rodo');
const mockServer = rodo(8000);
Lets start building the filtering process for a specific request:
var myRequest = mockServer.request().havingMethod('GET').havingPath('/foo');
You can do the same by doing:
var myRequest = mockServer.get('/foo');
Nice! Now Rodo will intercept every request to GET http://localhost:8000/foo
.
Now you want Rodo to return a specific response to that request:
var myResponse = myRequest
.reply()
.withHeader('content-type', 'application/json')
.withStatus(200)
.withBody({ bar: 'baz' });
You can do the same with:
var myResponse = myRequest
.reply({ bar: 'baz ' })
.withHeader('content-type', 'application/json');
Good! you can now check for the requests that were resolved with that response:
myResponse.calls; // [...calls]
You are all set, now Rodo will start intercepting all that requests and will return the response that you specify.
mockServer.clean();
mockServer.waitForPending();
mockServer.close();
Aside from port and hostname, when instatiating the Rodo server you can optionally pass an object with any of these extra options:
Multipart will be handled using the multiparty library. An example for rodo handling multipart is:
mock
.post('/foo')
.havingFields({ bar: 'baz' })
.havingFiles({
quux: (file) => file.originalFilename === 'my-file.txt'
})
.reply('quux');
Instead of using .withDelay
explicitly on each of your response methods, you can use this option to set a default delay that
will be automatically applied to all of them. And if you have a case that needs a different value, you can still use .withDelay
to overwrite the default.
rodo(port?, hostname?, options?)
port
: The port number of the server to be used by Rodohostname
: Hostname of the server, if neededoptions
: Options for the server, options is an object with props, i.e.: { removeAfterUse: true }
removeAfterUse
: Remove call mocks after they are calledcors
: Enables CORS for every requestThe filtering process for a specific request:
.havingMethod(method)
Specifies the HTTP method, should be one of GET, POST, PUT, DELETE, PATCH.
.havingBody(body)
Specifies the body, can be an object or a string.
.havingFields(fields)
Specifies the fields, should be a key-value object.
.havingFiles(files)
Specifies the files, should be a key-value object, where value should be a function that validates every file configured.
.havingPath(path)
Will intercept a specific path.
.havingQuery(query)
Will filter by query object params.
.havingHeader(name, value)
Will filter by header, only requests with the specified header and value will be intercepted.
The response for a specific request:
.withHeader(name, value)
Will return specific header with the response.
.withBody(body)
Will return the specified body with the response.
.withFile(filePath)
Will return the content of the given file.
.withStatus(status)
Will change the status code to the one specified.
.withDelay(ms)
Will delay the response.
.use((req, res) => { })
Will add a middleware to the server.
Example:
mockServer.use(morgan('dev'));
MIT
FAQs
Http mocking service
The npm package rodo receives a total of 23 weekly downloads. As such, rodo popularity was classified as not popular.
We found that rodo 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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.