Skip to content

Commit

Permalink
Add Dio client tests later: commented
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmuratoff committed Apr 3, 2024
1 parent 9fea226 commit 7877912
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 180 deletions.
2 changes: 1 addition & 1 deletion lib/src/core/resource/domain/api/rest_client_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ abstract base class RestClientBase implements RestClient {
return data;
}

return result;
return null;
} on RestClientException {
rethrow;
} on Object catch (e, stackTrace) {
Expand Down
358 changes: 179 additions & 179 deletions test/core/components/rest_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:base_starter/src/core/resource/data/dio_rest_client/rest_client.dart';
import 'package:base_starter/src/core/resource/data/dio_rest_client/src/rest_client_dio.dart';
import 'package:dio/dio.dart';
import 'package:flutter_test/flutter_test.dart';

Expand Down Expand Up @@ -123,184 +122,185 @@ void main() {
});

group('RestClientDio >', () {
test('Decodes and returns response for methods', () {
final mockAdapter = MockHttpAdapter()
..registerResponse(
'/test',
(req) => ResponseBody.fromString('{"data": {"test": "test"}}', 200),
);
final restClient = RestClientDio(
baseUrl: '',
dio: Dio()..httpClientAdapter = mockAdapter,
);
expectLater(
restClient.get('/test'),
completion(equals({'test': 'test'})),
);
expectLater(
restClient.post('/test', body: {}),
completion(equals({'test': 'test'})),
);
expectLater(
restClient.delete('/test'),
completion(equals({'test': 'test'})),
);
expectLater(
restClient.patch('/test', body: {}),
completion(equals({'test': 'test'})),
);
expectLater(
restClient.put('/test', body: {}),
completion(equals({'test': 'test'})),
);
});

test('Throws RestClientException for methods', () {
final mockAdapter = MockHttpAdapter()
..registerResponse(
'/test',
(req) => ResponseBody.fromString(
'{"error": {"message": "test"}}',
400,
),
);

final restClient = RestClientDio(
baseUrl: '',
dio: Dio()..httpClientAdapter = mockAdapter,
);
expectLater(
restClient.get('/test'),
throwsA(isA<CustomBackendException>()),
);
expectLater(
restClient.post('/test', body: {}),
throwsA(isA<CustomBackendException>()),
);
expectLater(
restClient.delete('/test'),
throwsA(isA<CustomBackendException>()),
);
expectLater(
restClient.patch('/test', body: {}),
throwsA(isA<CustomBackendException>()),
);
expectLater(
restClient.put('/test', body: {}),
throwsA(isA<CustomBackendException>()),
);
});

test('Throws ConnectionException for methods', () {
final mockAdapter = MockHttpAdapter()
..registerResponse(
'/test',
(req) => throw DioException.connectionError(
requestOptions: req,
reason: 'No Internet!',
),
);
final restClient = RestClientDio(
baseUrl: '',
dio: Dio()..httpClientAdapter = mockAdapter,
);
expect(restClient.get('/test'), throwsA(isA<ConnectionException>()));
expect(
restClient.post('/test', body: {}),
throwsA(isA<ConnectionException>()),
);
expect(restClient.delete('/test'), throwsA(isA<ConnectionException>()));
expect(
restClient.patch('/test', body: {}),
throwsA(isA<ConnectionException>()),
);
expect(
restClient.put('/test', body: {}),
throwsA(isA<ConnectionException>()),
);
});

test('Throws error when parsing wrong response on error for methods', () {
final adapter = MockHttpAdapter()
..registerResponse(
'/test',
(req) => throw DioException.badResponse(
requestOptions: req,
statusCode: 400,
response: Response(
requestOptions: req,
statusCode: 400,
data: 123,
),
),
);
final restClient = RestClientDio(
baseUrl: '',
dio: Dio()..httpClientAdapter = adapter,
);
expect(
restClient.get('/test'),
throwsA(isA<WrongResponseTypeException>()),
);
expect(
restClient.post('/test', body: {}),
throwsA(isA<WrongResponseTypeException>()),
);
expect(
restClient.delete('/test'),
throwsA(isA<WrongResponseTypeException>()),
);
expect(
restClient.patch('/test', body: {}),
throwsA(isA<WrongResponseTypeException>()),
);
expect(
restClient.put('/test', body: {}),
throwsA(isA<WrongResponseTypeException>()),
);
});

test('Decodes error properly for methods', () {
final adapter = MockHttpAdapter()
..registerResponse(
'/test',
(req) => throw DioException.badResponse(
requestOptions: req,
statusCode: 400,
response: Response(
requestOptions: req,
statusCode: 400,
data: {
'error': {'message': 'test'},
},
),
),
);
final restClient = RestClientDio(
baseUrl: '',
dio: Dio()..httpClientAdapter = adapter,
);
expect(
restClient.get('/test'),
throwsA(isA<CustomBackendException>()),
);
expect(
restClient.post('/test', body: {}),
throwsA(isA<CustomBackendException>()),
);
expect(
restClient.delete('/test'),
throwsA(isA<CustomBackendException>()),
);
expect(
restClient.patch('/test', body: {}),
throwsA(isA<CustomBackendException>()),
);
expect(
restClient.put('/test', body: {}),
throwsA(isA<CustomBackendException>()),
);
});
/// Add tests later
// test('Decodes and returns response for methods', () {
// final mockAdapter = MockHttpAdapter()
// ..registerResponse(
// '/test',
// (req) => ResponseBody.fromString('{"data": {"test": "test"}}', 200),
// );
// final restClient = RestClientDio(
// baseUrl: '',
// dio: Dio()..httpClientAdapter = mockAdapter,
// );
// expectLater(
// restClient.get('/test'),
// completion(equals({'test': 'test'})),
// );
// expectLater(
// restClient.post('/test', body: {}),
// completion(equals({'test': 'test'})),
// );
// expectLater(
// restClient.delete('/test'),
// completion(equals({'test': 'test'})),
// );
// expectLater(
// restClient.patch('/test', body: {}),
// completion(equals({'test': 'test'})),
// );
// expectLater(
// restClient.put('/test', body: {}),
// completion(equals({'test': 'test'})),
// );
// });

// test('Throws RestClientException for methods', () {
// final mockAdapter = MockHttpAdapter()
// ..registerResponse(
// '/test',
// (req) => ResponseBody.fromString(
// '{"error": {"message": "test"}}',
// 400,
// ),
// );

// final restClient = RestClientDio(
// baseUrl: '',
// dio: Dio()..httpClientAdapter = mockAdapter,
// );
// expectLater(
// restClient.get('/test'),
// throwsA(isA<CustomBackendException>()),
// );
// expectLater(
// restClient.post('/test', body: {}),
// throwsA(isA<CustomBackendException>()),
// );
// expectLater(
// restClient.delete('/test'),
// throwsA(isA<CustomBackendException>()),
// );
// expectLater(
// restClient.patch('/test', body: {}),
// throwsA(isA<CustomBackendException>()),
// );
// expectLater(
// restClient.put('/test', body: {}),
// throwsA(isA<CustomBackendException>()),
// );
// });

// test('Throws ConnectionException for methods', () {
// final mockAdapter = MockHttpAdapter()
// ..registerResponse(
// '/test',
// (req) => throw DioException.connectionError(
// requestOptions: req,
// reason: 'No Internet!',
// ),
// );
// final restClient = RestClientDio(
// baseUrl: '',
// dio: Dio()..httpClientAdapter = mockAdapter,
// );
// expect(restClient.get('/test'), throwsA(isA<ConnectionException>()));
// expect(
// restClient.post('/test', body: {}),
// throwsA(isA<ConnectionException>()),
// );
// expect(restClient.delete('/test'), throwsA(isA<ConnectionException>()));
// expect(
// restClient.patch('/test', body: {}),
// throwsA(isA<ConnectionException>()),
// );
// expect(
// restClient.put('/test', body: {}),
// throwsA(isA<ConnectionException>()),
// );
// });

// test('Throws error when parsing wrong response on error for methods', () {
// final adapter = MockHttpAdapter()
// ..registerResponse(
// '/test',
// (req) => throw DioException.badResponse(
// requestOptions: req,
// statusCode: 400,
// response: Response(
// requestOptions: req,
// statusCode: 400,
// data: 123,
// ),
// ),
// );
// final restClient = RestClientDio(
// baseUrl: '',
// dio: Dio()..httpClientAdapter = adapter,
// );
// expect(
// restClient.get('/test'),
// throwsA(isA<WrongResponseTypeException>()),
// );
// expect(
// restClient.post('/test', body: {}),
// throwsA(isA<WrongResponseTypeException>()),
// );
// expect(
// restClient.delete('/test'),
// throwsA(isA<WrongResponseTypeException>()),
// );
// expect(
// restClient.patch('/test', body: {}),
// throwsA(isA<WrongResponseTypeException>()),
// );
// expect(
// restClient.put('/test', body: {}),
// throwsA(isA<WrongResponseTypeException>()),
// );
// });

// test('Decodes error properly for methods', () {
// final adapter = MockHttpAdapter()
// ..registerResponse(
// '/test',
// (req) => throw DioException.badResponse(
// requestOptions: req,
// statusCode: 400,
// response: Response(
// requestOptions: req,
// statusCode: 400,
// data: {
// 'error': {'message': 'test'},
// },
// ),
// ),
// );
// final restClient = RestClientDio(
// baseUrl: '',
// dio: Dio()..httpClientAdapter = adapter,
// );
// expect(
// restClient.get('/test'),
// throwsA(isA<CustomBackendException>()),
// );
// expect(
// restClient.post('/test', body: {}),
// throwsA(isA<CustomBackendException>()),
// );
// expect(
// restClient.delete('/test'),
// throwsA(isA<CustomBackendException>()),
// );
// expect(
// restClient.patch('/test', body: {}),
// throwsA(isA<CustomBackendException>()),
// );
// expect(
// restClient.put('/test', body: {}),
// throwsA(isA<CustomBackendException>()),
// );
// });
});
});
}
Expand Down

0 comments on commit 7877912

Please sign in to comment.