Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/smeshkov/go-mock-server

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/smeshkov/go-mock-server

  • v0.10.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Go Mock

Light weight mock server written in Go.

Supported features:

  • good old mocked JSON responses for HTTP methods and URI paths;
  • custom error behaviours;
  • proxying;
  • dynamic results, as in store data from an incoming JSON and then retrieve it.

Installation

For MacOS:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/smeshkov/gomock/master/_bin/install.sh)"

For Linux:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/smeshkov/gomock/master/_bin/install.sh)" linux

For Windows:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/smeshkov/gomock/master/_bin/install.sh)" windows

Usage

Create JSON file with your mocks and start mock server by running gomock -mock /path/to/your/mock.json.

mock.json defines mocks - handlers which will serve provided mocks.

Use gomock --help for more information.

mock.json example:

{
  "port": 8080,
  "endpoints": [
    {
      "methods": [ "GET" ],
      "status": 200,
      "path": "/users",
      "jsonPath": "./users.json"
    },
    {
      "path": "/user/1",
      "json": {
        "id": 1,
        "name": "name",
        "address": "address"
      },
      "allowCors": [ "example.com" ]
    },
    {
      "methods": ["POST"],
      "path": "/error",
      "delay": 100,
      "status": 400,
      "json": {
        "message": "failed"
      }
    },
    {
      "methods": [ "POST" ],
      "path": "/api/*",
      "delay": 200,
      "url": "http://localhost:8090"
    }
  ]
}

Mock JSON configuration properties:

  • port - optional (defaults to 8080);
  • endpoints - an array of endpoints to configure;

Endpoint object in endpoints list:

  • methods - list of allowed methods, optional defaults to "GET";
  • path - URL path to the mocked endpoint, if not set, then defaults to catch all;
  • delay - delay in milliseconds on the server side;
  • status - HTTP response status code, optional defaults to 200;
  • json - one way of defining response payload, will output given JSON;
  • jsonPath - another way of defining response payload, will read file from the given path (can be relative to the root mock JSON file) and write its contents to response;
  • proxy - proxies requests to the given address;
  • static - serves static files;
  • errors - helps to setup sampled errors, with the randomised error codes;
  • allowCors - list of allowed domains for CORS;
  • dynamic - allows to configure dynamic read/write behaviour, i.e. values can be stored and retrieved from the inetrnal store.

mock.json is the default name for a mock configuration file, it can be renamed and set via -mock option, e.g. ./gomock -mock api.json

Dynamic mocking

You can store and retrieve values in your mocks by using dynamic property.

For writes use dynamic.write.json:

{
  "port": 8080,
  "endpoints": [
    {
      "methods": [ "POST" ],
      "path": "/note",
      "dynamic": {
        "write": {
          "json": {
            "name": "note",
            "key": "/id", // path to an entity's key inside the incoming request JSON from the client ("id" field in this case)
            "value": "." // path to an entity's value inside the incoming request JSON from the client (root in this case)
          }
        }
      }
    }
  ]
}

For reads use dynamic.read.json:

{
  "port": 8080,
  "endpoints": [
    {
      "methods": [ "GET" ],
      "path": "/note/{noteID:[a-zA-Z0-9-]+}", // uses Gorilla Mux paths
      "dynamic": {
        "read": {
          "json": {
            "name": "note",
            "keyParam": "noteID", // path to an entity's key inside the incoming request path from the client ("noteID" param in this case)
          }
        }
      }
    }
  ]
}

Changelog

See CHANGELOG.md

Contributing

See CONTRIBUTING.md

License

Released under the Apache License 2.0.

FAQs

Package last updated on 06 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc