mock

command module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 1 Imported by: 0

README

mock

Language-agnostic API mocking and testing utility

Go Reference Go Report Card

mock enables you to quickly setup a HTTP server for end-to-end tests.

  • Define endpoints and their respective responses;
  • Make assertions on...
    • Whether a given endpoint was requested;
    • If a JSON payload body was passed correctly to a given endpoint;
    • If a header value was passed correctly;
    • And other useful things...

The Getting Started section ahead gives you an overview of basic usage. To learn all the advanced features, check the User Guide.

Getting started

$ mock serve -c /path/to/your/config.json -p 4000

Below is a configuration file sample, defining two simple endpoints:

{
  "endpoints": [
    {
      "route": "foo/bar",
      "method": "POST",
      "response": {
        "foo": "bar"
      }
    },
    {
      "route": "hello/world",
      "method": "GET",
      "response": {
        "some_key": "some_value"
      }
    }
  ]
}

Let's now make a request to the foo/bar endpoint:

$ curl localhost:4000/foo/bar \
  -H 'Content-type: application/json' \
  -d '{"some_key":"some_value"}'

And then let's make assertions - Let's verify whether the foo/bar endpoint was called with the expected values:

$ curl -v http://localhost:4000/__mock__/assert -d @- <<EOF
{
  "route": "foo/bar",
  "assert": {
    "type": "json_body_match",
    "key_values": {
      "some_key": "some_value"
    },
    "and": {
      "type": "method_match",
      "value": "put"
    }
  }
}
EOF

In the command above we're trying to assert the following:

The foo/bar endpoint was called, with the JSON Payload {"some_key":"some_value"}, and with the put method.

Obviously, there's a problem with that assertion - the request we made previously was a post request, not put, therefore we get a response indicating so:

{
  "validation_errors": [
    {
      "code": "method_mismatch",
      "metadata": {
        "method_expected": "put",
        "method_requested": "post"
      }
    }
  ]
}

Installing

mock is distributed as a single-file executable. Check the releases page and download the latest tarball.

License

mock is licensed under MIT. For more information check the LICENSE file.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
cmd
pkg
tests

Jump to

Keyboard shortcuts

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