Vaultx.Test.HTTPHelpers (Vaultx v0.7.0)

View Source

Comprehensive HTTP mocking utilities for Vaultx testing.

This module provides a unified, consistent interface for mocking HTTP interactions in Vaultx tests. It supports all HTTP methods, automatic JSON handling, flexible assertions, and both single-use expectations and reusable stubs.

Key Features

  • Method Coverage: Support for all HTTP methods (GET, POST, PUT, PATCH, DELETE)
  • JSON Handling: Automatic JSON encoding/decoding with configurable options
  • Flexible Assertions: URL, body, and options validation with custom functions
  • Error Simulation: Both structured Vaultx errors and raw error conditions
  • Response Patterns: Standard and enveloped response formats
  • Test Isolation: Single-use expectations and reusable stubs

Usage Patterns

# Simple GET expectation
expect_get(200, %{"data" => "value"})

# POST with body validation
expect_post(201, %{"id" => 123}, fn _url, body, _opts ->
  assert body["name"] == "test"
end)

# Error simulation
stub_request(:get, :not_found, "Secret not found")

References

Summary

Functions

Assert error type and call function

Assert LIST method in opts

Assert URL contains fragment

Return a standard successful HTTP tuple

Return an enveloped response with data wrapper

Stub any method with Vaultx.Base.Error

Stub any method with raw error

Functions

assert_error(expected_type, fun)

Assert error type and call function

assert_list_method(url, body, opts)

Assert LIST method in opts

assert_url_contains(fragment)

Assert URL contains fragment

expect_any(method, status, body, assert_fn \\ fn _url, _body, _opts -> :ok end)

Expect any method with simple response (fallback)

expect_delete(status, body, assert_fn \\ fn _url, _body, _opts -> :ok end)

Expect a DELETE request

expect_delete_with_headers(status, body, assert_fn \\ fn _url, _body, _headers, _opts -> :ok end)

Expect a DELETE request with headers access

expect_get(status, body, assert_fn \\ fn _url, _body, _opts -> :ok end)

Expect a GET request

expect_get_enveloped(status, inner_data, assert_fn \\ fn _url, _body, _opts -> :ok end)

Expect GET with enveloped data response

expect_get_with_headers(status, body, assert_fn \\ fn _url, _body, _headers, _opts -> :ok end)

Expect a GET request with headers access

expect_patch(status, body, assert_fn \\ fn _url, _body, _opts -> :ok end, opts \\ [])

Expect a PATCH request with optional JSON decode

expect_patch_with_headers(status, body, assert_fn \\ fn _url, _body, _headers, _opts -> :ok end, opts \\ [])

Expect a PATCH request with headers access and optional JSON decode

expect_post(status, body, assert_fn \\ fn _url, _body, _opts -> :ok end, opts \\ [])

Expect a POST request with optional JSON decode

expect_post_with_headers(status, body, assert_fn \\ fn _url, _body, _headers, _opts -> :ok end, opts \\ [])

Expect a POST request with headers access and optional JSON decode

expect_put(status, body, assert_fn \\ fn _url, _body, _opts -> :ok end, opts \\ [])

Expect a PUT request with optional JSON decode

expect_put_with_headers(status, body, assert_fn \\ fn _url, _body, _headers, _opts -> :ok end, opts \\ [])

Expect a PUT request with headers access and optional JSON decode

ok_resp(status, body)

Return a standard successful HTTP tuple

ok_resp_enveloped(status, inner_data)

Return an enveloped response with data wrapper

stub_ok(method, status, body, assert_fn \\ fn _url, _body, _opts -> :ok end, opts \\ [])

Stub any method with success response

stub_ok_with_headers(method, status, body, assert_fn \\ fn _url, _body, _headers, _opts -> :ok end, opts \\ [])

Stub any method with success response and headers access

stub_request(method, type, message)

Stub any method with Vaultx.Base.Error

stub_request_raw(method, reason)

Stub any method with raw error