项目作者: zeachco

项目描述 :
NodeJS Stub server for test and dev purposes
高级语言: JavaScript
项目地址: git://github.com/zeachco/stubborn-server.git
创建时间: 2016-05-26T18:14:01Z
项目社区:https://github.com/zeachco/stubborn-server

开源协议:GNU General Public License v3.0

下载


stubborn-server

Greenkeeper badge
Build Status
License

NodeJS Stub server for test and dev purposes

Is your backend stubborn?

Features

  • Mocking with static json
  • Mocking with dynamic request handlers based on Express requests
  • Storing/accessing a memory database
  • Separate database per namespace
  • Reset a db at anytime with default values
  • Usage of aliases to get alternate mock responses (allow scenarios flexibility)
  • Integrates well with any test framework

Just show me code!

Ok… create a stubborn.js file where you want to use it content might look like

  1. module.exports = {
  2. logMode: 'all', // can be 'all', 'info', 'mock', 'warn', 'error' or 'none'
  3. namespace: '', // help switching between different senarios
  4. pathToMocks: 'mock-examples', // mock folder relative path
  5. servePort: 8059,
  6. includes: ['__custom/express_handlers'], // List of files into the mock folder where the express app will be exposed for custom handling
  7. fallbacks: [
  8. { // using "path" key tells that we refer to a local file
  9. url: '/assets/*',
  10. path: '/path/to/static/folder'
  11. },
  12. { // using "target" key tells that we refer to an http location and is treated as a server proxy
  13. url: '/*':,
  14. target: 'localhost:3000'
  15. },
  16. { // using "mock" key tells that we refer to an existing mock, a direct regex can be used here
  17. url: /api\/path\/([^\/]+)/,
  18. mock: 'api/path/__wildcard__/more/paths'
  19. }
  20. ]
  21. };

or a stubborn.json file

  1. {
  2. "logMode": "mock",
  3. "namespace": "",
  4. "pathToMocks": "mock-examples",
  5. "servePort": 8059,
  6. "includes": ["__custom/express_handlers"],
  7. "fallbacks": [
  8. {
  9. "url": "/assets/*",
  10. "path": "/path/to/static/folder"
  11. },
  12. {
  13. "url": "/*",
  14. "target": "localhost:3000"
  15. },
  16. {
  17. "url": "api\/path\/([^\/]+)",
  18. "target": "api/path/__wildcard__/more/paths"
  19. }
  20. ]
  21. }

Then write your initiator like the one given in demo.js or in tests/...

Might look like this :

  1. const stubbornServer = require('stubborn-server');
  2. const stub = stubbornServer();
  3. stub.start(/* config to extend ./stubborn.js if required */);
  4. // from this point, you may run your queries
  5. stub.config.set({
  6. namespace: 'alt'
  7. });
  8. // from this point, you may run other queries
  9. stub.stop();

I also have the intention of having this available directly from the command line.

Write your mocks

Mocks can be specified in three ways:

  1. Attaching express handlers
  2. Including express handlers
  3. Configuration-driven

The way you choose depends on the needs of your project. You may also
mix and match as the options are not exclusive.

Attaching express handlers

When creating your mock server, a reference to the underlying raw express app
is available. You can attach any response handlers you want:

  1. const stubbornServer = require('stubborn-server');
  2. const stub = stubbornServer();
  3. // Attaching express handlers directly via `app` raw reference.
  4. stub.app.use('/my/url/path', (req, res) => {
  5. // req / res are from express
  6. console.log(req.method);
  7. const response = {'hello': 'world'};
  8. // stub is stubborn server object
  9. console.log(stub.config.get());
  10. res.json(response);
  11. });
  12. stub.start();
  13. // from this point, you may run your queries
  14. stub.stop();

Including express handlers

Instead of manually attaching handlers to the app, you may specify paths to
express handlers. The handlers will then be require()ed for you.

This example is functionally equivalent to the example above:

main.js:

  1. const stubbornServer = require('stubborn-server');
  2. const stub = stubbornServer();
  3. stub.config.set({
  4. includes: ['path/to/handler.js'],
  5. });
  6. stub.start();
  7. // from this point, you may run your queries
  8. stub.stop();

handler.js:

  1. module.exports = (app, stub) => {
  2. app.use('/my/url/path', (req, res) => {
  3. // req / res are from express
  4. console.log(req.method);
  5. const response = {'hello': 'world'};
  6. // stub is stubborn server instance
  7. console.log(stub.config.get());
  8. res.json(response);
  9. });
  10. };

Configuration-driven

By default, when the server recieves a request it will look into the directory
specified by the pathToMocks configuration option for a mock response.

The file name serving as a mock must match the request path plus the request
method.

For example, a POST request to http://localhost:3000/api/service will try
to find files at the following locations:

  • /path/to/mocks/api/service/post.js
  • /path/to/mocks/api/service/post.json
  • /path/to/mocks/api/service/post/index.js
  • /path/to/mocks/api/service/post/index.json

Basically, it’s a require.resolve that finds the proper mock response files.

The following is an example of a basic configuration driven post.js mock:

  1. // this function is the handler being called
  2. module.exports = (req, res, stub) => {
  3. // req / res are from express
  4. console.log(req.method); // POST
  5. // stub is stubborn server object
  6. console.log(stub.config.get());
  7. res.json({ 'hello': 'world' });
  8. };

and a post.json mock that returns the same mock response data:

  1. {
  2. "hello": "world"
  3. }

Plugins

A plugin architecture allows you to extend even more the custom functionality of your server.

Custom loaders

Add your own custom loader that takes precedence over the default behavior of file system lookup and the fallbacks property - this way even if your loader throws you can still rely on the standard functionality.

Here is an example of a custom loader that uses sent data in order to load a mock:

  1. const stubbornServer = require('stubborn-server');
  2. const stub = stubbornServer();
  3. stub.start({
  4. plugins: [
  5. {
  6. loader: (req, { pathToMocks }) => {
  7. return require(path.join(pathToMocks, req.body.data);
  8. }
  9. }
  10. ]
  11. });

NOTE: you may also define multiple loaders, allowing you to have multiple custom fallbacks systems.

License

GPL-3.0 © 2018 Olivier Rousseau-Guyot