npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rodo

v2.17.1

Published

Http mocking service

Downloads

99

Readme

Rodo

Rodo

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.

Install

npm install rodo

Sample usage

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);

Getting started

The request

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.

The response

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.

If you now want to clear the results and start again

mockServer.clean();

If you now want to wait for all endpoints to be executed

mockServer.waitForPending();

Want to kill the server?

mockServer.close();

Extra server options

Aside from port and hostname, when instatiating the Rodo server you can optionally pass an object with any of these extra options:

Using multipart

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');

defaultResponseDelay

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.

API

Constructor

rodo(port?, hostname?, options?)

  • port: The port number of the server to be used by Rodo
  • hostname: Hostname of the server, if needed
  • options: Options for the server, options is an object with props, i.e.: { removeAfterUse: true }
    • removeAfterUse: Remove call mocks after they are called
    • cors: Enables CORS for every request

Request methods

The 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.

Response methods

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.

Other utilities

.use((req, res) => { })

Will add a middleware to the server.

Example:

mockServer.use(morgan('dev'));

License

MIT