@nolemmings/mockingbird

0.3.3 • Public • Published

mockingbird

A stand-alone express server which allows dynamically registering a mock request and consume them when called. This server allows you to build a fake backend without much overhead, complex mocking frameworks for your framework/language/whatever. Just start the server, make a request describing your expectation and run your actual test.

Installation

yarn add -D @nolemmings/mockingbird

or

npm install @nolemmings/mockingbird

Run

Start the server like a normal express server:

var mockingbird = require('@nolemmings/mockingbird');

var server = mockingbird.listen(3000, function() {
  var port = server.address().port;
  console.log('App listening at http://localhost:%s', port);
});

Create an Expectation

To register which requests you want to trigger and what response you'd like to have returned you must create an expectation.

Request:

POST /tests/eae37fb0/expectations

{
  "request": {
    "method": "post",
    "url": "/users/123",
    "body": {
      "some": "object"
    }
  },
  "response": {
    "status": 200,
    "body": {
      "id": "123",
      "name": "Johnny",
      "email": "my@email.org"
    }
  },
  "repeat": 3
}

Set repeat to -1 to repeat indefinitely.

Body is an optional param which when omitted ensures any request body is accepted.

Response:

{
   "id": "7df3567b-3b84-4496-8df5-57506c51eabb",
   "testId": "eae37fb0",
   "request": {
      "method": "post",
      "url": "/users/123",
      "body": {
        "some": "object"
      }
   },
   "response": {
      "status": 200,
      "body": {
         "id": "123",
         "name": "Johnny",
         "email": "my@email.org"
      }
   },
   "repeat": 3,
   "requestCount": 0
}

When a request has been triggered the number of times defined in repeat it will start returning a 404 with an error message.

Retrieve an Expectation

To get the latests details about an expectation you can retrieve it as follows:

Request:

GET /tests/eae37fb0/expectations/7df3567b-3b84-4496-8df5-57506c51eabb

Response:

{
   "id": "7df3567b-3b84-4496-8df5-57506c51eabb",
   "testId": "eae37fb0",
   "request": {
      "method": "get",
      "url": "/users/123"
   },
   "response": {
      "status": 200,
      "body": {
         "id": "123",
         "name": "Johnny",
         "email": "my@email.org"
      },
      "headers": {
        "X-RateLimit-Limit": "5000",
        "X-RateLimit-Remaining": "4999"
      }
   },
   "repeat": 3,
   "requestCount": 0
}

Get all test expectations

Return all expectations.

GET /tests/eae37fb0

Remove a Test

To delete all expectations for a specific test case simply run the following:

DELETE /tests/eae37fb0

Running a mock request

When you've registered an expectation you can trigger it by simply calling the specified url and method.

For example:

curl -v -H "Content-Type: application/json" -X GET localhost:3000/users/123

Response:

{
  "id": "123",
  "name": "Johnny",
  "email": "my@email.org"
}

Returns 429 Too Many Requests when the request count is more than the specified number of repetitions. Returns 404 Not Found when expectation could not be found.

Readme

Keywords

none

Package Sidebar

Install

npm i @nolemmings/mockingbird

Weekly Downloads

4

Version

0.3.3

License

none

Unpacked Size

209 kB

Total Files

36

Last publish

Collaborators

  • nielskrijger
  • nolemmings