stubFailure method

void stubFailure(
  1. HttpMethod method,
  2. String endpoint, {
  3. Failure? failure,
  4. int statusCode = 400,
  5. Object? body,
  6. String? message,
})

Stubs a failure.

Pass failure for full control, or let one be built from statusCode and body the same way a real error response would be.

Implementation

void stubFailure(
  HttpMethod method,
  String endpoint, {
  Failure? failure,
  int statusCode = 400,
  Object? body,
  String? message,
}) {
  if (failure != null) {
    stub(method, endpoint, left(failure));
    return;
  }

  // Built exactly as a real error response is: a 401 is `unauthorized`,
  // and the body's own message wins over the generic one. It used to be
  // `badRequest` with the generic text whatever the status — so consumer
  // code branching on `ErrorSource.unauthorized` could not be tested.
  final built = failureForStatus(statusCode, body);
  stub(
    method,
    endpoint,
    left(
      message == null
          ? built
          : Failure(
              built.errorType,
              built.code,
              message,
              data: built.data,
              statusCode: built.statusCode,
            ),
    ),
  );
}