Sakura Lang

Test a router endpoint directly

Call the router as a function to test dispatch, request validation and response handling.

Call the router as a function to test dispatch, request validation and response handling.

Request.json validates a refined name and caps the body at 256 bytes. The handler maps BadRequest to a generic 400 response. Router.handle dispatches by method and path, producing 404 or 405 when appropriate. There is no listener, socket, Serve handler or permission to configure. Route patterns are std/urlpattern patterns: :name matches one segment, and /docs/** matches any number of them, which the handler reads as Request.param(req, "0").

The tests call the actual router, not just the endpoint function. They assert the successful response body, rejection of malformed JSON, an invalid refinement and an oversized body, plus missing-route and wrong-method statuses. Only the tests run, and no server is started.

Source project

Run it

From the repository root, with this checkout's sakura on PATH:

cd compiler/examples/router_endpoint
sakura check --json
sakura test --json

Source and tests

src/main.sak includes all required imports and tests.

import std/request.{BadRequest, Get, Request, Text}
import std/response.{Response, Status}
import std/router.{Router}
import std/url.{Url}

type Greeting = { name: String where String.len(it) > 0 && String.len(it) <= 40 }

fn greet(req: Request) -> Response {
  try {
    let input: Greeting = Request.json(req, 256)
    Response.status(200, "Hello, {input.name}!")
  } catch {
    BadRequest { .. } => Response.status(400, "expected a short name"),
  }
}

// `**` matches any number of segments; the router passes them on as parameter "0".
fn docs(req: Request) -> Response {
  Response.status(200, "page {Request.param(req, "0").unwrap_or("")}")
}

fn app() -> Router<{}> { Router.new().post("/greet", greet).get("/docs/**", docs) }

test "route dispatch calls the endpoint without a server" {
  let response = Router.handle(
    app(),
    Request.post("/greet", Json.encode(Greeting { name: "Mina" })),
  )
  expect_eq(response.status, Status.from(200))
  expect_eq(response.body, Text("Hello, Mina!"))
}

test "malformed, invalid and oversized bodies return 400" {
  for body in ["not json", "\{\"name\":\"\"\}", String.repeat("x", 257)] {
    expect_eq(Router.handle(app(), Request.post("/greet", body)).status, Status.from(400))
  }
}

test "a wildcard route receives the rest of the path" {
  let response = Router.handle(app(), Request.of(Get, Url.from("/docs/tour/effects")))
  expect_eq(response.body, Text("page tour/effects"))
}

test "unknown paths and wrong methods are distinct" {
  expect_eq(Router.handle(app(), Request.post("/missing", "")).status, Status.from(404))
  expect_eq(Router.handle(app(), Request.of(Get, Url.from("/greet"))).status, Status.from(405))
}

Manifest and permissions

sakura.toml names the package and grants what the program may use.

[package]
name = "router_endpoint"
version = "0.1.0"
edition = "2026"

[permissions]

How this recipe is checked

Every project runs offline in the repository's tests (cargo test -p sakura_cli --test examples), against the compiler in the same checkout, on the JavaScript backend. A recipe that talks to an HTTP API or a model replays a cassette in its tests: a committed file of recorded requests and responses. These cassettes are synthetic, so no network access or API key is needed. Running a project needs Node 26, whose built-in SQLite the SQLite recipe uses.

To adapt a recipe, build the CLI from the same checkout and run sakura --version and sakura prompt first. Sakura Lang is pre-1.0: read STABILITY.md before depending on an API. The initial API baseline is compiler revision 5cae349516c21714d90d36e87f1cfd6debe980c6. An installed skill's frontmatter records its compiler version and build revision. These teaching examples are separate from the agent evaluation tasks.

Each page is generated from its example's recipe.md, sakura.toml, src/main.sak and fixture files. Edit those sources, not the rendered pages, then run cargo run -p agent_docs from compiler/ to regenerate the cookbook and the skill reference.

Edit on GitHub

Last updated

On this page