Giter VIP home page Giter VIP logo

mock-rest-server's Introduction

Mock REST Server

Mock REST Server is a lightweight (in memory) REST server for unit test.

Known Vulnerabilities JavaScript Standard Style NPM version License


Mock REST Server

No configuration needed: Just start it, and test your code!

Mock REST Server comes with these features ๐Ÿš€ :

  • Support GET, POST, PUT, PATCH, DELETE and OPTIONS HTTP methods
  • Accept and respond with JSON
  • Support real HTTP status code response from REST API
  • Return HTTP error status codes via api version
  • Fake latency
  • Filtering, sorting and pagination
  • Auto populate tool to fill database, don't waste your time for a REST server!

Changes

All details of changes to this project will be documented in this file.

Installation & Usage

npm i -g https://github.com/jorgemxm/mock-rest-server
node_modules/.bin/mock-server
  or
node_modules/.bin/mserver
Param name Description Type Default
-p3000 --port=3000 (Optional) Change server port Number 3000
-s --silent (Optional) Hide server output Boolean false
-l500 --latency (Optional) wait (ms) before response Number 0

Unit test

For more details, look at the full example from test file.

Assuming you're using a module-compatible system (like webpack), start MockRestServer on top of your unit test file:

import MockRestServer from 'mock-rest-server'

describe('MockRestServer', function () {
  it('Start server', async () => {
    const server = await MockRestServer.start(3000, true, 0)
    server.populate('articles', 30, {
      title: String,
      body: String,
      userId: Number,
      created: Date,
      private: Boolean
    })

    // ...
    // ...
    // ...

    // don't forget to stop!
    // If you work with `--watch` param that reload your unit test, don't forget to stop server at the end of your tests.
    // It'll try to launch server again when refresh, and port will already in use.
    await server.stop()
  })
})

MockRestServer come with (optional) awesome feature that fill database with random typed data: populate(collection, length, schema)

Param name Description Type
collection Collection name to create/populate String
length Number of resource to create in collection Number
schema Object send to create a resource. Values are replaced by random value based on JS type String, Number, Date or Boolean. Object

Example

Start MockRestServer, open a new shell and run some curl on api /v1/:

curl -X POST -d '{"title":"Awesome movie!","body":"Some content."}' http://localhost:3000/v1/movies
# {"title":"Awesome news!","body":"Some content.","id":1}

Now, get your movies with:

curl http://localhost:3000/v1/movies
# [{"title":"Awesome news!","body":"Some content.","id":1},{"title":"Awesome news!","body":"Some content.","id":2}]
curl http://localhost:3000/v1/movies/1
# {"title":"Awesome news!","body":"Some content.","id":1}

Use api /v[xxx]/ to mock HTTP status codes (403, 404, 500...) from server response:

curl http://localhost:3000/v401/books
curl http://localhost:3000/v200/superheroes

Contribution

Any help or feedback are really welcome, no matter how great or small!

Please make sure to read the Contributing Guide before making a pull request.

License

GPL-3.0

mock-rest-server's People

Contributors

guillew avatar

Watchers

Jorge M avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.