CodingAleCR / http_interceptor

A lightweight, simple plugin that allows you to intercept request and response objects and modify them if desired.
MIT License
134 stars 67 forks source link

Unit testing interceptors/retry policies #45

Closed robgravy closed 3 years ago

robgravy commented 3 years ago

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!

issue-label-bot[bot] commented 3 years ago

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.

CodingAleCR commented 3 years ago

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 commented 3 years ago

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.

stale[bot] commented 3 years ago

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.