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,
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

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: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  data: BitArray,
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

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: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  data: List(#(String, String)),
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

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: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  html: String,
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

Add an HTML body to a request.

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

pub fn json_body(
  request: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  data: json.Json,
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

Add a JSON body to a request.

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

pub fn multipart_body(
  request: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  values values: List(#(String, String)),
  files files: List(#(String, FileUpload)),
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

Add a multipart form body to a request.

The content-type header includes the generated multipart boundary.

pub fn read_bytes(
  response: response.Response(
    @internal ReadableStream(@internal Uint8Array),
  ),
) -> 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: response.Response(
    @internal ReadableStream(@internal Uint8Array),
  ),
) -> 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,
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

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: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  previous_request: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  previous_response: response.Response(
    @internal ReadableStream(@internal Uint8Array),
  ),
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

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: request.Request(
    @internal ReadableStream(@internal Uint8Array),
  ),
  text: String,
) -> request.Request(@internal ReadableStream(@internal Uint8Array))

Add a UTF-8 text body to a request.

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

Search Document