http_server_mock/response

Builder for ResponseDefinition — what the server sends back when a stub matches an incoming request.

Start with new() (or one of the shorthand constructors) and pipe through the functions you need.

let resp =
  response.new()
  |> response.status(201)
  |> response.header("x-request-id", "abc123")
  |> response.json_body("{\"id\":1,\"status\":\"created\"}")

Values

pub fn accepted() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 202 Accepted with no body.

pub fn bad_request() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 400 Bad Request with no body.

pub fn body(
  response_def: types.ResponseDefinition,
  text: String,
) -> types.ResponseDefinition

Sets the response body to a plain text string.

pub fn created() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 201 Created with no body.

pub fn delay(
  response_def: types.ResponseDefinition,
  milliseconds: Int,
) -> types.ResponseDefinition

Delays the response by milliseconds before sending it.

Useful for testing timeout handling or slow-network behaviour.

pub fn forbidden() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 403 Forbidden with no body.

pub fn header(
  response_def: types.ResponseDefinition,
  key: String,
  value: String,
) -> types.ResponseDefinition

Adds a response header. Can be called multiple times to add several headers.

pub fn json_body(
  response_def: types.ResponseDefinition,
  json: String,
) -> types.ResponseDefinition

Sets the response body to a JSON string and automatically adds a content-type: application/json header.

pub fn new() -> types.ResponseDefinition

Returns a new ResponseDefinition with status 200, no headers, and no body.

pub fn no_content() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 204 No Content with no body.

pub fn not_found() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 404 Not Found with no body.

pub fn ok() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 200 OK with no body.

pub fn server_error() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 500 Internal Server Error with no body.

pub fn status(
  response_def: types.ResponseDefinition,
  code: Int,
) -> types.ResponseDefinition

Sets the HTTP status code for the response.

pub fn unauthorized() -> types.ResponseDefinition

Returns a ResponseDefinition for HTTP 401 Unauthorized with no body.

Search Document