pretender

module
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2024 License: MIT

README

pretender

A naive HTTP mock server with sequential responses from a file.


❯ bin/pretender

██████╗ ██████╗ ███████╗████████╗███████╗███╗   ██╗██████╗ ███████╗██████╗
██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝████╗  ██║██╔══██╗██╔════╝██╔══██╗
██████╔╝██████╔╝█████╗     ██║   █████╗  ██╔██╗ ██║██║  ██║█████╗  ██████╔╝
██╔═══╝ ██╔══██╗██╔══╝     ██║   ██╔══╝  ██║╚██╗██║██║  ██║██╔══╝  ██╔══██╗
██║     ██║  ██║███████╗   ██║   ███████╗██║ ╚████║██████╔╝███████╗██║  ██║
╚═╝     ╚═╝  ╚═╝╚══════╝   ╚═╝   ╚══════╝╚═╝  ╚═══╝╚═════╝ ╚══════╝╚═╝  ╚═╝ v1.7.0

• starting server on port 8080
• using responses file: examples/example.json
• press ctrl+c to stop

Install

With go install

go install github.com/kilianc/pretender/cmd/pretender@v1.7.0

With docker

docker run --rm -it \
  -p 8080:8080 \
  -v $(pwd)/examples:/examples \
  kilianciuffolo/pretender:v1.7.0 --responses /examples/example.json

With curl

# change the os (linux or darwin) and arch (amd64 or arm64) based on your machine
curl -O https://github.com/kilianc/pretender/releases/download/v1.7.0/pretender-darwin-arm64.tar.gz
tar -xzf pretender-darwin-arm64.tar.gz
./pretender --version

Usage

Every response in examples/example.json will match one consecutive http response when hitting http://localhost:8080

pretender --port 8080 --responses examples/example.json

The server has a default /healthz endpoint that responds with a 200. If this conflicts with your mock responses, it is possible to configure it by setting the PRETENDER_HEALTH_CHECK_PATH environment variable.

PRETENDER_HEALTH_CHECK_PATH=/alive pretender

Responses File

Both plain text and JSON formats are supported.

A TEXT file contains one response per line:

This line is the first text/plain response body with 200 status code
This line is the second text/plain response body with 200 status code

A JSON file allows more flexibility and controls:

[
  {
    "status_code": 200,
    "body": "hello",
    "headers": {"content-type":"text/plain"},
    "delay_ms": 1000,
    "repeat": 5
  },
  {
    "body": {
      "hello": "world"
    },
    "headers": {"Content-Type":"application/json"}
  },
  // ...
  {
    "body": "will repeat forever",
    "repeat": -1
  }
]

A valid response definition can contain the following fields

name description default
status_code HTTP status code 200
body HTTP response body ""
headers HTTP headers {"content-type":"text/plain"}
delay_ms Number of ms to wait before responding 0
repeat Number of times the response repeats or -1 for 1

Local Development

These are the usual suspects

make run
make build
make test
make cover

After running make build the binary available in the bin/ folder

bin/pretender --port 8080 --responses examples/example.json

Docker

If you prefer to build and run pretender in a docker container, just on of these commands

make docker-build
make docker-run

License

MIT License, see LICENSE

Directories

Path Synopsis
cmd
internal
pkg
tools

Jump to

Keyboard shortcuts

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