mockjax-router
mock out the server-side in your browser tests
Last updated 5 years ago by refractalize .
MIT · Original npm · Tarball · package.json
$ cnpm install mockjax-router 
SYNC missed versions from official npm registry.

mockjax-router

npm install mockjax-router

Build a mock API to use in your browser tests:

var mockjaxRouter = require('mockjax-router');

var router = mockjaxRouter();

var users = {};
var lastUserId = -1;

router.get('/users/:userId', function(request) {
  var user = users[request.params.userId];

  return {
    body: user
  };
});

router.post('/users', function(request) {
  var user = request.body;
  user.id = lastUserId++;

  users[user.id] = user;

  return {
    statusCode: 201,
    headers: {
      location: '/users/' + user.id
    },
    body: user
  };
});

Use it with jquery:

var $ = require('jquery');

$.get('/users/1').then(function (user) {
  console.log(user);
});

api

var mockjaxRouter = require('mockjax-router');
var router = mockjaxRouter();

router.get(path, handler);
router.delete(path, handler);
router.head(path, handler);
router.post(path, handler);
router.put(path, handler);
router.head(path, handler);
  • path - a path for the resource, usually a root relative path like /api/users, but could also be an absolute URL. Can contain parameters, in the form of :paramName, e.g. /users/:userId.

handlers

function handler(request) {
  return response;
}
  • request - an object containing:
    • method - the method, one of GET, POST, etc.
    • url - the url of the resource
    • params - an object containing the parameters taken from the path
    • headers - an object with the request headers
    • body - body. if this was transmitted as JSON then this is parsed into a JS value.
  • response - an object containing the following fields.
    • statusCode - the status code, if omitted then 200.
    • headers - an object with the response headers. Optional.
    • body - the body, if a JS object, then it is transmitted as JSON. Optional.

The response can be omitted too, giving a 200 OK.

Current Tags

  • 1.1.1                                ...           latest (5 years ago)

5 Versions

  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (9)
Dependents (2)

Copyright 2014 - 2016 © taobao.org |