Sign inSign up

pocket/api_mock

By pocket

•Updated over 5 years ago

Emulate api services with set values

Image
0

100K+

pocket/api_mock repository overview

⁠apimock

A proxy server to help mock external calls.

⁠V1 API

DELETE /guzzle-server

Shuts down server.

DELETE /guzzle-server/requests

Flushes requests and responses.

PUT /guzzle-server/responses

Queues responses. Request body:

[
    {
        "status": 200,
        "reason": "OK",
        "body": "AbcDfjefNGie",
        "headers": {}
    }
]

body must be base64-encoded if given. headers['content-length'], if supplied, must reflect the raw body's length.

GET /guzzle-server/requests

Returns all the requests received so far (e.g. GET /random). Response body:

[
    {
        "http_method": "GET",
        "domain": "${request.headers.host}",
        "uri": "/random",
        "query_string": "string | null",
        "headers": {},
        "body": ""
    }
]

* * (for paths not starting with /guzzle-server)

Return next response from queue. If there are no more responses, return:

HTTP/1.1 500

No responses in queue

⁠V2 API

V2 adds more introspection and control to the mock server.

GET /guzzle-server/status

Returns current state of the server. Response:

{
    "requestsReceived": 0,
    "requestsUnexpected": 0,
    "responsesQueued": 0,
    "spyMode": {
        "enabled": true,
        "defaultResponse": {
            "status": 200,
            "reason": "OK",
            "body": "AbcDfjefNGie",
            "headers": {}
        }
    }
}

PUT /guzzle-server/spymode

Enables spy mode, which allows requests that have no responses queued. Payload body defines the default response. If called multiple times, each call may change the default response without resetting requests. If request body is empty, uses default system response (plain 200 with body {})

# todo

GET /guzzle-server/requests/unexpected

Returns unqueued requests if they are received when spymode is enabled.

GET /guzzle-server/requests/all

Returns both queued and unqueued requests. Returned in the order they were received.

DELETE /guzzle-server/spymode

Disables spy mode. All requests will remain in queue until flushed.

PUT /guzzle-server/responses (Extended)

Smarter response queueing -- match requests to responses by decorating each response with "match": object to have more granular control. object is 1 or more key-value pairs:

  • method: string|string[]: 1 or more methods
  • path: string|string[]: 1 or more paths. Simple wildcards * and ? are supported
  • domain: string|string[]" 1 or more domains to match. Use domain:port for each value if you need to supply a port

If a key is omitted, it's not evaluated.

Example:

[
    {
        "status": 200,
        "body": "",
        "match": {
            "path": "/expected/path*",
            "domain": ["api-mock:1234", "aws.amazon.com"],
            "method": "POST"
        }
    },
    {
        "status": 200,
        "body": "",
        "match": {
            "path": "/expected/path/?*",
        }
    },
    {
        "status": 200,
        "body": ""
    }
]

When a request is made:

  1. Find the first matchable response. Match response is removed from queue; OR
  2. Fall back to V1

Tag summary

Content type

Image

Digest

Size

251.4 MB

Last updated

over 7 years ago

docker pull pocket/api_mock