fake_server v2.1.0 FakeServer.Response

Response structure and helpers.

FakeServer makes use of the %FakeServer.Response{} structure to define the responses that will be given by the server.

Structure Fields

  • :status: The status code of the response. It must be an integer.
  • :body: Optional. The response body. Can be a string or a map. If the body is a map, it will be encoded so the map must be equivalent to a valid JSON.
  • :headers: Optional. The response headers. Must be a map with the string keys.

You can use the new/3 function to create a response. Since this function performs several validations, you should avoid to create the structure directly.

Link to this section Summary

Functions

Creates a new response with status 202

Creates a new response with status 202 and returns it

Returns a list with all 4xx HTTP methods available

Similar to all_4xx/0, but excludes the status codes in parameter

Returns a list with all 5xx HTTP methods available

Similar to all_5xx/0, but excludes the status codes in parameter

Creates a new response with status 502

Creates a new response with status 502 and returns it

Creates a new response with status 400

Creates a new response with status 400 and returns it

Creates a new response with status 409

Creates a new response with status 409 and returns it

Creates a new response with status 201

Creates a new response with status 201 and returns it

FakeServer default response. Used when there are no responses left to reply

Similar to default/0

Creates a new response with status 417

Creates a new response with status 417 and returns it

Creates a new response with status 424

Creates a new response with status 424 and returns it

Creates a new response with status 403

Creates a new response with status 403 and returns it

Creates a new response with status 504

Creates a new response with status 504 and returns it

Creates a new response with status 410

Creates a new response with status 410 and returns it

Creates a new response with status 505

Creates a new response with status 505 and returns it

Creates a new response with status 418

Creates a new response with status 418 and returns it

Creates a new response with status 507

Creates a new response with status 507 and returns it

Creates a new response with status 500

Creates a new response with status 500 and returns it

Creates a new response with status 411

Creates a new response with status 411 and returns it

Creates a new response with status 423

Creates a new response with status 423 and returns it

Creates a new response with status 405

Creates a new response with status 405 and returns it

Creates a new response with status 511

Creates a new response with status 511 and returns it

Creates a new Response structure. Returns {:ok, response} on success or {:error, reason} when validation fails

Similar to new/3, but raises FakeServer.Error when validation fails

Creates a new response with status 204

Creates a new response with status 204 and returns it

Creates a new response with status 203

Creates a new response with status 203 and returns it

Creates a new response with status 406

Creates a new response with status 406 and returns it

Creates a new response with status 510

Creates a new response with status 510 and returns it

Creates a new response with status 404

Creates a new response with status 404 and returns it

Creates a new response with status 501

Creates a new response with status 501 and returns it

Creates a new response with status 200

Creates a new response with status 200 and returns it

Creates a new response with status 206

Creates a new response with status 206 and returns it

Creates a new response with status 413

Creates a new response with status 413 and returns it

Creates a new response with status 412

Creates a new response with status 412 and returns it

Creates a new response with status 428

Creates a new response with status 428 and returns it

Creates a new response with status 407

Creates a new response with status 407 and returns it

Creates a new response with status 431

Creates a new response with status 431 and returns it

Creates a new response with status 408

Creates a new response with status 408 and returns it

Creates a new response with status 205

Creates a new response with status 205 and returns it

Creates a new response with status 503

Creates a new response with status 503 and returns it

Creates a new response with status 429

Creates a new response with status 429 and returns it

Creates a new response with status 401

Creates a new response with status 401 and returns it

Creates a new response with status 422

Creates a new response with status 422 and returns it

Creates a new response with status 415

Creates a new response with status 415 and returns it

Creates a new response with status 426

Creates a new response with status 426 and returns it

Creates a new response with status 414

Creates a new response with status 414 and returns it

Creates a new response with status 506

Creates a new response with status 506 and returns it

Link to this section Functions

Link to this function

accepted(body \\ "", headers \\ %{})

Creates a new response with status 202

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

accepted!(body \\ "", headers \\ %{})

Creates a new response with status 202 and returns it.

Raises FakeServer.Error if the validation fails.

Returns a list with all 4xx HTTP methods available

Similar to all_4xx/0, but excludes the status codes in parameter.

Returns a list with all 5xx HTTP methods available.

Similar to all_5xx/0, but excludes the status codes in parameter.

Link to this function

bad_gateway(body \\ "", headers \\ %{})

Creates a new response with status 502

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

bad_gateway!(body \\ "", headers \\ %{})

Creates a new response with status 502 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

bad_request(body \\ "", headers \\ %{})

Creates a new response with status 400

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

bad_request!(body \\ "", headers \\ %{})

Creates a new response with status 400 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

conflict(body \\ "", headers \\ %{})

Creates a new response with status 409

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

conflict!(body \\ "", headers \\ %{})

Creates a new response with status 409 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

created(body \\ "", headers \\ %{})

Creates a new response with status 201

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

created!(body \\ "", headers \\ %{})

Creates a new response with status 201 and returns it.

Raises FakeServer.Error if the validation fails.

FakeServer default response. Used when there are no responses left to reply.

iex> FakeServer.Response.default()
{:ok,
  %FakeServer.Response{
    body: "{"message": "This is a default response from FakeServer"}",
    headers: %{},
    status: 200
  }
}

Similar to default/0.

Link to this function

expectation_failed(body \\ "", headers \\ %{})

Creates a new response with status 417

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

expectation_failed!(body \\ "", headers \\ %{})

Creates a new response with status 417 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

failed_dependency(body \\ "", headers \\ %{})

Creates a new response with status 424

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

failed_dependency!(body \\ "", headers \\ %{})

Creates a new response with status 424 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

forbidden(body \\ "", headers \\ %{})

Creates a new response with status 403

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

forbidden!(body \\ "", headers \\ %{})

Creates a new response with status 403 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

gateway_timeout(body \\ "", headers \\ %{})

Creates a new response with status 504

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

gateway_timeout!(body \\ "", headers \\ %{})

Creates a new response with status 504 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

gone(body \\ "", headers \\ %{})

Creates a new response with status 410

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

gone!(body \\ "", headers \\ %{})

Creates a new response with status 410 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

http_version_not_supported(body \\ "", headers \\ %{})

Creates a new response with status 505

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

http_version_not_supported!(body \\ "", headers \\ %{})

Creates a new response with status 505 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

im_a_teapot(body \\ "", headers \\ %{})

Creates a new response with status 418

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

im_a_teapot!(body \\ "", headers \\ %{})

Creates a new response with status 418 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

insufficient_storage(body \\ "", headers \\ %{})

Creates a new response with status 507

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

insufficient_storage!(body \\ "", headers \\ %{})

Creates a new response with status 507 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

internal_server_error(body \\ "", headers \\ %{})

Creates a new response with status 500

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

internal_server_error!(body \\ "", headers \\ %{})

Creates a new response with status 500 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

length_required(body \\ "", headers \\ %{})

Creates a new response with status 411

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

length_required!(body \\ "", headers \\ %{})

Creates a new response with status 411 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

locked(body \\ "", headers \\ %{})

Creates a new response with status 423

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

locked!(body \\ "", headers \\ %{})

Creates a new response with status 423 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

method_not_allowed(body \\ "", headers \\ %{})

Creates a new response with status 405

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

method_not_allowed!(body \\ "", headers \\ %{})

Creates a new response with status 405 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

network_authentication_required(body \\ "", headers \\ %{})

Creates a new response with status 511

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

network_authentication_required!(body \\ "", headers \\ %{})

Creates a new response with status 511 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

new(status_code, body \\ "", headers \\ %{})

Creates a new Response structure. Returns {:ok, response} on success or {:error, reason} when validation fails

Example

iex> FakeServer.Response.new(200, %{name: "Test User", email: "test_user@test.com"}, %{"Content-Type" => "application/json"})
iex> FakeServer.Response.new(200, ~s<{"name":"Test User","email":"test_user@test.com"}>, %{"Content-Type" => "application/json"})
iex> FakeServer.Response.new(201, ~s<{"name":"Test User","email":"test_user@test.com"}>)
iex> FakeServer.Response.new(404)
Link to this function

new!(status_code, body \\ "", headers \\ %{})

Similar to new/3, but raises FakeServer.Error when validation fails.

Link to this function

no_content(body \\ "", headers \\ %{})

Creates a new response with status 204

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

no_content!(body \\ "", headers \\ %{})

Creates a new response with status 204 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

non_authoritative_information(body \\ "", headers \\ %{})

Creates a new response with status 203.

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

non_authoritative_information!(body \\ "", headers \\ %{})

Creates a new response with status 203 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

not_acceptable(body \\ "", headers \\ %{})

Creates a new response with status 406

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

not_acceptable!(body \\ "", headers \\ %{})

Creates a new response with status 406 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

not_extended(body \\ "", headers \\ %{})

Creates a new response with status 510

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

not_extended!(body \\ "", headers \\ %{})

Creates a new response with status 510 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

not_found(body \\ "", headers \\ %{})

Creates a new response with status 404

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

not_found!(body \\ "", headers \\ %{})

Creates a new response with status 404 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

not_implemented(body \\ "", headers \\ %{})

Creates a new response with status 501

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

not_implemented!(body \\ "", headers \\ %{})

Creates a new response with status 501 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

ok(body \\ "", headers \\ %{})

Creates a new response with status 200

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

ok!(body \\ "", headers \\ %{})

Creates a new response with status 200 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

partial_content(body \\ "", headers \\ %{})

Creates a new response with status 206

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

partial_content!(body \\ "", headers \\ %{})

Creates a new response with status 206 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

payload_too_large(body \\ "", headers \\ %{})

Creates a new response with status 413

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

payload_too_large!(body \\ "", headers \\ %{})

Creates a new response with status 413 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

precondition_failed(body \\ "", headers \\ %{})

Creates a new response with status 412

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

precondition_failed!(body \\ "", headers \\ %{})

Creates a new response with status 412 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

precondition_required(body \\ "", headers \\ %{})

Creates a new response with status 428

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

precondition_required!(body \\ "", headers \\ %{})

Creates a new response with status 428 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

proxy_authentication_required(body \\ "", headers \\ %{})

Creates a new response with status 407

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

proxy_authentication_required!(body \\ "", headers \\ %{})

Creates a new response with status 407 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

request_header_fields_too_large(body \\ "", headers \\ %{})

Creates a new response with status 431

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

request_header_fields_too_large!(body \\ "", headers \\ %{})

Creates a new response with status 431 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

request_timeout(body \\ "", headers \\ %{})

Creates a new response with status 408

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

request_timeout!(body \\ "", headers \\ %{})

Creates a new response with status 408 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

reset_content(body \\ "", headers \\ %{})

Creates a new response with status 205

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

reset_content!(body \\ "", headers \\ %{})

Creates a new response with status 205 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

service_unavailable(body \\ "", headers \\ %{})

Creates a new response with status 503

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

service_unavailable!(body \\ "", headers \\ %{})

Creates a new response with status 503 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

too_many_requests(body \\ "", headers \\ %{})

Creates a new response with status 429

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

too_many_requests!(body \\ "", headers \\ %{})

Creates a new response with status 429 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

unauthorized(body \\ "", headers \\ %{})

Creates a new response with status 401

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

unauthorized!(body \\ "", headers \\ %{})

Creates a new response with status 401 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

unprocessable_entity(body \\ "", headers \\ %{})

Creates a new response with status 422

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

unprocessable_entity!(body \\ "", headers \\ %{})

Creates a new response with status 422 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

unsupported_media_type(body \\ "", headers \\ %{})

Creates a new response with status 415

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

unsupported_media_type!(body \\ "", headers \\ %{})

Creates a new response with status 415 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

upgrade_required(body \\ "", headers \\ %{})

Creates a new response with status 426

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

upgrade_required!(body \\ "", headers \\ %{})

Creates a new response with status 426 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

uri_too_long(body \\ "", headers \\ %{})

Creates a new response with status 414

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

uri_too_long!(body \\ "", headers \\ %{})

Creates a new response with status 414 and returns it.

Raises FakeServer.Error if the validation fails.

Link to this function

variant_also_negotiates(body \\ "", headers \\ %{})

Creates a new response with status 506

Returns {:ok, response} tuple on succes and {:error, reason} when validation fails.

Link to this function

variant_also_negotiates!(body \\ "", headers \\ %{})

Creates a new response with status 506 and returns it.

Raises FakeServer.Error if the validation fails.