Giter VIP home page Giter VIP logo

mock0's Introduction

Mock0

Mock0 is a zero effort http(s) mocking library with build-in capture to file solution. It can intercept http(s) traffic and capture requests and responses in a JSON formatted file. Once capture is done, it can be used with zero effort to play back responses for the captured requests and use it as mock for unit tests.

Capturing

The example code below starts capturing requests and responses to a file:

require('mock0').capture('mock-data.json', ['x-amz-target'])

Second parameter of the capture method (in this example ['x-amz-target']) is an array of headers that will be saved. Other headers will be dropped.

That's it. The file saved above will look like this example:

[
  {
    "request": {
      "method": "POST",
      "uri": "https://codecommit.eu-central-1.amazonaws.com/",
      "headers": {
        "x-amz-target": "CodeCommit_20150413.ListRepositories"
      },
      "body": "{}"
    },
    "response": {
      "statusCode": 200,
      "headers": [],
      "body": "{\n  \"repositories\": [\n    {\n      \"repositoryId\": \"882407d2-17b3-4b9d-a420-8bb1f8730822\",\n      \"repositoryName\": \"test1\"\n    }\n  ]\n}"
    }
  }
]

This request was made by aws-sdk node module by the following code:

const AWS = require('aws-sdk')
AWS.config.region = 'eu-central-1'
AWS.config.credentials = new AWS.SharedIniFileCredentials({ profile: 'dev' })
const CodeCommit = new AWS.CodeCommit()
CodeCommit.listRepositories({ }, (err, data) => { console.log(err, data) })

Mocking

The example code below starts playing back the requests and responses from the previously captured file:

require('mock0').mock('mock-data.json')

Now all setup. If you run the code below with fake credentials, it will show the exact result as the one which has proper access, because now the mock0 will replay back the captured response.

const AWS = require('aws-sdk')
AWS.config.region = 'eu-central-1'
AWS.config.credentials = { accessKeyId: 'fake-key', secretAccessKey: 'fake-secret' }
const CodeCommit = new AWS.CodeCommit()
CodeCommit.listRepositories({ }, (err, data) => { console.log(err, data) })

Installing

npm install --save mock0

Versioning of this module follows the Schemantic Versioning standard. The current version depend on the following two external modules: axios and mitm. This might change in the future.

About

If you find an issues or you have a feature request, please feel free to contact me directly Ivan Marinov, or create an issue in the issue tracker.

mock0's People

Contributors

dependabot[bot] avatar marinovivan avatar

Stargazers

 avatar

Watchers

 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.