smol/simulate

Types

A file to include in a simulated multipart form submission.

pub type FileUpload {
  FileUpload(
    file_name: String,
    content_type: String,
    content: BitArray,
  )
}

Constructors

  • FileUpload(
      file_name: String,
      content_type: String,
      content: BitArray,
    )

Values

pub fn browser_request(
  method: http.Method,
  path: String,
) -> smol.Request

Create a test request that resembles a request made by a browser.

This includes an origin header, which is useful when testing cookies and cross-origin policies.

pub fn bytes_body(
  request: smol.Request,
  data: BitArray,
) -> smol.Request

Add a binary body to a request.

The content-type header is set to application/octet-stream.

pub const default_host: String

The default host for test requests.

pub fn form_body(
  request: smol.Request,
  data: List(#(String, String)),
) -> smol.Request

Add a URL-encoded form body to a request.

The content-type header is set to application/x-www-form-urlencoded.

pub fn html_body(
  request: smol.Request,
  html: String,
) -> smol.Request

Add an HTML body to a request.

The content-type header is set to text/html; charset=utf-8.

pub fn json_body(
  request: smol.Request,
  data: json.Json,
) -> smol.Request

Add a JSON body to a request.

The content-type header is set to application/json.

pub fn multipart_body(
  request: smol.Request,
  values values: List(#(String, String)),
  files files: List(#(String, FileUpload)),
) -> smol.Request

Add a multipart form body to a request.

The content-type header includes the generated multipart boundary.

pub fn read_bytes(
  response: smol.Response,
) -> promise.Promise(Result(BitArray, Nil))

Read an entire response body as binary data.

This consumes the response body stream, which can only be read once. It waits for the stream to close, so it is not suitable for unbounded streams such as server-sent events.

pub fn read_string(
  response: smol.Response,
) -> promise.Promise(Result(String, Nil))

Read an entire response body as UTF-8 text.

This consumes the response body stream, which can only be read once. It waits for the stream to close, so it is not suitable for unbounded streams such as server-sent events.

pub fn request(method: http.Method, path: String) -> smol.Request

Create a test request that can be used to test your request handler functions.

If you are testing handlers that are intended to be accessed from a browser (such as those that use cookies) consider using browser_request instead.

pub fn session(
  next_request: smol.Request,
  previous_request: smol.Request,
  previous_response: smol.Response,
) -> smol.Request

Continue a lightweight browser session from a previous request and response.

Existing request cookies are carried forward, then updated or removed using the response’s set-cookie headers. Cookie path, domain, secure, and expiry rules are not simulated. Cookies are removed only by a Max-Age=0 attribute.

pub fn string_body(
  request: smol.Request,
  text: String,
) -> smol.Request

Add a UTF-8 text body to a request.

The content-type header is set to text/plain; charset=utf-8.

Search Document