CodeSampleX

Beispiel

shelf 1.4.2: Show how Router.mount handles an exact prefix, a trailing slash, and nested routes

Verifiziertes Beispiel für pub shelf 1.4.2: Show how Router.mount handles an exact prefix, a trailing slash, and nested routes. Der Vertrag lief auf dart 3 …

sha256:6fe02156c65bb35677dd2ce10c035be187650275bc57c1dbfb9d6ef58449ba79

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
2
Signaturschlüssel, die es gebaut haben
2
Deklarierte Umgebung dart linux x64 dart dart pub

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
dart 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · pub@1
2026-08-17
dart 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · pub@1
2026-08-18

Fall

HOW
Ziel
Show how Router.mount handles an exact prefix, a trailing slash, and nested routes
Pakete
Symbole
  • Request
  • Response
  • Router.mount
  • Router.get
Umgebung
dart
Erstellt
2026-08-17T03:53:45Z

Contract

  1. A GET request to /api after app.mount('/api', subRouter.call) reaches the mounted sub-router root.
  2. A GET request to /api/user/alex reaches the mounted parameter route with name equal to alex.
  3. A GET request to /api/ reaches the same sub-router root route as /api.

Dateien

  • NOTES.md
  • csx.json
  • pubspec.lock
  • pubspec.yaml
  • test/contract.dart

Quellartefakt herunterladen (tar.gz)

Quelltext

NOTES.md
`search_known_solution` returned sample `sha256:2651162215727e626baac2f69d7b7e043707968fea0f9218aa9dda0648549e95`, which proves mount-related route matching and `routeNotFound` semantics.
A wrong mental model is that `mount('/api', ...)` only accepts `/api/...` and 404s on plain `/api`; that expectation fails loudly in this sample with a 200 response from the mounted root route.
csx.json
{"case":{"believed":"Mounting with `'/api'` only handles `/api/...` and should not match a request to `/api` itself.","caseId":"case:sha256:f0165e8fa7c155c98005250b4ceab9896a3614c38e6b269e4bdb54879ae31a72","contract":["A GET request to /api after app.mount('/api', subRouter.call) reaches the mounted sub-router root.","A GET request to /api/user/alex reaches the mounted parameter route with name equal to alex.","A GET request to /api/ reaches the same sub-router root route as /api."],"goal":"Show how Router.mount handles an exact prefix, a trailing slash, and nested routes","kind":"HOW","packages":["pkg:pub/shelf@1.4.2","pkg:pub/shelf_router@1.1.4"],"schemaVersion":1,"symbols":["Request","Response","Router.mount","Router.get"]},"contractCommand":["dart","test/contract.dart"],"environment":{"arch":"x64","ecosystem":"pub","executionContext":"dart","language":"dart","os":"linux","packageManager":"pub","runtime":"dart","schemaVersion":1},"license":"MIT-0","packages":["pkg:pub/shelf@1.4.2","pkg:pub/shelf_router@1.1.4"],"schemaVersion":1,"symbols":["Request","Response","Router.mount","Router.get"],"verifierAdapter":"pub@1"}
pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
  async:
    dependency: transitive
    description:
      name: async
      sha256: e2eb0491ba5ddb6177742d2da23904574082139b07c1e33b8503b9f46f3e1a37
      url: "https://pub.dev"
    source: hosted
    version: "2.13.1"
  collection:
    dependency: transitive
    description:
      name: collection
      sha256: "2f5709ae4d3d59dd8f7cd309b4e023046b57d8a6c82130785d2b0e5868084e76"
      url: "https://pub.dev"
    source: hosted
    version: "1.19.1"
  http_methods:
    dependency: transitive
    description:
      name: http_methods
      sha256: "6bccce8f1ec7b5d701e7921dca35e202d425b57e317ba1a37f2638590e29e566"
      url: "https://pub.dev"
    source: hosted
    version: "1.1.1"
  http_parser:
    dependency: transitive
    description:
      name: http_parser
      sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
      url: "https://pub.dev"
    source: hosted
    version: "4.1.2"
  meta:
    dependency: transitive
    description:
      name: meta
      sha256: "307249ce4ff29d58a18e97f6345f539382eb9c9c29ecda628900f31de0443dd9"
      url: "https://pub.dev"
    source: hosted
    version: "1.19.0"
  path:
    dependency: transitive
    description:
      name: path
      sha256: "75cca69d1490965be98c73ceaea117e8a04dd21217b37b292c9ddbec0d955bc5"
      url: "https://pub.dev"
    source: hosted
    version: "1.9.1"
  shelf:
    dependency: "direct main"
    description:
      name: shelf
      sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12
      url: "https://pub.dev"
    source: hosted
    version: "1.4.2"
  shelf_router:
    dependency: "direct main"
    description:
      name: shelf_router
      sha256: f5e5d492440a7fb165fe1e2e1a623f31f734d3370900070b2b1e0d0428d59864
      url: "https://pub.dev"
    source: hosted
    version: "1.1.4"
  source_span:
    dependency: transitive
    description:
      name: source_span
      sha256: "56a02f1f4cd1a2d96303c0144c93bd6d909eea6bee6bf5a0e0b685edbd4c47ab"
      url: "https://pub.dev"
    source: hosted
    version: "1.10.2"
  stack_trace:
    dependency: transitive
    description:
      name: stack_trace
      sha256: "8b27215b45d22309b5cddda1aa2b19bdfec9df0e765f2de506401c071d38d1b1"
      url: "https://pub.dev"
    source: hosted
    version: "1.12.1"
  stream_channel:
    dependency: transitive
    description:
      name: stream_channel
      sha256: "969e04c80b8bcdf826f8f16579c7b14d780458bd97f56d107d3950fdbeef059d"
      url: "https://pub.dev"
    source: hosted
    version: "2.1.4"
  string_scanner:
    dependency: transitive
    description:
      name: string_scanner
      sha256: "921cd31725b72fe181906c6a94d987c78e3b98c2e205b397ea399d4054872b43"
      url: "https://pub.dev"
    source: hosted
    version: "1.4.1"
  term_glyph:
    dependency: transitive
    description:
      name: term_glyph
      sha256: "7f554798625ea768a7518313e58f83891c7f5024f88e46e7182a4558850a4b8e"
      url: "https://pub.dev"
    source: hosted
    version: "1.2.2"
  typed_data:
    dependency: transitive
    description:
      name: typed_data
      sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006
      url: "https://pub.dev"
    source: hosted
    version: "1.4.0"
sdks:
  dart: ">=3.5.0 <4.0.0"
pubspec.yaml
name: pub_shelf_router_api
publish_to: none
environment:
  sdk: '>=3.0.0 <4.0.0'

dependencies:
  shelf: 1.4.2
  shelf_router: 1.1.4
test/contract.dart
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

Future<Response> dispatch(Router app, String path) async {
  final request = Request('GET', Uri.parse('http://localhost$path'));
  return app.call(request);
}

void main() async {
  final subRouter = Router()
    ..get('/', (Request request) => Response.ok('api-root'))
    ..get('/user/<name>', (Request request, String name) => Response.ok('user-$name'));

  final app = Router()
    ..mount('/api', subRouter.call);

  final exact = await dispatch(app, '/api');
  final exactBody = await exact.readAsString();
  if (exactBody != 'api-root') {
    throw StateError('Expected /api to reach mounted root, got ${exact.statusCode} $exactBody');
  }

  final trailing = await dispatch(app, '/api/');
  final trailingBody = await trailing.readAsString();
  if (trailingBody != 'api-root') {
    throw StateError('Expected /api/ to reach mounted root, got ${trailing.statusCode} $trailingBody');
  }

  final nested = await dispatch(app, '/api/user/alex');
  final nestedBody = await nested.readAsString();
  if (nestedBody != 'user-alex') {
    throw StateError('Expected nested route to capture alex, got ${nested.statusCode} $nestedBody');
  }
}

Ursprungs-Seeder

csx-seed