httpVendorSteps<W extends ScenarioWorld> function

List<StepDefinition<W>> httpVendorSteps<W extends ScenarioWorld>({
  1. required HttpResponseSpec? latestResponse(
    1. W world
    ),
  2. bool matchesSchema(
    1. Object? body,
    2. String schemaId
    )?,
})

Reusable HTTP vocabulary. Projects supply the response produced by their preceding logical-endpoint step; vendor steps never embed an environment URL or make a second request.

Implementation

List<StepDefinition<W>> httpVendorSteps<W extends ScenarioWorld>({
  required HttpResponseSpec? Function(W world) latestResponse,
  bool Function(Object? body, String schemaId)? matchesSchema,
}) => [
  StepDefinition(
    tier: StepTier.vendor,
    target: 'http',
    pattern: RegExp(r'^the response status is (\d+)$'),
    action: (world, _, arguments) {
      HttpAssertions.expectStatus(
        _requireResponse(latestResponse(world)),
        int.parse(arguments['1']!),
      );
    },
  ),
  StepDefinition(
    tier: StepTier.vendor,
    target: 'http',
    pattern: RegExp(r'^the response error code is "([^"]+)"$'),
    action: (world, _, arguments) {
      HttpAssertions.expectJson(
        _requireResponse(latestResponse(world)),
        '/error/code',
        arguments['1'],
      );
    },
  ),
  StepDefinition(
    tier: StepTier.vendor,
    target: 'http',
    pattern: RegExp(r'^the response body at "([^"]+)" contains "([^"]*)"$'),
    action: (world, _, arguments) {
      final value = HttpAssertions.jsonPointer(
        _requireResponse(latestResponse(world)).body,
        arguments['1']!,
      );
      if (value is! String || !value.contains(arguments['2']!)) {
        throw StateError(
          'Expected ${arguments['1']} to contain ${arguments['2']}',
        );
      }
    },
  ),
  StepDefinition(
    tier: StepTier.vendor,
    target: 'http',
    pattern: RegExp(
      r'^the response body at "([^"]+)" does not contain "([^"]*)"$',
    ),
    action: (world, _, arguments) {
      final value = HttpAssertions.jsonPointer(
        _requireResponse(latestResponse(world)).body,
        arguments['1']!,
      );
      if (value is String && value.contains(arguments['2']!)) {
        throw StateError(
          'Expected ${arguments['1']} not to contain ${arguments['2']}',
        );
      }
    },
  ),
  StepDefinition(
    tier: StepTier.vendor,
    target: 'http',
    pattern: RegExp(r'^the response conforms to schema "([^"]+)"$'),
    action: (world, _, arguments) {
      if (matchesSchema == null) {
        throw StateError(
          'No schema matcher is configured for vendor HTTP steps',
        );
      }
      if (!matchesSchema(
        _requireResponse(latestResponse(world)).body,
        arguments['1']!,
      )) {
        throw StateError(
          'Response does not conform to schema ${arguments['1']}',
        );
      }
    },
  ),
];