SparkyTestClient class final
In-memory test client for Sparky servers.
Boots a real Sparky server on an OS-assigned port (so no port conflicts) and provides a clean API for making requests in tests.
late SparkyTestClient client;
setUp(() async {
client = await SparkyTestClient.boot(
routes: [
RouteHttp.get('/hello',
middleware: (r) async => const Response.ok(body: 'hi')),
],
);
});
tearDown(() => client.close());
test('GET /hello returns 200', () async {
final res = await client.get('/hello');
expect(res.statusCode, 200);
});
Constructors
- SparkyTestClient.from(Sparky server)
-
Wraps an already-running Sparky instance in a test client.
factory
Properties
- baseUrl → String
-
The base URL of the test server (e.g.
http://localhost:12345).no setter - hashCode → int
-
The hash code for this object.
no setterinherited
- port → int
-
The port the test server is listening on.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
) → Future< void> - Shuts down the test server and closes the HTTP client.
-
delete(
String path, {Object? body, Map< String, String> ? headers, ContentType? contentType}) → Future<TestResponse> -
Sends a DELETE request to the given
path. -
get(
String path, {Map< String, String> ? headers}) → Future<TestResponse> -
Sends a GET request to the given
path. -
head(
String path, {Map< String, String> ? headers}) → Future<TestResponse> -
Sends a HEAD request to the given
path. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
patch(
String path, {Object? body, Map< String, String> ? headers, ContentType? contentType}) → Future<TestResponse> -
Sends a PATCH request to the given
path. -
post(
String path, {Object? body, Map< String, String> ? headers, ContentType? contentType}) → Future<TestResponse> -
Sends a POST request to the given
path. -
put(
String path, {Object? body, Map< String, String> ? headers, ContentType? contentType}) → Future<TestResponse> -
Sends a PUT request to the given
path. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
boot(
{required List< Route> routes, OpenApiConfig? openApi, MetricsConfig? metrics, HealthCheckConfig? health, SchedulerConfig? scheduler, Route? routeNotFound, Pipeline? pipelineBefore, Pipeline? pipelineAfter, int? maxBodySize, Duration? requestTimeout, bool enableGzip = false, int gzipMinLength = 0, Duration? cacheTtl, int? cacheMaxEntries}) → Future<SparkyTestClient> - Boots a Sparky server on a random port and returns a test client.