jmock

command module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2021 License: MIT Imports: 20 Imported by: 0

README

Jmock

Tests Status Docker Image

Simple and easy to use json/post API mock server

Install

Install binary

go install github.com/fullpipe/jmock@latest

Usage

First create mocks collection file some where in your project. Use standard wildcards for request matching. For example ./mocks/users.json:

[
  {
    "name": "Allow CORS",
    "request": {
      "method": "OPTIONS",
      "priority": 100
    },
    "response": {
      "code": 204,
      "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "*"
      }
    }
  },
  {
    "request": {
      "method": "POST",
      "headers": {
        "Authorization": "Bearer *"
      },
      "url": "/api/users",
      "json": {
        "name": "*"
      }
    },
    "response": {
      "code": 200
    }
  },
  {
    "request": {
      "method": "GET",
      "url": "/api/users/*"
    },
    "response": {
      "code": 200,
      "json": {
        "name": "John Doe"
      }
    }
  },
  {
    "request": {
      "method": "GET",
      "url": "/api/users/7",
      "priority": 42
    },
    "response": {
      "code": 200,
      "file": "data/user-7.json"
    }
  },
  {
    "name": "Get posts list",
    "request": {
      "method": "GET",
      "url": "/api/posts"
    },
    "proxy": "http://realapi.loc"
  }
]

Start jmock server

jmock "./mocks/*.json" --port 9090 --watch

Thats it your fake api is ready. Check the request

curl localhost:9090/api/users/1

Output

{
  "name": "John Doe"
}
Usage with docker

Run mock server

docker run -p 9090:9090 -v ${PWD}/mocks:/mocks fullpipe/jmock

Or if you need to watch files

docker run -p 9090:9090 -v ${PWD}/mocks:/mocks fullpipe/jmock /mocks/*.json --port 9090 --watch

Or with docker-compose

services:
    api:
        image: fullpipe/jmock
        command: "/mocks/*.json --port 9090 --watch"
        ports:
            - "9090:9090"
        volumes:
            - ./mocks:/mocks

Mocks

Mock consists of 3 blocks request, response, proxy.
Also you could use name to name it.

Request

You could match request by:

    "request": {
      "method": "POST", // http method
      "url": "/api/users/*", // query path
      "headers": {
        "Authorization": "Bearer *"
      },
      "query" { // get params
          "country": "R*"
      },
      "post": { // post variables
          "first_name": "Jo*",
          "last_name": "?oe"
      },
      "json": { // JSON request body
        "name": "*",
        "gender": "?"
      },
      "priority": 42 // high number for more "sticky" requests
    }
Response

For matched request server returns response:

    "response": {
      "code": 200, // status code

      "body": "plain text or html", // response body
      "json": { // OR response body with json
        "name": "John Doe"
      },
      "file": "data/user-7.json", // OR path to a file with data for response body

      "headers": { // add response headers if required
        "Access-Control-Allow-Origin": "*"
      }
    }
Proxy (optional)

If you get one mock working. You could use proxy to bypass matched request to real API.

    "proxy": "http://realapihost.loc"

Examples

JSON RPC 2.0
[
  {
    "name": "User registration mock",
    "request": {
      "mehtod": "POST",
      "url": "/rpc",
      "json": {
        "jsonrpc": "2.0",
        "method": "registerUser",
        "id": 1,
        "params": {
          "email": "*"
        }
      }
    },
    "response": {
      "code": 200,
      "json": {
        "jsonrpc": "2.0",
        "result": {
          "user_id": "15"
        },
        "id": 1
      }
    }
  }
]

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL