0.1.1 • Published 9 years ago

connect-api-mocker-adv v0.1.1

Weekly downloads
3
License
ISC
Repository
github
Last release
9 years ago

Advanced API mocker

This is a connect.js middleware to mock http request with manual body, headers and status code. Used ot mock REST API.

Usage

Add this this middleware to your config. Example of configuring gulp-connect with API root '/api' and mock location in directory `mocks'

gulpConnect.connect.server({
middleware: function (connect, opt) {
  var mocker = require('connect-api-mocker-adv'),
    options = {
      urlRoot: '/api',
      pathRoot: 'mocks'
    };

  return [mocker(options)];
}
});

Firstly mocks will be served, than other middleware.

Structure

Mock file will be searched from pathRoot according to request url and request method. For example if pathRoot is 'mocks', method is 'GET' and url is '/api/collection mock files will be searched under './mocks/api/collection/GET.yaml'. Filename int the upper case.

Mock file structure

Mock file is a yaml config file with next keys:

  • status (number) - Response status code, default is 200
  • headers (object) - Headers which will be appended to default headers
  • disabled (boolean) - Is this mock disabled
  • body (object|string) - Response body. Can be an object, that will be serialized to json or simple text (use | or > for multiline text)

Example

status: 200
disabled: false
headers:
  X-Header: header-value
body:
  prop: val

Or only text body

body: |
  This is text body line breaks.
  Use > to disable line breaks

Or with json

body: |
  {
    "key": "value",
    "subKey": {
      "subObjectKey":"subObjectValue"
    }
  }

API

mocker(options)

options.urlRoot

Required

Type: String

URL root for api. If url equals to * then it will try to mock all requests

options.pathRoot

Required

Type: String

Root location of mock files

options.headers

Type: Object

Default: {'Content-Type': 'application/json; charset=utf-8'}

Response headers for all requests

options.forced

Type: Boolean

Default: false

If true all disabled mocks will be served

options.mockAll

Type: Boolean

Default: false

All request will be mocked. If mock not found or disabled response with status 500 will be returned

options.urlMangler

Type: Function

Modify request url. Arguments:

  • url - request url without query string
  • request - request object

options.speedLimit

Type: Number

Default: 0 (unlimited)

Limit speed of response in KB