Skip to content

Commit

Permalink
Update Dio to 5.2.0 (#150)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Revial <[email protected]>
  • Loading branch information
orevial and orevial authored Aug 17, 2023
1 parent 8d133e8 commit f999fbf
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
sdk: [ 2.15.0, stable ]
sdk: [ 2.18.3, stable ]

steps:
- name: Checkout the repository
Expand Down
4 changes: 2 additions & 2 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void main() async {
signInRoute,
(server) => server.throws(
401,
DioError(
DioException(
requestOptions: RequestOptions(
path: signInRoute,
),
Expand All @@ -99,7 +99,7 @@ void main() async {
// Throws without user credentials.
expect(
() async => await dio.post(signInRoute),
throwsA(isA<DioError>()),
throwsA(isA<DioException>()),
);

// Returns an access token if user credentials are provided.
Expand Down
2 changes: 1 addition & 1 deletion lib/http_mock_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export 'src/adapters/dio_adapter.dart';
export 'src/exceptions.dart' show MockDioError;
export 'src/exceptions.dart' show MockDioException;
export 'src/extensions/matches_request.dart';
export 'src/interceptors/dio_interceptor.dart';
export 'src/matchers/http_matcher.dart';
Expand Down
4 changes: 2 additions & 2 deletions lib/src/adapters/dio_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class DioAdapter with Recording, RequestHandling implements HttpClientAdapter {
// Waits for defined duration.
if (response.delay != null) await Future.delayed(response.delay!);

// Throws DioError if response type is MockDioError.
if (isMockDioError(response)) throw response as DioError;
// Throws DioException if response type is MockDioException.
if (isMockDioException(response)) throw response as DioException;

return response as MockResponseBody;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'package:http_mock_adapter/src/adapters/dio_adapter.dart';
import 'package:http_mock_adapter/src/interceptors/dio_interceptor.dart';
import 'package:http_mock_adapter/src/response.dart';

/// Wrapper of [Dio]'s [DioError] [Exception].
class MockDioError extends DioError implements MockResponse {
/// Wrapper of [Dio]'s [DioException] [Exception].
class MockDioException extends DioException implements MockResponse {
@override
final Duration? delay;

MockDioError({
MockDioException({
required RequestOptions requestOptions,
Response? response,
DioErrorType type = DioErrorType.unknown,
DioExceptionType type = DioExceptionType.unknown,
dynamic error,
this.delay,
}) : super(
Expand All @@ -21,8 +21,8 @@ class MockDioError extends DioError implements MockResponse {
error: error,
);

static MockDioError from(DioError dioError, [Duration? delay]) =>
MockDioError(
static MockDioException from(DioException dioError, [Duration? delay]) =>
MockDioException(
requestOptions: dioError.requestOptions,
response: dioError.response,
type: dioError.type,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/handlers/request_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class MockServer {

void throws(
int statusCode,
DioError dioError, {
DioException dioError, {
Duration? delay,
});
}
Expand Down Expand Up @@ -77,9 +77,9 @@ class RequestHandler implements MockServer {
};
}

/// Stores the [DioError] inside the [mockResponse].
/// Stores the [DioException] inside the [mockResponse].
@override
void throws(int statusCode, DioError dioError, {Duration? delay}) {
mockResponse = (requestOptions) => MockDioError.from(dioError, delay);
void throws(int statusCode, DioException dioError, {Duration? delay}) {
mockResponse = (requestOptions) => MockDioException.from(dioError, delay);
}
}
6 changes: 3 additions & 3 deletions lib/src/interceptors/dio_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class DioInterceptor extends Interceptor with Recording, RequestHandling {
await setDefaultRequestHeaders(dio, requestOptions);
final response = mockResponse(requestOptions);

// Reject the response if type is MockDioError.
if (isMockDioError(response)) {
requestInterceptorHandler.reject(response as DioError);
// Reject the response if type is MockDioException.
if (isMockDioException(response)) {
requestInterceptorHandler.reject(response as DioException);

return;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/mixins/request_handling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,6 @@ mixin RequestHandling on Recording {
),
);

bool isMockDioError(MockResponse mockResponse) =>
mockResponse is MockDioError;
bool isMockDioException(MockResponse mockResponse) =>
mockResponse is MockDioException;
}
4 changes: 2 additions & 2 deletions lib/src/response.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'dart:typed_data';

import 'package:dio/dio.dart';

/// Top level interface for [Dio]'s [ResponseBody] and also [Dio]'s [DioError].
/// This interface makes sure that we can save [DioError] and [ResponseBody]
/// Top level interface for [Dio]'s [ResponseBody] and also [Dio]'s [DioException].
/// This interface makes sure that we can save [DioException] and [ResponseBody]
/// inside the same list.
abstract class MockResponse {
/// Delays this response by the given duration.
Expand Down
8 changes: 4 additions & 4 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ repository: https://github.com/lomsa-dev/http-mock-adapter
issue_tracker: https://github.com/lomsa-dev/http-mock-adapter/issues

environment:
sdk: ">=2.15.0 <3.0.0"
sdk: '>=2.18.3 <3.0.0'

dependencies:
collection: ^1.15.0
dio: ^5.0.0
collection: ^1.17.1
dio: ^5.2.1
http_parser: ^4.0.2

dev_dependencies:
lints: '>=1.0.1'
test: ^1.21.0
test: ^1.24.3
fake_async: ^1.3.1
8 changes: 4 additions & 4 deletions test/adapters/dio_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void main() {
expect(
() async => await dio.get('/route'),
throwsA(predicate(
(DioError dioError) => dioError.error is ClosedException)),
(DioException dioError) => dioError.error is ClosedException)),
);
});

Expand All @@ -47,8 +47,8 @@ void main() {

test('delays error', () async {
const delay = 5000;
final dioError = DioError(
type: DioErrorType.badResponse,
final dioError = DioException(
type: DioExceptionType.badResponse,
requestOptions: RequestOptions(path: 'path'),
);

Expand All @@ -65,7 +65,7 @@ void main() {
() async {
try {
await dio.get('/route');
} on DioError catch (_) {
} on DioException catch (_) {
// Ignore expected error.
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/extensions/matches_request_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ void main() {
expect(
() => dio.get(path),
throwsA(predicate((e) =>
e is DioError &&
e.type == DioErrorType.unknown &&
e is DioException &&
e.type == DioExceptionType.unknown &&
e.error is AssertionError)));
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/handlers/request_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ void main() {
expect(mockResponseBody.headers, headers);
});

test('sets DioError for a status code', () async {
test('sets DioException for a status code', () async {
const statusCode = HttpStatus.badRequest;
final dioError = DioError(
final dioError = DioException(
requestOptions: RequestOptions(
path: 'path',
),
type: DioErrorType.badResponse,
type: DioExceptionType.badResponse,
);

requestHandler.throws(
Expand All @@ -141,10 +141,10 @@ void main() {

expect(statusHandler, isNotNull);

final mockDioError =
statusHandler(RequestOptions(path: '')) as MockDioError;
final mockDioException =
statusHandler(RequestOptions(path: '')) as MockDioException;

expect(mockDioError.type, dioError.type);
expect(mockDioException.type, dioError.type);
});
});
}
3 changes: 2 additions & 1 deletion test/mixins/recording_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ void main() {
expect(
() async => await dio.get('/undefined'),
throwsA(
predicate((DioError dioError) => dioError.error is AssertionError),
predicate(
(DioException dioError) => dioError.error is AssertionError),
),
);
});
Expand Down
11 changes: 6 additions & 5 deletions test/mixins/request_handling_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,28 @@ void main() {
});

test('throws raises custom exception', () async {
final dioError = DioError(
final dioError = DioException(
error: {'message': 'error'},
requestOptions: RequestOptions(path: path),
response: Response(
statusCode: 500,
requestOptions: RequestOptions(path: path),
),
type: DioErrorType.badResponse,
type: DioExceptionType.badResponse,
);

tester.onGet(
path,
(server) => server.throws(500, dioError),
);

expect(() async => await dio.get(path), throwsA(isA<MockDioError>()));
expect(() async => await dio.get(path), throwsA(isA<DioError>()));
expect(() async => await dio.get(path),
throwsA(isA<MockDioException>()));
expect(() async => await dio.get(path), throwsA(isA<DioException>()));
expect(
() async => await dio.get(path),
throwsA(
predicate((DioError error) => error is MockDioError),
predicate((DioException error) => error is MockDioException),
),
);
});
Expand Down

0 comments on commit f999fbf

Please sign in to comment.