Mocking from the CLI

Introduction

There are three ways to mock requests from the CLI

  1. Dynamically configure the proxy to mock with the intercept command

  2. The mock command

  3. The request response get command

Intercept Command

The intercept command is used to configure the proxy settings with the CLI. In our case, we can use it to configure stoobly-agent to intercept and mock requests.

$ stoobly-agent intercept --help
Usage: stoobly-agent intercept [OPTIONS] COMMAND [ARGS]...

  Manage request intercept

Options:
  -h, --help  Show this message and exit.

Commands:
  configure  Configure intercept
  disable    Disable intercept
  enable     Enable intercept
  show       Show intercept

  Run 'stoobly-agent intercept COMMAND --help' for more information on a
  command.

Here's the configure sub-command to configure the proxy mode and policy.

$ stoobly-agent intercept configure --help
Usage: stoobly-agent intercept configure [OPTIONS]

  Configure intercept

Options:
  --mode [mock|record|replay]
  --policy [all|found]
  -h, --help                   Show this message and exit.

Run that command and set the mode to mock. Optionally add extra option flags as needed.

stoobly-agent intercept configure --mode mock

Next, enable the proxy to start mocking. This is equivalent to pressing the Run button in the UI

stoobly-agent intercept enable

Finally send some requests to the Agent and see your mocked HTTP requests!

As cleanup, you can optionally disable the intercept mock mode with:

stoobly-agent intercept disable

Mock Command

Here is the CLI command to mock HTTP requests:

$ stoobly-agent mock --help
Usage: stoobly-agent mock [OPTIONS] URL

  Mock request

Options:
  -d, --data TEXT      HTTP POST data
  --format [raw]       Format response
  -H, --header TEXT    Pass custom header(s) to server
  -X, --request TEXT   Specify request command to use
  --scenario-key TEXT
  -h, --help           Show this message and exit.

Its usage is very similar to cURL where you specify a URL and flags with extra request data.

For example here's a GET request:

stoobly-agent mock https://google.com

For example here's a POST request

stoobly-agent mock -X POST https://my-service.com \
    --header "Content-Type: application/json" \
    --data '{"key": "value"}'

Response Get Command

If you already have the request's key handy, you can use the request response get command

$ stoobly-agent request response get --help                  
Usage: stoobly-agent request response get [OPTIONS] REQUEST_KEY

  Retrieve mocked response

Options:
  -h, --help  Show this message and exit.

For example, if your Request key is "eyJwIjogMCwgImkiOiAzNjl9", the command would look like this:

$ stoobly-agent request response get eyJwIjogMCwgImkiOiAzNjl9
{"key": "value"}

Last updated