0.3.3 • Published 6 years ago

@nolemmings/mockingbird v0.3.3

Weekly downloads
4
License
-
Repository
github
Last release
6 years ago

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.

0.3.3

6 years ago

0.3.2

7 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.8

8 years ago

0.2.7

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

8 years ago

0.2.3

8 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.6

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago