serverTest function

  1. @visibleForTesting
  2. @isTest
void serverTest(
  1. String description,
  2. TestCallback callback, {
  3. required RequestHandler handler,
  4. TransportMode transportMode = TransportMode.inMemory,
  5. Timeout? timeout,
  6. Object? skip,
  7. Object? tags,
  8. Map<String, dynamic>? onPlatform,
  9. int? retry,
})

Creates a test that uses an TestClient to test HTTP endpoints.

This function is similar to test but automatically sets up an TestClient with the provided handler and cleans it up after the test completes.

Parameters:

  • description: Description of the test
  • callback: Test function that receives an TestClient
  • handler: The RequestHandler implementation that will process requests
  • transportMode: The transport mode to use (in-memory or server)
  • timeout: Optional timeout for the test
  • skip: Whether to skip this test
  • tags: Tags to apply to this test
  • onPlatform: Platform-specific configuration
  • retry: Number of times to retry this test if it fails

Example:

import 'package:server_testing/server_testing.dart';

void main() {
  // Create a handler for your application
  final handler = MyApplicationHandler();

  serverTest('GET /users returns a list of users', (client) async {
    final response = await client.get('/users');

    response
        .assertStatus(200)
        .assertJson((json) {
          json.has('users');
        });
  }, handler: handler);
}

Implementation

@visibleForTesting
@isTest
void serverTest(
  String description,
  TestCallback callback, {
  required RequestHandler handler,
  TransportMode transportMode = TransportMode.inMemory,
  Timeout? timeout,
  Object? skip,
  Object? tags,
  Map<String, dynamic>? onPlatform,
  int? retry,
}) {
  test(
    description,
    () async {
      // Initialize TestClient based on transport mode
      final client = transportMode == TransportMode.inMemory
          ? TestClient.inMemory(handler)
          : TestClient.ephemeralServer(handler);

      try {
        await callback(client, handler);
      } catch (e) {
        rethrow; // Rethrow to ensure test fails properly
      } finally {
        await client.close();
      }
    },
    timeout: timeout,
    skip: skip,
    tags: tags,
    onPlatform: onPlatform,
    retry: retry,
  );
}