IoRequestHandler class

A simple RequestHandler implementation that adapts a plain dart:io request-handling callback to the server_testing ecosystem.

This is intended as the "default" handler for applications that already use HttpServer directly and therefore expose a function of the form:

FutureOr<void> onRequest(HttpRequest request) async { … }

Instead of re-writing your application to conform to RequestHandler, you can simply wrap the existing callback:

final handler = IoRequestHandler(onRequest);

serverTest('GET /ping returns pong', (client) async {
  final response = await client.get('/ping');
  response.assertStatus(200).assertBodyEquals('pong');
}, handler: handler);

The handler works in both transport modes provided by server_testing:

TransportMode.inMemory – the callback is executed directly with the mock HttpRequest. • TransportMode.ephemeralServer – an HttpServer is started on an ephemeral port and all traffic is forwarded to the callback.

Implemented types

Constructors

IoRequestHandler(FutureOr<void> onRequest(HttpRequest))
Creates a new IoRequestHandler that delegates all work to onRequest.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

close([bool force = true]) Future<void>
Releases any resources used by this handler.
override
handleRequest(HttpRequest request) Future<void>
Handles an HTTP request directly.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
startServer({int port = 0}) Future<int>
Starts a server on the given port (for server-based transports).
override
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited