IbkrApi.Support.HTTPSandbox (ibkr_api v1.0.3)

View Source

HTTP sandbox for mocking HTTP requests in tests.

Stores response functions in a Registry under the PID of the test process. In test, the IbkrApi.SharedUtils.HTTP module will check this sandbox before making actual HTTP requests.

Summary

Functions

Retrieves the response for a DELETE request

Finds the appropriate response function for the given action and URL. Returns the function or raises an error.

Retrieves the response for a GET request

Retrieves the response for a HEAD request

Retrieves the response for a POST request

Retrieves the response for a PUT request

Checks if the HTTP sandbox is disabled. Used by the HTTP client to determine whether to use mock responses.

Sets sandbox responses for DELETE requests.

Sets sandbox responses for GET requests.

Sets sandbox responses for HEAD requests.

Sets sandbox responses for POST requests.

Sets sandbox responses for PUT requests.

Start the HTTP sandbox registry. Should be called from test_helper.exs

Types

action()

@type action() :: :get | :post | :put | :delete

body()

@type body() :: map() | String.t()

headers()

@type headers() :: keyword() | list()

options()

@type options() :: keyword()

url()

@type url() :: String.t()

Functions

delete_response(url, headers, options)

@spec delete_response(url(), headers(), options()) :: any()

Retrieves the response for a DELETE request

find!(action, url)

@spec find!(action(), url()) :: (... -> any())

Finds the appropriate response function for the given action and URL. Returns the function or raises an error.

get_response(url, headers, options)

@spec get_response(url(), headers(), options()) :: any()

Retrieves the response for a GET request

head_response(url, headers, options)

@spec head_response(url(), headers(), options()) :: any()

Retrieves the response for a HEAD request

post_response(url, body, headers, options)

@spec post_response(url(), body(), headers(), options()) :: any()

Retrieves the response for a POST request

put_response(url, body, headers, options)

@spec put_response(url(), body(), headers(), options()) :: any()

Retrieves the response for a PUT request

sandbox_disabled?()

@spec sandbox_disabled?() :: boolean()

Checks if the HTTP sandbox is disabled. Used by the HTTP client to determine whether to use mock responses.

Always returns false in tests, meaning the sandbox is always enabled.

set_delete_responses(tuples)

@spec set_delete_responses([{String.t(), (... -> any())}]) :: :ok

Sets sandbox responses for DELETE requests.

Examples

HTTPSandbox.set_delete_responses([
  {"https://localhost:5000/v1/api/iserver/account/order/12345", fn ->
    {:ok, %{"id" => "12345"}, %IbkrApi.SharedUtils.HTTP.Response{status: 200}}
  end}
])

set_get_responses(tuples)

@spec set_get_responses([{String.t(), (... -> any())}]) :: :ok

Sets sandbox responses for GET requests.

Examples

HTTPSandbox.set_get_responses([
  {"https://localhost:5000/v1/api/iserver/auth/status", fn ->
    {:ok, %{
      "authenticated" => true,
      "competing" => false,
      "connected" => true,
      "message" => ""
    }, %IbkrApi.SharedUtils.HTTP.Response{status: 200}}
  end}
])

set_head_responses(tuples)

@spec set_head_responses([{String.t(), (... -> any())}]) :: :ok

Sets sandbox responses for HEAD requests.

Examples

HTTPSandbox.set_head_responses([
  {"https://localhost:5000/v1/api/iserver/auth/status", fn ->
    {:ok, %{}, %IbkrApi.SharedUtils.HTTP.Response{status: 200}}
  end}
])

set_post_responses(tuples)

@spec set_post_responses([{String.t(), (... -> any())}]) :: :ok

Sets sandbox responses for POST requests.

Examples

HTTPSandbox.set_post_responses([
  {"https://localhost:5000/v1/api/iserver/account", fn ->
    {:ok, %{"acctId" => "U12345678"}, %IbkrApi.SharedUtils.HTTP.Response{status: 200}}
  end}
])

set_put_responses(tuples)

@spec set_put_responses([{String.t(), (... -> any())}]) :: :ok

Sets sandbox responses for PUT requests.

Examples

HTTPSandbox.set_put_responses([
  {"https://localhost:5000/v1/api/iserver/account/orders", fn ->
    {:ok, %{"id" => "12345"}, %IbkrApi.SharedUtils.HTTP.Response{status: 200}}
  end}
])

start_link()

@spec start_link() :: {:error, any()} | {:ok, pid()}

Start the HTTP sandbox registry. Should be called from test_helper.exs