sayjava / mimic

A data generation and API mocking server

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mimic

Mimic is an API mocking and fake data generation server. It provides a simple approach to virtualizing services

records

mocks

Why Mimic

  • Prototype against APIs easily
  • Test boundary values by mocking responses
  • Test alternative scenarios by varying responses for requests
  • Write predictable acceptance tests with predictable API responses
  • Debug HTTP requests made by applications under development
  • Generate large dataset using declarative response templates
  • Validate API calls

Mimic Features

  • Declarative mock definitions
  • Transparently proxy requests to remote servers.
  • Built-in handlebars and Fakerjs for large dataset generation.
  • Reusable mock templates which helps to reduce mock duplications.

Requirements

  • Docker v20+

Quick Start

    docker run docker.io/sayjava/mimic -p 8080:8080

The server is now running at http://localhost:8080. Any request made to that endpoint will be recorded and can be examined at http://localhost:8080/_/dashboard

Use mock definitions

    docker run docker.io/sayjava/mimic -p 8080:8080 -v "mocks:/app/mocks"

This will load all the mock definitions present in the mocks folder.

Mocking

Mocks are declarative yaml/json documents that instructs Mimic how to responds to the requests it receives. Here are some simple examples of mock definitions.

Regex based request paths and methods

A very simple regexp based mock that will respond to requests with paths like todo/2 or todo/some-id for both GET or POST request methods.

    - name: Sample Mock
      request: 
        path: /todo/.*
        method: GET|POST
      response:
        status: 200
        headers:
            content-type: application/json
        body:
          id: sample-id
          status: done

See the Mock Definition docs here for more details

Fake Data Generation

Fake data generation is built into Mimic. This endpoint /todos will consistently generate 10 todo objects

    - name: Dynamic Data
      request: 
        path: /todos
        method: GET
      response:
        status: 200
        headers:
            content-template: json/template
        body: |
            [
                {{#repeat 10}}
                    {
                        "title": "{{data.random.words}}",
                        "createdAt": "{{data.date.recent}}",
                    }
                {{/repeat}}
            ]

See the Data Generation docs for more examples

Mimic Use Cases

Here are some use cases that Mimic can help with during development and testing.

Fully Mocked Server Side Rendered Apps

Server side rendered apps can point their API endpoints to Mimic as the their remote API and all requests will be matched against mock definitions.

sequenceDiagram
    participant c as Client(Browser)
    participant s as Server App
    participant m as Mimic(Remote API)
    c->>s: Load Page
    s->>m: Remote API Call
    loop Match Mocks
        m->>m: Data Generation/Return static mock
    end
    m-->>s: Mocked/404 Response
    s->>c: Rendered Page
Loading

Fully Mocked Client Side Rendered Apps

Server side rendered apps can point their API endpoints to Mimic as the their remote API and all requests will be matched against mock definitions.

sequenceDiagram
    participant c as Client(Browser)
    participant m as Mimic(Remote API)
    c->>m: XHR Request
    loop Match Mocks
        m->>m: Data Generation/Return static mock
    end
    m-->>c: Mocked/404 Response
Loading

Transparent Proxy Mode

In this mode, Mimic acts as a transparent proxy by forwarding all requests it receives to a defined remote server transparently and depending on the mock definition, it will cache the response and respond with the cache on subsequent matching requests.

sequenceDiagram
    participant c as Client
    participant m as Mimic
    participant r as Remote Server
    c->>m: HTTP Request Request
    loop Match Mocks
        m->>m: Match a forwarding definition
    end
    m->>r: Forward request to remote server 
    r->>m: Remote response
    alt Cache on response?
        m->>c: cache and return response
    else
        m->>c: return response
    end
Loading

see the proxy docs for more information on forwarding requests to remote servers

Examples

Development

    docker compose up

The Mimic mock server is started on http://localhost:8080 and the UI dashboard can be reached at http://localhost:9090/_/dashboard

Built With

  • Deno v1.30
  • VueJS 3.0
  • Handlebars
  • FakerJS

Deploy

docker compose up

About

A data generation and API mocking server


Languages

Language:TypeScript 80.7%Language:Vue 14.4%Language:Handlebars 3.6%Language:Dockerfile 0.5%Language:CSS 0.3%Language:HTML 0.3%Language:JavaScript 0.3%