http_interceptor icon indicating copy to clipboard operation
http_interceptor copied to clipboard

Unit testing interceptors/retry policies

Open rgray00 opened this issue 6 years ago • 4 comments

I'm building an http client with:

    final client = HttpClientWithInterceptor.build(
      interceptors: [
        MyApiInterceptor()
      ],
      retryPolicy: RefreshTokenRetryPolicy(),
    );

I'd like to mock a 401 via:

    test('test retry policy called twice', () async {
      when(apiClient.post('/path', body: {})).thenAnswer(
          (realInvocation) async => Response('Not Authorized', 401));

      final response = await apiClient.post('/path', body: {});

      expect(response.statusCode, 401);
      verify(tokenService.refresh()).called(2);
    });

But I'm getting an error: type 'Future<Response>' is not a subtype of type 'Future<String>' . Any examples or tips would be awesome!

rgray00 avatar Oct 08 '20 21:10 rgray00

Issue-Label Bot is automatically applying the label feature_request to this issue, with a confidence of 0.61. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

issue-label-bot[bot] avatar Oct 08 '20 21:10 issue-label-bot[bot]

Hi, I am looking further into this, since the way you are approaching the test seems to be the correct one. I will get back to you ASAP with a solution. Cheers and thanks for the issue!

CodingAleCR avatar Oct 12 '20 02:10 CodingAleCR

Dang it! I remembered why you can't do that as of right now ☹️. So, since the client is actually not implemented via interfaces we need to provide one or provide a client to be mocked. I am working hard to add testing and refactoring the bits and pieces that are affected by this so we can actually start using the package in the way it's supposed to work. I am so sorry for the inconveniences that this has brought for you.

CodingAleCR avatar Oct 12 '20 04:10 CodingAleCR

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 11 '20 05:12 stale[bot]