dynamock

command module
v0.0.0-...-49a7d7b Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dynamock

This is a api to run a simple http server, which can serve up mock service responses. Responses can be JSON to simulate REST.

Development
go run main.go -c templates/sample.json
Configuration

On startup, config values are loaded from the config.json file.

  • Services can be configured to return different responses, depending on a request parameter or request header.
  • Content-type for a service response can be set for each service. If not set, content-type defaults to application/text.
  • HTTP Status code can be set for each service.
  • Latency (ms) can be set to simulate slow service responses. Latency can be set for a single service, or globally for all services.
  • Additional headers can be defined for responses.
{
    "port": "3010",
    "latency": 30,
    "contentType": "application/json",
    "mockDir": "templates/mock",
    "services": {
        "ping": {
            "contentType": "application/json",
            "responses": {
                "get": {
                    "status": 200,
                    "body": "json"
                }
            }
        },
        "api/:id/services": {
            "contentType": "application/json",
            "responses": {
                "get": {
                    "status": 200,
                    "body": "{\"id\": 1, \"name\": \"service\"}"
                },
                "post": {
                    "status": 200
                },
                "put": {
                    "status": 200
                },
                "delete": {
                    "status": 200
                }
            }
        },
        "api/:id/files": {
            "contentType": "application/json",
            "responses": {
                "get": {
                    "status": 200,
                    "bodyFile": "files.json"
                 }
            }
        },
        "dyn1": {
            "responses": {
                "get": {
                    "dynamic": {
                        "random": {
                            "status": [200, 201],
                            "body": ["ok1", "ok2", "ok3"]
                        }
                    }
                 }
            }
        },
        "dyn2": {
            "responses": {
                "get": {
                    "dynamic": {
                        "random": {
                            "status": [200, 201, 500],
                            "bodyFile": [
                                "dyn.1.json",
                                "dyn.2.json"
                            ]
                        }
                    }
                 }
            }
        },
        "headers": {
            "contentType": "application/json",
            "headers": {
                "x-requested-by": "a1334c7dh3a8",
                "authorization": "Bearer a1334c7dh3a8"
            },
            "responses": {
                "get": {
                    "status": 200,
                    "bodyFile": "files.json"
                 }
            }
        },
        "switch": {
            "contentType": "application/json",
            "headers": {
                "x-requested-by": "a1334c7dh3a8",
                "authorization": "Bearer a1334c7dh3a8"
            },
            "responses": {
                 "get": {
                    "dynamic": {
                        "switch": {
                            "id": [
                                {"if": "1", "when": {"status": 200, "body": "ok1"}},
                                {"if": "2", "when": {"status": 200, "body": "ok2"}},
                                {"if": "3", "when": {"status": 200, "body": "ok3"}}
                            ],
                            "name": [
                                {"if": "go", "when": {"status": 500, "bodyFile": "error.json"}}
                            ]
                        }
                    }
                 }
            }
        },
        "api/qrcode": {
            "contentType": "image/png",
            "responses": {
                 "get": {
                    "dynamic": {
                        "qrcode": {
                            "status": 200,
                            "quality": "highest",
                            "size": 512,
                            "content": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJjcGYiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiZGF0YU5hc2NpbWVudG8iOiIwNy8wMy8xOTgyIiwiY2FydGVpcmEiOiIxMjM0NTYiLCJuaSI6IjEyMzQ1Njc4OSJ9.b7ef9n68RWvNYF-T6VsU7rO-_DFdqkLrwEvQyx9ynGL4RrS1ZcEUdPPkTNGx5Mo-3rTcwX7AypxatNah1YVPvbVLot3tVn_-rNU87p6ulWtlcqZwzj7eoTRQYiL-nyZUFvizuIfOhc5SGf0F6gkoLbd9ch3XmtQ2uqxP0Q__ybs"
                        }
                    }
                 }
            }
        },
        "ping_latency": {
            "latency": 1000,
            "responses": {
                "get": {
                    "status": 200,
                    "body": "latency_2000"
                }
            }
        },
        "/api/:id/vars": {
            "latency": 10,
            "responses": {
                "get": {
                    "status": 200,
                    "body": "@id @cpf (params and queryparams)"
                }
            }
        },
        "/api/:id/vars/file": {
            "latency": 10,
            "responses": {
                "get": {
                    "status": 200,
                    "bodyFile": "vars.json"
                }
            }
        },
        "jwt": {
            "responses": {
                "get": {
                    "dynamic": {
                        "jwt": {
                            "status": 200,
                            "alg": "HS512",
                            "payload": "{ \"sub\": \"sub\", \"iss\": \"https://jsbin.com\" }",
                            "secret": "SECRET_KEY",
                            "output": "{\"access_token_custom\": \"@token\"}"
                        }
                    }
                }
            }
        }
    }
}
Usage
# step 1 - build docker
make docker

# compose
docker-compose up -d

# binary
curl -L https://github.com/helderfarias/dynamock/releases/download/v1.0/dynamock_darwin_osx.zip > dynamock.zip \
    && unzip dynamock.zip \
    && rm dynamock.zip

# shell
docker run --rm -ti -v $PWD/templates:/etc/config helderfarias/dynamock -c /etc/config/basic.json
Plugins
  • QrCode
  • Switch response
  • Random response
  • JWT response
Inspired

https://github.com/gstroup/apimocker

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cli

Jump to

Keyboard shortcuts

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