CodeSampleX

Sample

axum 0.8.8: axum::routing::post

Verified sample for cargo axum 0.8.8: axum::routing::post. The contract ran on rust 1 · linux alpine/x64 · docker and passed: axum::routing::post dispatches…

sha256:20b90aa7d3db0516e7b40dc401a72a512a53e6cfc577e8f73dc9e7c32be3bee3

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux · alpine · musl x64 cargo

Verification-run environments

Environment Contract Stages Run
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-11-17

Case

HOW
Goal
verify axum::routing::post in pkg:cargo/axum@0.8.8
Packages
Symbols
  • axum::routing::post
Created
2026-11-17T04:22:18Z

Contract

  1. axum::routing::post dispatches POST requests to an async handler and extracts String request bodies
  2. A POST-only MethodRouter rejects non-POST methods (such as GET) with 405 Method Not Allowed and sets Allow: POST
  3. MethodRouter returned by post chains additional method handlers via .get() and binds shared State<T> when generic over state S and finalized with Router::with_state
  4. post handlers extract Path<u32> on axum 0.8 brace-syntax routes (/batches/{id}) and Json<Vec<String>> payloads, returning 415 Unsupported Media Type when Content-Type: application/json is omitted
  5. MethodRouter::fallback customizes the rejection response for non-POST HTTP methods on a post route
  6. Chaining .post() onto an existing post() MethodRouter panics with Overlapping method route

Files

  • Cargo.lock
  • Cargo.toml
  • PROMPT.md
  • csx.json
  • spec.json
  • src/lib.rs
  • test/contract.rs

Download the source artifact (tar.gz)

Source

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "atomic-waker"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"

[[package]]
name = "axum"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b52af3cb4058c895d37317bb27508dccc8e5f2d39454016b297bf4a400597b8"
dependencies = [
 "axum-core",
 "bytes",
 "form_urlencoded",
 "futures-util",
 "http",
 "http-body",
 "http-body-util",
 "hyper",
 "hyper-util",
 "itoa",
 "matchit",
 "memchr",
 "mime",
 "percent-encoding",
 "pin-project-lite",
 "serde_core",
 "serde_json",
 "serde_path_to_error",
 "serde_urlencoded",
 "sync_wrapper",
 "tokio",
 "tower",
 "tower-layer",
 "tower-service",
 "tracing",
]

[[package]]
name = "axum-core"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1"
dependencies = [
 "bytes",
 "futures-core",
 "http",
 "http-body",
 "http-body-util",
 "mime",
 "pin-project-lite",
 "sync_wrapper",
 "tower-layer",
 "tower-service",
 "tracing",
]

[[package]]
name = "bytes"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"

[[package]]
name = "form_urlencoded"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
dependencies = [
 "percent-encoding",
]

[[package]]
name = "futures-core"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e"

[[package]]
name = "futures-task"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd"

[[package]]
name = "futures-util"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc"
dependencies = [
 "futures-core",
 "futures-task",
 "pin-project-lite",
 "slab",
]

[[package]]
name = "http"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
dependencies = [
 "bytes",
 "itoa",
]

[[package]]
name = "http-body"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c"
dependencies = [
 "bytes",
 "http",
]

[[package]]
name = "http-body-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c"
dependencies = [
 "bytes",
 "futures-core",
 "http",
 "http-body",
 "pin-project-lite",
]

[[package]]
name = "httparse"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"

[[package]]
name = "httpdate"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"

[[package]]
name = "hyper"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c"
dependencies = [
 "atomic-waker",
 "bytes",
 "futures-core",
 "http",
 "http-body",
 "httparse",
 "httpdate",
 "itoa",
 "pin-project-lite",
 "smallvec",
 "tokio",
]

[[package]]
name = "hyper-util"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff"
dependencies = [
 "bytes",
 "http",
 "http-body",
 "hyper",
 "pin-project-lite",
 "tokio",
 "tower-service",
]

[[package]]
name = "itoa"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4"

[[package]]
name = "libc"
version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"

[[package]]
name = "log"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"

[[package]]
name = "matchit"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"

[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"

[[package]]
name = "mime"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5848f5a9e8117cf94f2f90faac24a87ff81600ff8628c1e3deaab572e76bfa1"

[[package]]
name = "mio"
version = "1.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20521a903f02b0a573cd6840e9d0a5a0cc043d361fe924092cd70f8556cc9c4a"
dependencies = [
 "libc",
 "wasi",
 "windows-sys",
]

[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"

[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"

[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "ryu"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f"

[[package]]
name = "sample-axum-routing-post"
version = "1.0.0"
dependencies = [
 "axum",
]

[[package]]
name = "serde"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
dependencies = [
 "serde_core",
]

[[package]]
name = "serde_core"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "serde_json"
version = "1.0.154"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
dependencies = [
 "itoa",
 "memchr",
 "serde",
 "serde_core",
 "zmij",
]

[[package]]
name = "serde_path_to_error"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457"
dependencies = [
 "itoa",
 "serde",
 "serde_core",
]

[[package]]
name = "serde_urlencoded"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
dependencies = [
 "form_urlencoded",
 "itoa",
 "ryu",
 "serde",
]

[[package]]
name = "slab"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"

[[package]]
name = "smallvec"
version = "1.16.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "149309498a5f7334b12ddae0cad8aba81ce785fe91755068867e017cf98fee46"

[[package]]
name = "socket2"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
dependencies = [
 "libc",
 "windows-sys",
]

[[package]]
name = "syn"
version = "3.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "sync_wrapper"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"

[[package]]
name = "tokio"
version = "1.54.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3"
dependencies = [
 "libc",
 "mio",
 "pin-project-lite",
 "socket2",
 "tokio-macros",
 "windows-sys",
]

[[package]]
name = "tokio-macros"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "tower"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4"
dependencies = [
 "futures-core",
 "futures-util",
 "pin-project-lite",
 "sync_wrapper",
 "tokio",
 "tower-layer",
 "tower-service",
 "tracing",
]

[[package]]
name = "tower-layer"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"

[[package]]
name = "tower-service"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"

[[package]]
name = "tracing"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
 "log",
 "pin-project-lite",
 "tracing-core",
]

[[package]]
name = "tracing-core"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
 "once_cell",
]

[[package]]
name = "unicode-ident"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"

[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
 "windows-link",
]

[[package]]
name = "zmij"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
Cargo.toml
[package]
name = "sample-axum-routing-post"
version = "1.0.0"
edition = "2021"
publish = false

[dependencies]
axum = "=0.8.8"

[[bin]]
name = "contract"
path = "test/contract.rs"
PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify axum::routing::post in pkg:cargo/axum@0.8.8
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:cargo/axum@0.8.8
Demonstrate these symbols/APIs:
  - axum::routing::post

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:2a1788a89925b5310d2069bebad2f3806588e32897c2ad28e5c0ddb1f4cfd803","contract":["axum::routing::post dispatches POST requests to an async handler and extracts String request bodies","A POST-only MethodRouter rejects non-POST methods (such as GET) with 405 Method Not Allowed and sets Allow: POST","MethodRouter returned by post chains additional method handlers via .get() and binds shared State\u003cT\u003e when generic over state S and finalized with Router::with_state","post handlers extract Path\u003cu32\u003e on axum 0.8 brace-syntax routes (/batches/{id}) and Json\u003cVec\u003cString\u003e\u003e payloads, returning 415 Unsupported Media Type when Content-Type: application/json is omitted","MethodRouter::fallback customizes the rejection response for non-POST HTTP methods on a post route","Chaining .post() onto an existing post() MethodRouter panics with Overlapping method route"],"goal":"verify axum::routing::post in pkg:cargo/axum@0.8.8","kind":"HOW","packages":["pkg:cargo/axum@0.8.8"],"schemaVersion":1,"symbols":["axum::routing::post"]},"contractCommand":["cargo","run","--offline"],"environment":{"arch":"x64","distro":"alpine","ecosystem":"cargo","libc":"musl","os":"linux","packageManager":"cargo","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/axum@0.8.8"],"schemaVersion":1,"subject":"pkg:cargo/axum@0.8.8","symbols":["axum::routing::post"],"verifierAdapter":"cargo@1"}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify axum::routing::post in pkg:cargo/axum@0.8.8",
  "kind": "HOW",
  "packages": [
    "pkg:cargo/axum@0.8.8"
  ],
  "symbols": [
    "axum::routing::post"
  ]
}
src/lib.rs
//! Clean-room verification sample for axum::routing::post.

use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json, Router,
    routing::{MethodRouter, post},
};

pub fn items_post_router<S>() -> MethodRouter<S>
where
    S: Clone + Send + Sync + 'static,
{
    post(|body: String| async move { (StatusCode::CREATED, format!("created:{body}")) })
}

pub fn build_app(prefix: String) -> Router {
    let mixed_route: MethodRouter<String> = post(
        |State(prefix): State<String>, body: String| async move {
            (StatusCode::OK, format!("{prefix}:{body}"))
        },
    )
    .get(|| async { (StatusCode::OK, "list") });

    let json_route: MethodRouter<String> = post(
        |State(prefix): State<String>,
         Path(id): Path<u32>,
         Json(payload): Json< Vec<String> >| async move {
            (
                StatusCode::ACCEPTED,
                Json(format!("{prefix}:{id}:{}", payload.join(","))),
            )
        },
    );

    let custom_fallback_route: MethodRouter<String> = post(|| async { (StatusCode::OK, "posted") })
        .fallback(|| async { (StatusCode::METHOD_NOT_ALLOWED, "post-only") });

    Router::new()
        .route("/items", items_post_router())
        .route("/prefixed", mixed_route)
        .route("/batches/{id}", json_route)
        .route("/strict", custom_fallback_route)
        .with_state(prefix)
}
test/contract.rs
use axum::{
    ServiceExt,
    body::{Body, to_bytes},
    http::{Method, Request, StatusCode, header},
    response::Response,
    routing::{MethodRouter, post},
};
use sample_axum_routing_post::build_app;
use std::{
    convert::Infallible,
    future::Future,
    panic::{AssertUnwindSafe, catch_unwind},
    pin::pin,
    task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

fn block_on<F: Future>(fut: F) -> F::Output {
    fn noop_clone(_: *const ()) -> RawWaker {
        RawWaker::new(std::ptr::null(), &VTABLE)
    }
    fn noop(_: *const ()) {}
    static VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop);

    let waker = unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTABLE)) };
    let mut cx = Context::from_waker(&waker);
    let mut fut = pin!(fut);
    loop {
        if let Poll::Ready(val) = fut.as_mut().poll(&mut cx) {
            return val;
        }
        std::thread::yield_now();
    }
}

fn dispatch<S>(svc: &mut S, req: Request<Body>) -> Response
where
    S: ServiceExt<Request<Body>, Response = Response, Error = Infallible>,
{
    block_on(svc.call(req)).expect("infallible response")
}

fn main() {
    let app = build_app("tag".to_string());
    let mut svc = app.into_service::<Body>();

    // 1. axum::routing::post dispatches POST requests to the async handler and extracts String request bodies
    let req = Request::builder()
        .method(Method::POST)
        .uri("/items")
        .body(Body::from("widget"))
        .expect("valid POST request");
    let res = dispatch(&mut svc, req);
    assert_eq!(res.status(), StatusCode::CREATED);
    let body = block_on(to_bytes(res.into_body(), 1024)).expect("body bytes");
    assert_eq!(&body[..], b"created:widget");

    // 2. POST-only MethodRouter rejects non-POST methods with 405 Method Not Allowed and sets Allow: POST
    let get_req = Request::builder()
        .method(Method::GET)
        .uri("/items")
        .body(Body::empty())
        .expect("valid GET request");
    let get_res = dispatch(&mut svc, get_req);
    assert_eq!(get_res.status(), StatusCode::METHOD_NOT_ALLOWED);
    let allow = get_res
        .headers()
        .get(header::ALLOW)
        .expect("Allow header on 405")
        .to_str()
        .expect("ASCII Allow header");
    assert_eq!(allow, "POST");

    // 3. MethodRouter returned by post chains additional method handlers (.get) and binds shared State via Router::with_state
    let post_prefixed = Request::builder()
        .method(Method::POST)
        .uri("/prefixed")
        .body(Body::from("alpha"))
        .expect("valid POST /prefixed");
    let post_prefixed_res = dispatch(&mut svc, post_prefixed);
    assert_eq!(post_prefixed_res.status(), StatusCode::OK);
    let post_prefixed_body =
        block_on(to_bytes(post_prefixed_res.into_body(), 1024)).expect("body bytes");
    assert_eq!(&post_prefixed_body[..], b"tag:alpha");

    let get_prefixed = Request::builder()
        .method(Method::GET)
        .uri("/prefixed")
        .body(Body::empty())
        .expect("valid GET /prefixed");
    let get_prefixed_res = dispatch(&mut svc, get_prefixed);
    assert_eq!(get_prefixed_res.status(), StatusCode::OK);
    let get_prefixed_body =
        block_on(to_bytes(get_prefixed_res.into_body(), 1024)).expect("body bytes");
    assert_eq!(&get_prefixed_body[..], b"list");

    let del_prefixed = Request::builder()
        .method(Method::DELETE)
        .uri("/prefixed")
        .body(Body::empty())
        .expect("valid DELETE /prefixed");
    let del_prefixed_res = dispatch(&mut svc, del_prefixed);
    assert_eq!(del_prefixed_res.status(), StatusCode::METHOD_NOT_ALLOWED);
    let allow_mixed = del_prefixed_res
        .headers()
        .get(header::ALLOW)
        .expect("Allow header on mixed route 405")
        .to_str()
        .expect("ASCII Allow header");
    assert!(allow_mixed.contains("POST") && allow_mixed.contains("GET"));

    // 4. post handler extracts Path<u32> on axum 0.8 brace-syntax routes ("/batches/{id}") and Json<Vec<String>> payloads, returning 415 without application/json Content-Type
    let json_req = Request::builder()
        .method(Method::POST)
        .uri("/batches/42")
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(r#"["one","two"]"#))
        .expect("valid JSON POST");
    let json_res = dispatch(&mut svc, json_req);
    assert_eq!(json_res.status(), StatusCode::ACCEPTED);
    let json_body = block_on(to_bytes(json_res.into_body(), 1024)).expect("body bytes");
    assert_eq!(&json_body[..], br#""tag:42:one,two""#);

    let missing_ct_req = Request::builder()
        .method(Method::POST)
        .uri("/batches/42")
        .body(Body::from(r#"["one","two"]"#))
        .expect("POST without Content-Type");
    let missing_ct_res = dispatch(&mut svc, missing_ct_req);
    assert_eq!(missing_ct_res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);

    // 5. MethodRouter::fallback customizes non-POST rejection responses on a post route
    let strict_put = Request::builder()
        .method(Method::PUT)
        .uri("/strict")
        .body(Body::empty())
        .expect("valid PUT /strict");
    let strict_res = dispatch(&mut svc, strict_put);
    assert_eq!(strict_res.status(), StatusCode::METHOD_NOT_ALLOWED);
    let strict_body = block_on(to_bytes(strict_res.into_body(), 1024)).expect("body bytes");
    assert_eq!(&strict_body[..], b"post-only");

    // 6. Chaining .post() onto an existing post() MethodRouter panics due to overlapping method registration
    let prev_hook = std::panic::take_hook();
    std::panic::set_hook(Box::new(|_| {}));
    let duplicate_post = catch_unwind(AssertUnwindSafe(|| {
        let _: MethodRouter = post(|| async { "first" }).post(|| async { "second" });
    }));
    std::panic::set_hook(prev_hook);
    let panic_payload = duplicate_post.expect_err("duplicate post must panic");
    let panic_msg = panic_payload
        .downcast_ref::<String>()
        .map(String::as_str)
        .or_else(|| panic_payload.downcast_ref::<&str>().copied())
        .unwrap_or("");
    assert!(panic_msg.contains("Overlapping method route"));

    println!("All axum::routing::post contract assertions passed.");
}

Origin Seeder

anonymous