|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:http/http.dart'; |
| 4 | +import 'package:powersync_core/src/exceptions.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + group('SyncResponseException', () { |
| 9 | + const errorResponse = |
| 10 | + '{"error":{"code":"PSYNC_S2106","status":401,"description":"Authentication required","name":"AuthorizationError"}}'; |
| 11 | + |
| 12 | + test('fromStreamedResponse', () async { |
| 13 | + final exc = await SyncResponseException.fromStreamedResponse( |
| 14 | + StreamedResponse(Stream.value(utf8.encode(errorResponse)), 401)); |
| 15 | + |
| 16 | + expect(exc.statusCode, 401); |
| 17 | + expect(exc.description, |
| 18 | + 'Request failed: PSYNC_S2106(AuthorizationError): Authentication required'); |
| 19 | + }); |
| 20 | + |
| 21 | + test('fromResponse', () { |
| 22 | + final exc = |
| 23 | + SyncResponseException.fromResponse(Response(errorResponse, 401)); |
| 24 | + expect(exc.statusCode, 401); |
| 25 | + expect(exc.description, |
| 26 | + 'Request failed: PSYNC_S2106(AuthorizationError): Authentication required'); |
| 27 | + }); |
| 28 | + |
| 29 | + test('with description', () { |
| 30 | + const errorResponse = |
| 31 | + '{"error":{"code":"PSYNC_S2106","status":401,"description":"Authentication required","name":"AuthorizationError", "details": "Missing authorization header"}}'; |
| 32 | + |
| 33 | + final exc = |
| 34 | + SyncResponseException.fromResponse(Response(errorResponse, 401)); |
| 35 | + expect(exc.statusCode, 401); |
| 36 | + expect(exc.description, |
| 37 | + 'Request failed: PSYNC_S2106(AuthorizationError): Authentication required, Missing authorization header'); |
| 38 | + }); |
| 39 | + |
| 40 | + test('malformed', () { |
| 41 | + const malformed = |
| 42 | + '{"message":"Route GET:/foo/bar not found","error":"Not Found","statusCode":404}'; |
| 43 | + |
| 44 | + final exc = SyncResponseException.fromResponse(Response(malformed, 401)); |
| 45 | + expect(exc.statusCode, 401); |
| 46 | + expect(exc.description, |
| 47 | + 'Request failed: {"message":"Route GET:/foo/bar not found","error":"Not Found","statusCode":404}'); |
| 48 | + |
| 49 | + final exc2 = SyncResponseException.fromResponse(Response( |
| 50 | + 'not even json', 500, |
| 51 | + reasonPhrase: 'Internal server error')); |
| 52 | + expect(exc2.statusCode, 500); |
| 53 | + expect(exc2.description, 'Internal server error'); |
| 54 | + }); |
| 55 | + }); |
| 56 | +} |
0 commit comments