|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:typed_data'; |
| 3 | + |
| 4 | +import 'package:http/http.dart'; |
| 5 | +import 'package:logging/logging.dart'; |
| 6 | +import 'package:postgrest/postgrest.dart'; |
| 7 | +import 'package:test/test.dart'; |
| 8 | + |
| 9 | +class _DelayedHttpClient extends BaseClient { |
| 10 | + final Duration delay; |
| 11 | + final int statusCode; |
| 12 | + final String body; |
| 13 | + |
| 14 | + _DelayedHttpClient({ |
| 15 | + required this.delay, |
| 16 | + this.statusCode = 200, |
| 17 | + this.body = '[]', |
| 18 | + }); |
| 19 | + |
| 20 | + @override |
| 21 | + Future<StreamedResponse> send(BaseRequest request) async { |
| 22 | + await Future.delayed(delay); |
| 23 | + return StreamedResponse( |
| 24 | + Stream.value(Uint8List.fromList(body.codeUnits)), |
| 25 | + statusCode, |
| 26 | + request: request, |
| 27 | + ); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class _InstantHttpClient extends BaseClient { |
| 32 | + final int statusCode; |
| 33 | + final String body; |
| 34 | + |
| 35 | + _InstantHttpClient({this.statusCode = 200, this.body = '[]'}); |
| 36 | + |
| 37 | + @override |
| 38 | + Future<StreamedResponse> send(BaseRequest request) async { |
| 39 | + return StreamedResponse( |
| 40 | + Stream.value(Uint8List.fromList(body.codeUnits)), |
| 41 | + statusCode, |
| 42 | + request: request, |
| 43 | + ); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +void main() { |
| 48 | + group('URL length validation', () { |
| 49 | + test('logs warning when URL exceeds urlLengthLimit', () async { |
| 50 | + final warnings = <String>[]; |
| 51 | + final subscription = Logger.root.onRecord.listen((record) { |
| 52 | + if (record.level == Level.WARNING) { |
| 53 | + warnings.add(record.message); |
| 54 | + } |
| 55 | + }); |
| 56 | + Logger.root.level = Level.ALL; |
| 57 | + |
| 58 | + final client = PostgrestClient( |
| 59 | + 'http://localhost:3000', |
| 60 | + httpClient: _InstantHttpClient(), |
| 61 | + urlLengthLimit: 50, |
| 62 | + ); |
| 63 | + |
| 64 | + // URL: http://localhost:3000/users?select=id,username,email,created_at — well over 50 chars |
| 65 | + await client |
| 66 | + .from('users') |
| 67 | + .select('id,username,email,created_at') |
| 68 | + .catchError((_) => []); |
| 69 | + |
| 70 | + await subscription.cancel(); |
| 71 | + |
| 72 | + expect( |
| 73 | + warnings.any((w) => w.contains('exceeds the limit of 50')), |
| 74 | + isTrue, |
| 75 | + reason: 'Expected a warning about URL length', |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + test('does not log warning when URL is within urlLengthLimit', () async { |
| 80 | + final warnings = <String>[]; |
| 81 | + final subscription = Logger.root.onRecord.listen((record) { |
| 82 | + if (record.level == Level.WARNING && |
| 83 | + record.message.contains('exceeds the limit')) { |
| 84 | + warnings.add(record.message); |
| 85 | + } |
| 86 | + }); |
| 87 | + Logger.root.level = Level.ALL; |
| 88 | + |
| 89 | + final client = PostgrestClient( |
| 90 | + 'http://localhost:3000', |
| 91 | + httpClient: _InstantHttpClient(), |
| 92 | + urlLengthLimit: 8000, |
| 93 | + ); |
| 94 | + |
| 95 | + await client.from('users').select().catchError((_) => []); |
| 96 | + |
| 97 | + await subscription.cancel(); |
| 98 | + |
| 99 | + expect(warnings, isEmpty, reason: 'Expected no URL length warning'); |
| 100 | + }); |
| 101 | + |
| 102 | + test('default urlLengthLimit is 8000', () { |
| 103 | + final client = PostgrestClient('http://localhost:3000'); |
| 104 | + expect(client.urlLengthLimit, equals(8000)); |
| 105 | + }); |
| 106 | + |
| 107 | + test('custom urlLengthLimit is stored', () { |
| 108 | + final client = |
| 109 | + PostgrestClient('http://localhost:3000', urlLengthLimit: 5000); |
| 110 | + expect(client.urlLengthLimit, equals(5000)); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + group('Timeout configuration', () { |
| 115 | + test('timeout is null by default', () { |
| 116 | + final client = PostgrestClient('http://localhost:3000'); |
| 117 | + expect(client.timeout, isNull); |
| 118 | + }); |
| 119 | + |
| 120 | + test('custom timeout is stored', () { |
| 121 | + final client = PostgrestClient('http://localhost:3000', timeout: 5000); |
| 122 | + expect(client.timeout, equals(5000)); |
| 123 | + }); |
| 124 | + |
| 125 | + test('request times out when response is delayed beyond timeout', () async { |
| 126 | + final client = PostgrestClient( |
| 127 | + 'http://localhost:3000', |
| 128 | + httpClient: _DelayedHttpClient(delay: const Duration(seconds: 5)), |
| 129 | + timeout: 100, // 100ms timeout |
| 130 | + ); |
| 131 | + |
| 132 | + expect( |
| 133 | + () => client.from('users').select(), |
| 134 | + throwsA(isA<TimeoutException>()), |
| 135 | + ); |
| 136 | + }); |
| 137 | + |
| 138 | + test('request succeeds when response is within timeout', () async { |
| 139 | + final client = PostgrestClient( |
| 140 | + 'http://localhost:3000', |
| 141 | + httpClient: _InstantHttpClient(body: '[{"id": 1}]'), |
| 142 | + timeout: 5000, // 5 second timeout |
| 143 | + ); |
| 144 | + |
| 145 | + final result = await client.from('users').select(); |
| 146 | + expect(result, isA<List>()); |
| 147 | + }); |
| 148 | + }); |
| 149 | +} |
0 commit comments