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

stateful-api-mock-server

v1.0.7

Published

Stateful API mock server to manage responses of any external API from files

Downloads

5

Readme

Stateful API mock server

Build Status Build Status codecov

Yet another API mock server. Why? Because it has to be simple and I didn't find a simple one in my case (independent api) based on directory file. It's mostly useful if your api use another api.

Just put a js/json file into a directory, start your server from your mocha/karma/etc and use the simple API to create your test cases.

Features

  • Independent mock server
  • Totally stateful to facilitate unit tests
  • Easy to use state API base on route paths

How to use

In your tests file

var ApiMockServer = require('stateful-api-mock-server');

var api = new ApiMockServer([options<object>]);

api.start(function(){
  // write your tests case here
});

or with mocha

var ApiMockServer = require('stateful-api-mock-server');

var api = new ApiMockServer([options<object>]);

describe('[API]', function() {
  before(function(done) {
    return api.start(function() {
      done();
    });
  });

  it('should do something', function() {
    //...
  });
});

Endpoints to File directory

tests/api-mocks/
  L users/
     L get.json     # 200 GET /users response
     L get-404.json # 404 GET /users response
     L :id/
        L get.json  # 200 GET /users/:id response
        L post.json # 200 POST /users/:id response
  L cars/
     L get.json     # 200 GET /cars response
     L get-404.json # 404 GET /cars response

Use default case

It's possible to define common response in a _defaults directory to avoid file duplication, like 404.

tests/api-mocks/
  L _defaults
     L get-404.json # 404 GET default response
  L users/
     L get.json     # 200 GET /users response
     L :id/
        L get.json  # 200 GET /users/:id response
        L post.json # 200 POST /users/:id response
  L cars/
     L get.json     # 200 GET /cars response

Change response state

Imagine this test case suite:

describe('api', function() {
  it('should respond with a 200', function(done) {
    request(app)
      .get('/users')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(200, done);
  });
})

Now you want to test the 404 case

describe('api', function() {
  it('should respond with a 404', function(done) {
    api.state.set('/users', 'GET', 404);

    request(app)
      .get('/users')
      .set('Accept', 'application/json')
      .expect('Content-Type', /json/)
      .expect(404, done);
  });
})

/!\ Important! If you set a state to unknown construct file (like GET 403 and ./get-403 and /_defaults/get-403 does not exist) it will return 503 and log an error in your terminal

Finally you can reset all

describe('api', function() {
  ...

  afterEach(function() {
    api.state.resetAll();
  });
})

API

Constructor API

Default values:

var api = new ApiMockServer({
  port: 7000, // Port to launch mocked api
  mockDir: 'tests/api-mocks', // Relative path to your mocks
});

State API

Set .set(Path<String>, Verb<String>, State<Number|String>)

Set a route to demanded state and set status response if State is a number.

// Return get-404.[ext] file and set
api.state.set('/users/:id', 'GET', 404);

// Return get-another.[ext] file and set status to 200
api.state.set('/users/:id', 'GET', 'another');

Get .get(Path<String>, Verb<String>)

Get a route state

api.state.get('/users/:id', 'GET');

Get All .getAll()

Get all route states

api.state.getAll();

Reset .reset(Path<String>, Verb<String>)

Reset a state to 200

api.state.reset('/users/:id', 'GET');

Reset All .resetAll()

Reset all routes to 200

api.state.resetAll();

Debug

To debug you can filter on this domain: stateful-api-mock-server

For example

DEBUG=stateful-api-mock-server:* npm test

TODO

  • [ ] Remove ext option