testing library
In-memory test client: drive a real ServerRunner (real dispatch,
middleware, error mapping) with no sockets and no native library.
import 'package:nitro_server/testing.dart';
final client = await NitroTestClient.start();
await client.server.get('/users/:id', (request) async {
return ResponseContext.text('user ${request.param('id')}');
});
final response = await client.get('/users/42');
expect(response.status, 200);
expect(response.text(), 'user 42');
await client.close();
Faithful to production where it matters (routing precedence, middleware order, handler/error answers) and honestly divergent where no engine exists:
- No sockets: no keep-alive, no
maxRequestsPerConnection, no ports.server.portstays0. - No route timeouts: the deadline wait lives in the C++ connection thread, so slow handlers simply resolve late here instead of 408ing.
maxBodyBytesis unenforced: bodies arrive whole, never 413.- Unrouted paths answer
404 "not found"directly, exactly like the engine — the runner'snotFoundHandlerdoes not run for them in production either (the engine answers before dispatch).
Classes
- NitroTestClient
- Drives server without sockets. Create with start, register routes on server exactly as in production, then issue requests.
- NitroTestResponse
- One answered test request.
- TestWsConnection
- The test side of an in-memory WebSocket session (see NitroTestClient.ws). Messages from the route handler arrive on messages; sendText and sendBytes deliver to the handler; close ends the session like a peer close; closedCode completes with the handler side's close code.