From 3e3c247b8bc764136d7613517acac1b05c8519f1 Mon Sep 17 00:00:00 2001 From: LukasMirbt Date: Tue, 7 May 2024 00:44:23 +0200 Subject: [PATCH 1/3] fix bug --- packages/fresh_dio/lib/src/fresh.dart | 8 +++++++ packages/fresh_dio/test/fresh_test.dart | 29 +++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/packages/fresh_dio/lib/src/fresh.dart b/packages/fresh_dio/lib/src/fresh.dart index b6285f2..e7909dd 100644 --- a/packages/fresh_dio/lib/src/fresh.dart +++ b/packages/fresh_dio/lib/src/fresh.dart @@ -80,6 +80,14 @@ class Fresh extends QueuedInterceptor with FreshMixin { RequestOptions options, RequestInterceptorHandler handler, ) async { + assert( + _httpClient.interceptors.every( + (interceptor) => interceptor is! Fresh, + ), + 'A Fresh interceptor must not be added to httpClient. ' + 'This will cause an infinite loop on token refresh.', + ); + final currentToken = await token; final headers = currentToken != null ? _tokenHeader(currentToken) diff --git a/packages/fresh_dio/test/fresh_test.dart b/packages/fresh_dio/test/fresh_test.dart index ee336fd..2c449c0 100644 --- a/packages/fresh_dio/test/fresh_test.dart +++ b/packages/fresh_dio/test/fresh_test.dart @@ -112,6 +112,35 @@ void main() { group('onRequest', () { const oAuth2Token = OAuth2Token(accessToken: 'accessToken'); + + test( + 'throws AssertionError when httpClient.interceptors ' + 'contains a Fresh interceptor', + () { + when(() => tokenStorage.read()).thenAnswer((_) async => oAuth2Token); + + final httpClient = Dio(); + + final fresh = Fresh.oAuth2( + tokenStorage: tokenStorage, + refreshToken: emptyRefreshToken, + httpClient: httpClient, + ); + + httpClient.interceptors.add(fresh); + + expect( + fresh.onRequest( + RequestOptions(), + RequestInterceptorHandler(), + ), + throwsA( + isA(), + ), + ); + }, + ); + test( 'appends token header when token is OAuth2Token ' 'and tokenHeader is not provided', () async { From 94e546e58cfd81deb8f1e38369c9d548b8e0030d Mon Sep 17 00:00:00 2001 From: LukasMirbt Date: Tue, 7 May 2024 00:56:06 +0200 Subject: [PATCH 2/3] check for this instead of type --- packages/fresh_dio/lib/src/fresh.dart | 4 ++-- packages/fresh_dio/test/fresh_test.dart | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/fresh_dio/lib/src/fresh.dart b/packages/fresh_dio/lib/src/fresh.dart index e7909dd..5b722e2 100644 --- a/packages/fresh_dio/lib/src/fresh.dart +++ b/packages/fresh_dio/lib/src/fresh.dart @@ -82,9 +82,9 @@ class Fresh extends QueuedInterceptor with FreshMixin { ) async { assert( _httpClient.interceptors.every( - (interceptor) => interceptor is! Fresh, + (interceptor) => interceptor != this, ), - 'A Fresh interceptor must not be added to httpClient. ' + 'The Fresh instance must not be added to httpClient as an interceptor. ' 'This will cause an infinite loop on token refresh.', ); diff --git a/packages/fresh_dio/test/fresh_test.dart b/packages/fresh_dio/test/fresh_test.dart index 2c449c0..f4b8eb5 100644 --- a/packages/fresh_dio/test/fresh_test.dart +++ b/packages/fresh_dio/test/fresh_test.dart @@ -115,7 +115,7 @@ void main() { test( 'throws AssertionError when httpClient.interceptors ' - 'contains a Fresh interceptor', + 'contains the Fresh instance as an interceptor', () { when(() => tokenStorage.read()).thenAnswer((_) async => oAuth2Token); From 984fabfabf007a0b65720b04be1e18fcf5af9fa2 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Sat, 20 Jul 2024 14:48:14 -0500 Subject: [PATCH 3/3] chore: adjust message --- packages/fresh_dio/lib/src/fresh.dart | 25 ++++++++++++++++++++----- packages/fresh_dio/test/fresh_test.dart | 9 ++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/fresh_dio/lib/src/fresh.dart b/packages/fresh_dio/lib/src/fresh.dart index 5b722e2..832c62f 100644 --- a/packages/fresh_dio/lib/src/fresh.dart +++ b/packages/fresh_dio/lib/src/fresh.dart @@ -81,11 +81,26 @@ class Fresh extends QueuedInterceptor with FreshMixin { RequestInterceptorHandler handler, ) async { assert( - _httpClient.interceptors.every( - (interceptor) => interceptor != this, - ), - 'The Fresh instance must not be added to httpClient as an interceptor. ' - 'This will cause an infinite loop on token refresh.', + _httpClient.interceptors.every((interceptor) => interceptor != this), + ''' +Cycle Detected! + +The Fresh instance was created using an http client +which already contains the Fresh instance as an interceptor. + +This will cause an infinite loop on token refresh. + +Example: + + ``` + final httpClient = Dio(); + final fresh = Fresh.oAuth2( + httpClient: httpClient, + ... + ); + httpClient.interceptors.add(fresh); // <-- BAD + ``` +''', ); final currentToken = await token; diff --git a/packages/fresh_dio/test/fresh_test.dart b/packages/fresh_dio/test/fresh_test.dart index f4b8eb5..e9b36af 100644 --- a/packages/fresh_dio/test/fresh_test.dart +++ b/packages/fresh_dio/test/fresh_test.dart @@ -130,13 +130,8 @@ void main() { httpClient.interceptors.add(fresh); expect( - fresh.onRequest( - RequestOptions(), - RequestInterceptorHandler(), - ), - throwsA( - isA(), - ), + fresh.onRequest(RequestOptions(), RequestInterceptorHandler()), + throwsA(isA()), ); }, );