Code Monkey home page Code Monkey logo

rest-service-mimic's Introduction

rest-service-mimic

A totally mocked REST service, driven by config files

What does this do?

This project will let you define static, mocked REST endpoints with a JSON file. The intention is to use that as part of integration tests for things that call out to external services where we can't depend on those services serving consisting data. Somewhere between unit tests and full on integration tests.

JSON config, you say...

So, the file itself is an array of objects with the following fields:

  • path - The path of the request to handle. These can have parameters (orgs/{orgId}/whatever) or regexed parameters (orgs/{orgId:[0-9]+}/whatever)
  • methods - An array of the HTTP methods to handle (POST, GET, etc.)
  • headers - Key value pairs of the headers (and their values) that need to be part of the request. The values can be regexs, if you're in to that sort of thing.
  • query_params - Key value paris of the query parameters that need to be part of the request. These adhere to the Gorilla Mux format ("name": "{some name}")
  • response - Information about how to respond to a valid request, specifically
    • status_code - The HTTP response code to respond with
    • payload - A JSON payload to include in the body of the response. This can also include values substituted from the request payload (see section below)

Ok, but how do I use it?

This whole thing is Dockerized so, if you want to run locally all you need to do is:

  • Create the json configuration file (or just use the example one at examples/simple_example.json)
  • Run the most recent Docker image making sure to
    • Expose the port you want to hit
    • Volume mount the directory where your config file lives

So, something like this: docker run -p 8001:8001 -v $(pwd)/examples:/examples rest-service-mimic:0.0.1 -config=examples/simple_example.json -port=8001

Now try hitting the end points: curl -v localhost:8001/hello -H "Is-Test: yeah" curl -v localhost:8001/goodbye/123 curl -v localhost:8001/not_defined <- This one will return a 404 because it's not defined in the json file

How do I build this to run as a Docker image?

The easiest way to do this is just to run ./build.sh. This will:

  • Build the Golang Docker build environment
  • Volume mount the output directory of your local machine to the build image
  • Run the build image, creating an artifact (output/rest-service-mimc)
  • Then make a Docker image with the artifact (docker build -t rest-service-mimic:<version number> .)

Wait, my response payload can contain data derived from the request payload?

Yup. The response payload definition support the Go templating language. So, to include request payload information in your response payload, just add {{ .Request.my_field }}. So, for example, if you have the request payload:

{
    "id": "abc-123-def-456"
}

and you need your payload to look like

{
    "name": "Mock response",
    "provided_id": "abc-123-def-456"
}

you can define your response payload using the following Go template structure:

{
    "name": "Mock response",
    "provided_id": "{{ .Request.id }}"
}

then the id field of the request payload will be substituted into the provided_id field of the response payload. Additionally, this supports the sprig template functions (https://github.com/Masterminds/sprig) so, you can do things like this:

{
    "name": "Mock response",
    "provided_id": "{{ default "unknown" .Request.id | upper }}"
}

rest-service-mimic's People

Contributors

alexherman2 avatar stevemar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.