initial commit
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:manager/core/network/api_exception.dart';
|
||||
import 'package:manager/core/network/dio_error_mapper.dart';
|
||||
|
||||
RequestOptions _options() => RequestOptions(path: '/v1/services/');
|
||||
|
||||
void main() {
|
||||
group('mapDioException', () {
|
||||
test('maps timeout variants to ApiException.timeout', () {
|
||||
for (final type in [
|
||||
DioExceptionType.connectionTimeout,
|
||||
DioExceptionType.sendTimeout,
|
||||
DioExceptionType.receiveTimeout,
|
||||
]) {
|
||||
final result = mapDioException(DioException(requestOptions: _options(), type: type));
|
||||
expect(result, isA<ApiTimeoutException>());
|
||||
}
|
||||
});
|
||||
|
||||
test('maps connectionError to ApiException.network', () {
|
||||
final result = mapDioException(
|
||||
DioException(
|
||||
requestOptions: _options(),
|
||||
type: DioExceptionType.connectionError,
|
||||
message: 'Failed host lookup',
|
||||
),
|
||||
);
|
||||
expect(result, isA<ApiNetworkException>());
|
||||
expect((result as ApiNetworkException).message, contains('Failed host lookup'));
|
||||
});
|
||||
|
||||
test('maps cancel to ApiException.cancelled', () {
|
||||
final result = mapDioException(
|
||||
DioException(requestOptions: _options(), type: DioExceptionType.cancel),
|
||||
);
|
||||
expect(result, isA<ApiCancelledException>());
|
||||
});
|
||||
|
||||
test('maps badResponse with a {"error": msg} body to ApiException.server, extracting the message', () {
|
||||
final response = Response<dynamic>(
|
||||
requestOptions: _options(),
|
||||
statusCode: 404,
|
||||
data: {'error': 'not found'},
|
||||
);
|
||||
final result = mapDioException(
|
||||
DioException(requestOptions: _options(), type: DioExceptionType.badResponse, response: response),
|
||||
);
|
||||
expect(result, isA<ApiServerException>());
|
||||
final server = result as ApiServerException;
|
||||
expect(server.statusCode, 404);
|
||||
expect(server.message, 'not found');
|
||||
});
|
||||
|
||||
test('maps badResponse without a parseable body to the status message', () {
|
||||
final response = Response<dynamic>(
|
||||
requestOptions: _options(),
|
||||
statusCode: 500,
|
||||
statusMessage: 'Internal Server Error',
|
||||
data: null,
|
||||
);
|
||||
final result = mapDioException(
|
||||
DioException(requestOptions: _options(), type: DioExceptionType.badResponse, response: response),
|
||||
);
|
||||
expect(result, isA<ApiServerException>());
|
||||
expect((result as ApiServerException).message, 'Internal Server Error');
|
||||
});
|
||||
|
||||
test('passes an already-typed ApiException through unchanged', () {
|
||||
const original = ApiException.timeout();
|
||||
expect(mapDioException(original), same(original));
|
||||
});
|
||||
|
||||
test('maps a non-Dio, non-ApiException error to ApiException.unknown', () {
|
||||
final result = mapDioException(FormatException('bad json'));
|
||||
expect(result, isA<ApiUnknownException>());
|
||||
});
|
||||
});
|
||||
|
||||
group('ApiExceptionUserMessage', () {
|
||||
test('server(404) gets a generic "not found" message regardless of body text', () {
|
||||
const exception = ApiException.server(404, 'service not found');
|
||||
expect(exception.userMessage, 'Not found.');
|
||||
});
|
||||
|
||||
test('server(non-404) includes the status code and body message', () {
|
||||
const exception = ApiException.server(400, 'no backup targets configured');
|
||||
expect(exception.userMessage, contains('400'));
|
||||
expect(exception.userMessage, contains('no backup targets configured'));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user