Skip to content

Let server return different responses #145

@AdrianDev0

Description

@AdrianDev0

I would like to test a retry mechanism. Therefore I want the server to return different responses. The first response should be a 500 and the second one a 200. I tried to do that with the following code, but its not working. The callback is only called once. Do you have an idea how to achieve this?

void main() {
  final dio = Dio(BaseOptions());
  //Adding retry interceptor here, which would execute the GET again with the same Dio after receiving the 500
  final dioAdapter = DioAdapter(dio: dio);

  const path = 'https://example.com';

  int counter = 0;

  test("retry", () async {
    dioAdapter.onGet(
      path,
      (server) {
        if (counter == 0) {
          counter += 1;
          server.reply(
            500,
            {},
          );
        } else {
          server.reply(
            200,
            {'message': 'Success!'},
            // Reply would wait for one-sec before returning data.
            delay: const Duration(seconds: 1),
          );
        }
      },
    );

    final Response response = await dio.get(path);
    expect(response.statusCode, 200);
  });
}

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions