0.1.2 • Published 5 years ago
@devaway/rest-mock-middleware v0.1.2
rest-mock-middleware
Node.js mock middleware for REST services. Configure REST mocks ease for express.
TL;DR
Captures /api requests and when /api/users/me is called with right authorization it returns a mock user.
server.js
const express = require("express");
const { resolve } = require("path")
const restMockMiddleware = require('@devaway/rest-mock-middleware');
let app = express();
app.use('/api', restMockMiddleware({
  root_dir: resolve(__dirname, "./mocks")
}));
app.listen(3000);mocks/mappings/GET-user.json
{
  "request": {
    "url": "/api/users/me",
    "method": "GET",
    "headers": {
      "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IlBvbCBKYWNrc29uIiwiaWF0IjoxNTE2MjM5MDIyfQ.EIkH2om-Yqw2pfQVofP-N9xfP0aAQNCS-vnS3dwLBdM"
    }
  },
  "response": {
    "status": 200,
    "bodyFileName": "GET-user.json"
  },
  "delay" : 2000
}mocks/responses/GET-user.json
{
    "firstName": "Pol",
    "lastName": "Jackson",
    "age": 24,
    "address": {
        "streetAddress": "126",
        "city": "San Jone",
        "state": "LA",
        "postalCode": "90009"
    },
    "phoneNumbers": [
        { "type": "home", "number": "7463694628" }
    ]
}curl --location --request GET 'http://localhost:3000/api/users/me' \
--header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IlBvbCBKYWNrc29uIiwiaWF0IjoxNTE2MjM5MDIyfQ.EIkH2om-Yqw2pfQVofP-N9xfP0aAQNCS-vnS3dwLBdM'Install
$ npm install --save-dev @devaway/rest-mock-middlewareDocumentation
- Getting started
 - Configuration
 - Use with cors
 - Use with proxy
 - Request matching
 - Stubbing
 - Response time management
 
Test
Run the test suite:
# install dependencies
$ npm install
# unit tests
$ npm test
# code coverage
$ npm run coverChangelog
License
The MIT License (MIT)
Copyright (c) 2020-2021 Devaway