|
| 1 | +import 'package:dio/dio.dart'; |
| 2 | +import 'package:fresh_dio/fresh_dio.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + test('does not hang when refreshToken throws (onError)', () async { |
| 7 | + final exception = Exception('any error'); |
| 8 | + final fresh = Fresh.oAuth2( |
| 9 | + tokenStorage: InMemoryTokenStorage<OAuth2Token>(), |
| 10 | + refreshToken: (_, __) async => throw exception, |
| 11 | + ); |
| 12 | + |
| 13 | + await fresh.setToken( |
| 14 | + const OAuth2Token( |
| 15 | + accessToken: 'access.token.jwt', |
| 16 | + refreshToken: 'refreshToken', |
| 17 | + ), |
| 18 | + ); |
| 19 | + |
| 20 | + final dio = Dio(); |
| 21 | + dio.interceptors.add(fresh); |
| 22 | + dio.httpClientAdapter = _MockAdapter( |
| 23 | + (options) { |
| 24 | + return ResponseBody.fromString( |
| 25 | + '{"error": "Unauthorized"}', |
| 26 | + 401, |
| 27 | + headers: { |
| 28 | + Headers.contentTypeHeader: [Headers.jsonContentType], |
| 29 | + }, |
| 30 | + ); |
| 31 | + }, |
| 32 | + ); |
| 33 | + |
| 34 | + final response = await dio.get<Object?>('http://example.com'); |
| 35 | + expect(response.statusCode, equals(401)); |
| 36 | + expect( |
| 37 | + response.extra['fresh'], |
| 38 | + isA<Map<String, dynamic>>() |
| 39 | + .having((m) => m['message'], 'message', equals('refresh failure')) |
| 40 | + .having((m) => m['error'], 'error', equals(exception)) |
| 41 | + .having((m) => m['stack_trace'], 'stack trace', isA<StackTrace>()), |
| 42 | + ); |
| 43 | + }); |
| 44 | + |
| 45 | + test('does not hang when refreshToken throws (onResponse)', () async { |
| 46 | + final exception = Exception('any error'); |
| 47 | + final fresh = Fresh.oAuth2( |
| 48 | + tokenStorage: InMemoryTokenStorage<OAuth2Token>(), |
| 49 | + refreshToken: (_, __) async => throw exception, |
| 50 | + ); |
| 51 | + |
| 52 | + await fresh.setToken( |
| 53 | + const OAuth2Token( |
| 54 | + accessToken: 'access.token.jwt', |
| 55 | + refreshToken: 'refreshToken', |
| 56 | + ), |
| 57 | + ); |
| 58 | + |
| 59 | + final dio = Dio(); |
| 60 | + dio.interceptors.add(fresh); |
| 61 | + dio.options.validateStatus = (_) => true; |
| 62 | + dio.httpClientAdapter = _MockAdapter( |
| 63 | + (options) { |
| 64 | + return ResponseBody.fromString( |
| 65 | + '{"error": "Unauthorized"}', |
| 66 | + 401, |
| 67 | + headers: { |
| 68 | + Headers.contentTypeHeader: [Headers.jsonContentType], |
| 69 | + }, |
| 70 | + ); |
| 71 | + }, |
| 72 | + ); |
| 73 | + |
| 74 | + final response = await dio.get<Object?>('http://example.com'); |
| 75 | + expect(response.statusCode, equals(401)); |
| 76 | + expect( |
| 77 | + response.extra['fresh'], |
| 78 | + isA<Map<String, dynamic>>() |
| 79 | + .having((m) => m['message'], 'message', equals('refresh failure')) |
| 80 | + .having((m) => m['error'], 'error', equals(exception)) |
| 81 | + .having((m) => m['stack_trace'], 'stack trace', isA<StackTrace>()), |
| 82 | + ); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | +class _MockAdapter implements HttpClientAdapter { |
| 87 | + _MockAdapter(this.onFetch); |
| 88 | + |
| 89 | + final ResponseBody Function(RequestOptions options) onFetch; |
| 90 | + |
| 91 | + @override |
| 92 | + void close({bool force = false}) {} |
| 93 | + |
| 94 | + @override |
| 95 | + Future<ResponseBody> fetch( |
| 96 | + RequestOptions options, |
| 97 | + Stream<List<int>>? requestStream, |
| 98 | + Future<void>? cancelFuture, |
| 99 | + ) async { |
| 100 | + return onFetch(options); |
| 101 | + } |
| 102 | +} |
0 commit comments