|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:dio/dio.dart'; |
| 4 | +import 'package:http/http.dart' show ClientException; |
| 5 | +import 'package:parse_server_sdk/parse_server_sdk.dart'; |
| 6 | +import 'package:test/test.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('buildParseResponseWithException', () { |
| 10 | + test('parses DioException with JSON body', () { |
| 11 | + final response = Response( |
| 12 | + requestOptions: RequestOptions(path: '/'), |
| 13 | + data: json.encode({'error': 'some error', 'code': '123'}), |
| 14 | + statusCode: 400, |
| 15 | + statusMessage: 'Bad Request', |
| 16 | + ); |
| 17 | + |
| 18 | + final dioException = DioException( |
| 19 | + requestOptions: response.requestOptions, |
| 20 | + response: response, |
| 21 | + ); |
| 22 | + |
| 23 | + final result = buildParseResponseWithException(dioException); |
| 24 | + |
| 25 | + expect(result.error, isNotNull); |
| 26 | + expect(result.error!.message, 'some error'); |
| 27 | + expect(result.error!.code, 123); |
| 28 | + expect(result.error!.exception, dioException); |
| 29 | + }); |
| 30 | + |
| 31 | + test('uses statusMessage and statusCode when body is not JSON', () { |
| 32 | + final response = Response( |
| 33 | + requestOptions: RequestOptions(path: '/'), |
| 34 | + data: 'Not a JSON body', |
| 35 | + statusCode: 404, |
| 36 | + statusMessage: 'Not Found', |
| 37 | + ); |
| 38 | + |
| 39 | + final dioException = DioException( |
| 40 | + requestOptions: response.requestOptions, |
| 41 | + response: response, |
| 42 | + ); |
| 43 | + |
| 44 | + final result = buildParseResponseWithException(dioException); |
| 45 | + |
| 46 | + expect(result.error, isNotNull); |
| 47 | + expect(result.error!.message, 'Not Found'); |
| 48 | + expect(result.error!.code, 404); |
| 49 | + expect(result.error!.exception, dioException); |
| 50 | + }); |
| 51 | + |
| 52 | + test('handles http ClientException', () { |
| 53 | + final clientEx = ClientException('no network'); |
| 54 | + |
| 55 | + final result = buildParseResponseWithException(clientEx); |
| 56 | + |
| 57 | + expect(result.error, isNotNull); |
| 58 | + expect(result.error!.message, 'no network'); |
| 59 | + expect(result.error!.exception, clientEx); |
| 60 | + }); |
| 61 | + |
| 62 | + test('handles generic Exception', () { |
| 63 | + final ex = Exception('generic'); |
| 64 | + |
| 65 | + final result = buildParseResponseWithException(ex); |
| 66 | + |
| 67 | + expect(result.error, isNotNull); |
| 68 | + expect(result.error!.message, ex.toString()); |
| 69 | + expect(result.error!.exception, ex); |
| 70 | + }); |
| 71 | + }); |
| 72 | +} |
0 commit comments