-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadme_examples_test.dart
More file actions
1102 lines (953 loc) · 37.3 KB
/
Copy pathreadme_examples_test.dart
File metadata and controls
1102 lines (953 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Every Dart snippet in README.md, compiled and — wherever the fakes allow —
/// executed. This file is what lets the README claim its examples cannot rot:
/// an API change that breaks a documented snippet breaks this file first.
///
/// Layout mirrors the README. Each test is named after the section its snippet
/// lives under, and the snippet bodies are kept verbatim except for:
///
/// * free variables the prose implies (`token`, `source`, `certPem`, …),
/// declared immediately above their use;
/// * network calls, which run against [FakeRequestExecutor] /
/// [FakeStreamDemux] instead of a socket — the same seam every other unit
/// test uses;
/// * the cancellation timer, shortened from 2 s to 1 ms so the suite stays
/// fast.
///
/// Deliberately absent, with reasons:
///
/// * "Why there is no web build" — imports `package:http/browser_client.dart`,
/// which only compiles for the web; a VM test file cannot contain it.
/// * the `dio` snippet — compiled and run by
/// `test/dio/dio_readme_example_test.dart`, which owns the adapter seam it
/// needs.
/// * `NitroHttp.init` — it constructs a real [NitroHttpClient], and that
/// constructor configures the native executor, so there is no seam to pass
/// a fake through. Covered against the real engine by the `NitroHttp.init`
/// group in `native_smoke_test.dart`, which is also the only place its
/// replace-and-dispose behaviour can actually be observed.
/// * `void main() => runApp(...)` in the hot-restart snippet — it is Flutter's
/// API, not this package's, and the whole point of that snippet is that it
/// contains NO recovery call. There is nothing here to execute; the
/// behaviour it claims is proven against the real engine by
/// `native_smoke_test.dart` and `example/integration_test/cancellation_test.dart`.
library;
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:nitro_http/nitro_http.dart';
import 'package:nitro_http/src/nitro_http.native.dart';
import 'package:nitro_http/src/internal/ws_runner.dart' show WsOpcode;
import 'support/fakes.dart';
void main() {
late FakeRequestExecutor executor;
late FakeStreamDemux demux;
NitroHttpClient makeClient({
ClientSettings settings = const ClientSettings(),
List<Interceptor> interceptors = const <Interceptor>[],
}) => NitroHttpClient(
settings: settings,
interceptors: interceptors,
executor: executor,
demux: demux,
);
setUp(() {
executor = FakeRequestExecutor();
demux = FakeStreamDemux();
});
tearDown(() => demux.closeAll());
// The snippet directly under the badges — the first code anyone reads, and
// the one most likely to be copied verbatim, so it is held to the same
// standard as the rest.
group('Header', () {
test('hero snippet', () async {
final defaultClient = makeClient();
NitroHttp.overrideDefaultClientForTesting(defaultClient);
addTearDown(() => NitroHttp.overrideDefaultClientForTesting(null));
executor.bufferedResponses.add(
rawResponse(body: Uint8List.fromList(utf8.encode('{"name":"Ada"}'))),
);
final res = await fetch('https://api.example.com/users/42');
expect(res.bodyToJson(), {'name': 'Ada'});
// The snippet's comment claims this prints a protocol label, so check it
// is one rather than merely non-null.
expect(res.version.label, matches(r'^HTTP/'));
});
});
group('Quick start', () {
test('client per API: get, post, dispose', () async {
final client = NitroHttpClient(
settings: const ClientSettings(
baseUrl: 'https://api.example.com',
userAgent: 'my_app/1.0',
),
executor: executor,
demux: demux,
);
executor.bufferedResponses
..add(rawResponse(body: Uint8List.fromList(utf8.encode('{"name":"Ada"}'))))
..add(rawResponse(status: 201));
final user = await client.get('/users/42');
expect((user.bodyToJson() as Map)['name'], 'Ada');
final created = await client.post(
'/users',
body: HttpBody.json({'name': 'Ada'}),
);
// The snippet's comment says 201, so assert 201 and not merely success.
expect(created.statusCode, 201);
client.dispose();
expect(executor.disposeCount, 1);
});
test('fetch() one-liner', () async {
final defaultClient = makeClient();
NitroHttp.overrideDefaultClientForTesting(defaultClient);
addTearDown(() => NitroHttp.overrideDefaultClientForTesting(null));
executor.bufferedResponses.add(
rawResponse(body: Uint8List.fromList(utf8.encode('ok'))),
);
final res = await fetch('https://api.example.com/health');
expect(res.statusCode, 200);
expect(res.body, 'ok');
});
test('configured client', () async {
final client = NitroHttpClient(
settings: const ClientSettings(
baseUrl: 'https://api.example.com/v1',
timeout: Duration(seconds: 30),
connectTimeout: Duration(seconds: 10),
httpVersionPref: HttpVersionPref.http2,
cookieSettings: CookieSettings(storeCookies: true),
),
interceptors: [RetryInterceptor(maxRetries: 3)],
executor: executor,
demux: demux,
);
executor.bufferedResponses.add(
rawResponse(body: Uint8List.fromList(utf8.encode('[{"id": 1}]'))),
);
final res = await client.get('/users', query: {
'page': '2',
'tag': ['a', 'b'],
});
final users = res.bodyToJson();
expect(users, [
{'id': 1},
]);
expect(
executor.bufferedRequests.single.request.url,
'https://api.example.com/v1/users?page=2&tag=a&tag=b',
);
client.dispose();
expect(executor.disposeCount, 1);
});
});
group('Feature tour', () {
test('Verbs', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
for (var i = 0; i < 9; i++) {
executor.bufferedResponses.add(rawResponse());
}
await client.get('/users');
await client.head('/users');
await client.post('/users', body: HttpBody.json({'name': 'Ada'}));
await client.put('/users/1', body: HttpBody.json({'name': 'Ada'}));
await client.patch('/users/1', body: HttpBody.json({'name': 'Grace'}));
await client.delete('/users/1');
await client.options_('/users');
await client.trace('/users');
await client.requestText(
HttpMethod.custom,
'/graph',
customMethod: 'PURGE',
);
expect(executor.bufferedRequests, hasLength(9));
expect(executor.bufferedRequests.last.request.customMethod, 'PURGE');
});
test('Verbs: a non-standard method through request', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse());
await client.requestText(HttpMethod.custom, '/graph', customMethod: 'PURGE');
final sent = executor.bufferedRequests.single.request;
expect(sent.method, RawMethod.custom);
expect(sent.customMethod, 'PURGE');
});
// NitroHttp.init itself cannot be exercised here — it constructs a real
// NitroHttpClient, whose constructor configures the native executor, so
// there is no seam to inject a fake through. It is covered against the real
// engine in native_smoke_test.dart ('NitroHttp.init'). What IS checkable
// here is the rest of the snippet: that the static verbs and getStream go
// to whatever client the default is set to.
test('The default client: static verbs and getStream', () async {
final defaultClient = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
NitroHttp.overrideDefaultClientForTesting(defaultClient);
addTearDown(() => NitroHttp.overrideDefaultClientForTesting(null));
executor.bufferedResponses
..add(rawResponse(body: Uint8List.fromList(utf8.encode('{"name":"Ada"}'))))
..add(rawResponse(status: 201));
final user = await NitroHttp.get('/users/42');
expect((user.bodyToJson() as Map)['name'], 'Ada');
final posted = await NitroHttp.post(
'/users',
body: HttpBody.json({'name': 'Ada'}),
);
expect(posted.statusCode, 201);
// baseUrl came from the default client, so the relative paths resolved.
expect(
executor.bufferedRequests.map((r) => r.request.url),
['https://api.example.com/users/42', 'https://api.example.com/users'],
);
executor.onStartStreamed = (request, body) {
scheduleMicrotask(() {
demux.push(chunk(request.requestId, utf8.encode('a,b\n1,2\n')));
demux.push(doneChunk(request.requestId));
});
return rawHead(requestId: request.requestId);
};
final report = await NitroHttp.getStream('/reports/2026.csv');
final sink = BytesBuilder();
await for (final c in report.body) {
sink.add(c);
}
expect(utf8.decode(sink.takeBytes()), 'a,b\n1,2\n');
});
test('Query parameters and per-call headers', () async {
const token = 'a-refresh-token';
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse());
final res = await client.get(
'/search',
query: {'q': 'flutter', 'limit': '20'},
headers: HttpHeaders.fromMap({'Authorization': 'Bearer $token'}),
);
final sent = executor.bufferedRequests.single.request;
expect(sent.url, 'https://api.example.com/search?q=flutter&limit=20');
expect(
sent.headers.any(
(h) =>
h.name.toLowerCase() == 'authorization' &&
h.value == 'Bearer $token',
),
isTrue,
);
expect(res.statusCode, 200);
});
test('Request bodies', () {
const token = 'a-refresh-token';
final source = Stream<Uint8List>.fromIterable([Uint8List(4096)]);
final bodies = <HttpBody>[
// UTF-8 text.
HttpBody.text('hello', contentType: 'text/plain; charset=utf-8'),
// jsonEncode-ed, with application/json; charset=utf-8.
HttpBody.json({
'name': 'Ada',
'roles': ['admin'],
}),
// Raw bytes.
HttpBody.bytes(Uint8List.fromList([0xDE, 0xAD, 0xBE, 0xEF])),
// application/x-www-form-urlencoded.
HttpBody.form({'grant_type': 'refresh_token', 'token': token}),
// multipart/form-data; file parts stream off disk.
HttpBody.multipart([
MultipartItem.text('caption', 'Sunset'),
MultipartItem.file('photo', '/tmp/sunset.jpg',
contentType: 'image/jpeg'),
]),
// A Dart stream, chunked when the length is unknown.
HttpBody.stream(source, contentLength: 4096),
// A path handed straight to the engine.
HttpBody.file('/tmp/backup.zip'),
];
expect(bodies, hasLength(7));
});
test('Responses: sealed switch and metadata', () async {
String describe(HttpResponse res) => switch (res) {
HttpTextResponse(:final body) => 'text: ${body.length} chars',
HttpBytesResponse(:final bodyBytes) => 'bytes: ${bodyBytes.length}',
HttpStreamResponse(:final contentLength) =>
'stream: ${contentLength ?? -1}',
};
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse(
body: Uint8List.fromList(utf8.encode('[]')),
version: RawHttpVersion.http2,
finalUrl: 'https://api.example.com/users',
headers: const [
RawHeader(name: 'Set-Cookie', value: 'a=1'),
RawHeader(name: 'Set-Cookie', value: 'b=2'),
],
));
final res = await client.get('/users');
expect(describe(res), 'text: 2 chars');
expect(res.version.label, 'HTTP/2');
expect(res.finalUrl.toString(), 'https://api.example.com/users');
expect(res.redirectCount, 0);
expect(res.primaryIp, isNotEmpty);
expect(res.fromCache, isFalse);
expect(res.headers.getAll('set-cookie'), ['a=1', 'b=2']);
});
test('Errors: the sealed exception switch', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse(
kind: RawErrorKind.timeoutRequest,
message: 'request timed out',
));
String? detail;
try {
await client.get('/users');
} on NitroHttpException catch (e) {
detail = switch (e) {
NitroHttpTimeoutException(:final stage) => 'timeout at ${stage.name}',
NitroHttpCancelException(:final reason) =>
'cancelled: ${reason ?? ''}',
NitroHttpStatusCodeException(:final statusCode) =>
'status $statusCode',
NitroHttpCertificateException(:final isPinMismatch) =>
isPinMismatch ? 'pin mismatch' : 'bad certificate',
NitroHttpTlsException() => 'no shared TLS version or cipher',
NitroHttpConfigurationException() =>
'these settings cannot be satisfied',
NitroHttpConnectionException(:final failure) =>
'connection ${failure.name}',
NitroHttpRedirectException(:final redirectCount) =>
'$redirectCount hops',
NitroHttpProtocolException() => 'protocol error',
NitroHttpDecodingException() => 'undecodable body',
NitroHttpResponseTooLargeException() => 'body too large',
NitroHttpCacheMissException() => 'nothing cached',
NitroHttpDisposedException() => 'client disposed',
NitroHttpUnknownException(:final engineErrorCode) =>
'CURLcode $engineErrorCode',
};
}
expect(detail, startsWith('timeout at'));
});
test('Streaming downloads', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.onStartStreamed = (request, body) {
scheduleMicrotask(() {
demux.push(chunk(request.requestId, utf8.encode('{"row":1}\n')));
demux.push(doneChunk(request.requestId));
});
return rawHead(requestId: request.requestId);
};
final sink = BytesBuilder();
final res = await client.requestStream(HttpMethod.get, '/dataset.ndjson');
await for (final chunk in res.body) {
sink.add(chunk); // a slow consumer stalls the socket, not the heap
}
expect(utf8.decode(sink.takeBytes()), '{"row":1}\n');
});
test('Stream chunk batching settings', () {
final client = NitroHttpClient(
settings: ClientSettings(
baseUrl: 'https://api.example.com',
streamChunks: const StreamChunkSettings.adaptive(), // the default
),
executor: executor,
demux: demux,
);
client.dispose();
const immediate = ClientSettings(
streamChunks: StreamChunkSettings.immediate(),
);
const fixed = ClientSettings(
streamChunks: StreamChunkSettings.fixed(64 * 1024),
);
expect(immediate.streamChunks.bytes, -1);
expect(fixed.streamChunks.bytes, 64 * 1024);
});
test('Streaming uploads', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.onSendBuffered = (request, body) async {
// A real server answers after the body arrives; hold the response
// until the upload pump has fed every chunk.
while (executor.finishedUploads.isEmpty) {
await Future<void>.delayed(Duration.zero);
}
return rawResponse(requestId: request.requestId);
};
const totalBytes = 8;
final source = Stream<Uint8List>.fromIterable([
Uint8List.fromList([1, 2, 3, 4]),
Uint8List.fromList([5, 6, 7, 8]),
]);
await client.post(
'/ingest',
body: HttpBody.stream(source, contentLength: totalBytes),
);
expect(
executor.uploadChunks.fold<int>(0, (n, c) => n + c.bytes.length),
totalBytes,
);
expect(executor.finishedUploads, hasLength(1));
});
test('File body', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://backups.example.com'),
);
executor.bufferedResponses.add(rawResponse());
await client.put(
'/backups/nightly.zip',
body: HttpBody.file('/var/tmp/nightly.zip'),
);
expect(
executor.bufferedRequests.single.request.bodyFilePath,
'/var/tmp/nightly.zip',
);
});
test('Downloading to a file', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://backups.example.com'),
);
executor.bufferedResponses.add(rawResponse());
final response = await client.downloadToFile(
'/backups/nightly.zip',
'/var/tmp/nightly.zip',
onReceiveProgress: (received, total) => '$received / $total',
);
// The destination travels with the request, and the response comes back
// with no body because the bytes went to disk.
expect(
executor.bufferedRequests.single.request.responseFilePath,
'/var/tmp/nightly.zip',
);
expect(response.bodyBytes, isEmpty);
});
test('Limiting response size', () async {
final client = makeClient(
settings: const ClientSettings(maxResponseBytes: 8 * 1024 * 1024),
);
executor.bufferedResponses.add(rawResponse());
await client.get('https://api.example.com/report');
expect(
executor.configs.last.maxResponseBytes,
8 * 1024 * 1024,
);
});
test('Cancellation', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
// The engine's contract: a cancel still posts exactly one completion,
// carrying the cancelled error kind. The fake plays the engine here.
final held = Completer<RawResponse>();
executor.onSendBuffered = (request, body) => held.future;
final token = CancelToken();
// README uses 2 seconds; 1 ms keeps the suite fast.
Timer(const Duration(milliseconds: 1), () {
token.cancel('user navigated away');
held.complete(
rawResponse(kind: RawErrorKind.cancelled, message: 'aborted'),
);
});
try {
await client.get('/slow', cancelToken: token);
fail('must not complete');
} on NitroHttpCancelException catch (e) {
expect(e.reason, 'user navigated away'); // user navigated away
}
});
test('Cancellation: one token, any number of requests', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses
..add(rawResponse())
..add(rawResponse())
..add(rawResponse());
final screen = CancelToken();
final results = await Future.wait([
client.get('/profile', cancelToken: screen),
client.get('/feed', cancelToken: screen),
client.get('/notifications', cancelToken: screen),
]);
expect(results, hasLength(3));
// The `dispose()` half of the snippet, minus the widget it lives in.
screen.cancel('screen closed');
// Three requests, ONE native call — the property the README claims.
expect(executor.cancelledTokens, hasLength(1));
expect(executor.cancelledTokens.single.$2, 'screen closed');
});
test('Cancellation: cancelling early keeps it off the network', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
// The engine refuses a pre-cancelled request before it opens a socket;
// the fake stands in for that verdict. `cancellation_test.dart` proves
// the real engine never reaches the server, which a fake cannot show.
executor.bufferedResponses.add(
rawResponse(kind: RawErrorKind.cancelled, message: 'aborted'),
);
final token = CancelToken()..cancel('never mind');
var refused = false;
try {
await client.get('/expensive', cancelToken: token);
} on NitroHttpCancelException {
// Fails straight away: no socket was opened and the server saw nothing.
refused = true;
}
expect(refused, isTrue);
});
test('Cancellation: cancelling twice or late is a no-op', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse());
final token = CancelToken();
await client.get('/done', cancelToken: token);
token.cancel('first');
token.cancel('second');
expect(token.reason, 'first', reason: 'the first cancel wins');
expect(executor.cancelledTokens, hasLength(1));
});
test('File body: straight from disk', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse());
await client.put('/backups/nightly.zip', body: HttpBody.file('/var/tmp/nightly.zip'));
// The path travels to the engine; no bytes pass through the Dart heap.
final sent = executor.bufferedRequests.single;
expect(sent.request.bodyKind, RawBodyKind.filePath);
expect(sent.request.bodyFilePath, '/var/tmp/nightly.zip');
expect(sent.body, isEmpty);
});
test('Progress callbacks', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
final log = <String>[];
executor.onSendBuffered = (request, body) async {
demux.pushEvent(RawEvent(
requestId: request.requestId,
kind: RawEventKind.downloadProgress,
a: 512,
b: 1024,
message: '',
));
// Let the event delivery beat the response.
await Future<void>.delayed(Duration.zero);
return rawResponse(requestId: request.requestId);
};
await client.post(
'/upload',
body: HttpBody.file('/tmp/video.mp4'),
onSendProgress: (sent, total) => log.add('$sent / ${total ?? -1}'),
onReceiveProgress: (received, total) => log.add('down $received'),
);
expect(log, contains('down 512'));
});
test('Timings', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
);
executor.bufferedResponses.add(rawResponse(
timings: const RawTimings(
queueMs: 0.01,
dnsMs: 1,
connectMs: 2,
tlsMs: 3,
firstByteMs: 9,
redirectMs: 0,
totalMs: 12,
),
));
final res = await client.get('/ping');
expect(res.timings.dns, const Duration(milliseconds: 1));
expect(res.timings.tls, const Duration(milliseconds: 3));
expect(res.timings.firstByte, const Duration(milliseconds: 9));
expect(res.timings.total, const Duration(milliseconds: 12));
});
test('TLS settings', () async {
const certPem = '-----BEGIN CERTIFICATE-----\n…\n-----END CERTIFICATE-----';
const keyPem = '-----BEGIN PRIVATE KEY-----\n…\n-----END PRIVATE KEY-----';
final client = NitroHttpClient(
settings: ClientSettings(
tlsSettings: TlsSettings(
minVersion: TlsVersion.tls13,
rootCaSource: RootCaSource.platform,
pinnedSpkiSha256: const [
'YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=',
],
clientCertificate: ClientCertificate(
certificatePem: certPem,
privateKeyPem: keyPem,
),
),
),
executor: executor,
demux: demux,
);
expect(executor.configs.single.tls.minTlsVersion, isNot(0));
executor.bufferedResponses.add(rawResponse());
await client.post(
'/payments',
options: const RequestOptions(
pinnedSpkiSha256: 'YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=',
),
);
expect(
executor.bufferedRequests.single.request.options.pinnedSpkiOverride,
isNotEmpty,
);
});
test('Proxy settings', () {
const system = ClientSettings(proxySettings: ProxySettings.system());
const none = ClientSettings(proxySettings: ProxySettings.noProxy());
final corp = ClientSettings(
proxySettings: const ProxySettings.http(
'proxy.corp.example:3128',
username: 'svc',
password: 'hunter2',
noProxy: 'localhost,127.0.0.1,*.internal',
),
);
const socks = ProxySettings.socks5('127.0.0.1:1080');
const socksRemote = ProxySettings.socks5Hostname('127.0.0.1:1080');
expect(system, isNot(none));
expect(corp.proxySettings, isNotNull);
expect(socks, isNot(socksRemote));
});
test('Timeouts: three separate deadlines', () {
const settings = ClientSettings(
connectTimeout: Duration(seconds: 10),
timeout: Duration(seconds: 30),
idleTimeout: Duration(seconds: 90),
);
// The snippet's whole point is that these are three DISTINCT deadlines,
// so assert each landed on its own field rather than that it constructs.
expect(settings.connectTimeout, const Duration(seconds: 10));
expect(settings.timeout, const Duration(seconds: 30));
expect(settings.idleTimeout, const Duration(seconds: 90));
});
test('HTTP versions and the connection pool', () {
const settings = ClientSettings(
httpVersionPref: HttpVersionPref.http2,
poolSettings: PoolSettings(
maxConnections: 64,
maxConnectionsPerHost: 6,
idleTimeout: Duration(seconds: 90),
maxLifetime: Duration(minutes: 10),
),
);
expect(settings.httpVersionPref, HttpVersionPref.http2);
expect(settings.poolSettings.maxConnections, 64);
expect(settings.poolSettings.maxConnectionsPerHost, 6);
expect(settings.poolSettings.idleTimeout, const Duration(seconds: 90));
expect(settings.poolSettings.maxLifetime, const Duration(minutes: 10));
});
test('DNS settings', () {
final pinned = ClientSettings(
dnsSettings: DnsSettings.static({
'api.example.com': ['203.0.113.10', '2001:db8::10'],
}, port: 443),
);
const doh = ClientSettings(
dnsSettings: DnsSettings.doh('https://cloudflare-dns.com/dns-query'),
);
expect(pinned.dnsSettings, isNotNull);
expect(doh.dnsSettings, isNotNull);
});
test('Cookies', () async {
const appSupportDir = '/tmp/app-support';
final client = NitroHttpClient(
settings: ClientSettings(
cookieSettings: CookieSettings(
storeCookies: true,
persistPath: '$appSupportDir/cookies.txt', // Netscape jar
),
),
executor: executor,
demux: demux,
);
executor.jar = const [
RawCookie(
name: 'session',
value: 'abc123',
domain: 'api.example.com',
path: '/',
expiresEpochMs: 0,
secure: true,
httpOnly: true,
),
];
final lines = <String>[];
for (final c in client.cookiesFor(Uri.parse('https://api.example.com/'))) {
lines.add('${c.name}=${c.value} (${c.domain}${c.path})');
}
expect(lines, ['session=abc123 (api.example.com/)']);
client.setCookie(const Cookie(
name: 'consent',
value: 'granted',
domain: 'api.example.com',
));
client.flushCookies(); // also happens automatically on dispose()
expect(executor.cookiesSet.single.name, 'consent');
expect(executor.flushCookiesCount, 1);
});
test('Interceptors', () async {
final tokens = TokenStore();
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
interceptors: [AuthInterceptor(tokens)],
);
executor.bufferedResponses.add(rawResponse());
await client.get('/users');
final sent = executor.bufferedRequests.single.request;
expect(
sent.headers.any(
(h) => h.name == 'authorization' && h.value == 'Bearer at-1',
),
isTrue,
);
// The DelegatingInterceptor shorthand.
final seen = <String>[];
final logger = DelegatingInterceptor(
onResponse: (res) async {
seen.add('${res.statusCode} ${res.finalUrl} in ${res.timings.total}');
return Interceptor.next();
},
);
final logged = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
interceptors: [logger],
);
executor.bufferedResponses.add(rawResponse());
await logged.get('/users');
expect(seen.single, startsWith('200 '));
});
test('The prebuilt interceptors', () async {
final lines = <String>[];
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
interceptors: [
LogInterceptor(level: HttpLogLevel.headers, sink: lines.add),
RetryInterceptor(maxRetries: 3),
],
);
executor.bufferedResponses.add(rawResponse());
await client.get('/users/7');
// The two lines the README prints.
expect(lines.first, '--> GET https://api.example.com/users/7');
expect(lines.any((l) => l.startsWith('<-- 200 ')), isTrue);
});
test('Keeping interceptors cheap: a synchronous hook', () async {
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
interceptors: [const TraceHeader()],
);
executor.bufferedResponses.add(rawResponse());
await client.get('/users');
final sent = executor.bufferedRequests.single.request;
expect(sent.headers.any((h) => h.name == 'x-trace-id'), isTrue);
});
test('ParallelInterceptors groups independent observers', () async {
final seen = <String>[];
final client = makeClient(
settings: const ClientSettings(baseUrl: 'https://api.example.com'),
interceptors: [
ParallelInterceptors([
DelegatingInterceptor(
onResponse: (r) async {
seen.add('log ${r.statusCode}');
return Interceptor.next();
},
),
DelegatingInterceptor(
onResponse: (r) async {
seen.add('metrics ${r.statusCode}');
return Interceptor.next();
},
),
]),
],
);
executor.bufferedResponses.add(rawResponse());
await client.get('/users');
expect(seen, containsAll(<String>['log 200', 'metrics 200']));
});
test('Retry', () {
final client = NitroHttpClient(
interceptors: [
RetryInterceptor(
maxRetries: 4,
baseDelay: const Duration(milliseconds: 250),
maxDelay: const Duration(seconds: 10),
respectRetryAfter: true,
),
],
executor: executor,
demux: demux,
);
client.dispose();
final custom = RetryInterceptor(
shouldRetry: (response, error, attempt) =>
response?.statusCode == 503 ||
RetryPolicy.isRetryableByDefault(response, error),
);
expect(custom, isA<Interceptor>());
});
test('Disk cache and prefetch', () async {
final engine = FakeEngineExecutor();
NitroHttp.overrideEngineExecutorForTesting(engine);
addTearDown(() => NitroHttp.overrideEngineExecutorForTesting(null));
const cacheDir = '/tmp/nitro-http-cache';
NitroHttp.configureCache(HttpCacheConfig(
directory: cacheDir,
maxSizeBytes: 128 * 1024 * 1024,
maxEntryBytes: 8 * 1024 * 1024,
));
expect(engine.cacheConfigs.single.maxSizeBytes, 128 * 1024 * 1024);
final client = NitroHttpClient(
settings: const ClientSettings(
cacheSettings: CacheSettings(enabled: true),
),
executor: executor,
demux: demux,
);
await NitroHttp.prefetchOnAppStart([
'https://api.example.com/v1/feed',
'https://api.example.com/v1/me',
]);
expect(engine.prefetched, hasLength(2));
executor.bufferedResponses
..add(rawResponse(fromCache: true))
..add(rawResponse());
await client.get(
'/feed',
options: const RequestOptions(cacheMode: CacheMode.onlyIfCached),
);
await client.get(
'/feed',
options: const RequestOptions(cacheMode: CacheMode.refresh),
);
final stats = NitroHttp.cacheStats();
expect('${stats.entryCount} entries, hit rate ${stats.hitRate}',
isNotEmpty);
NitroHttp.clearCache();
expect(engine.clearCacheCount, 1);
});
test('WebSockets', () async {
final wsExecutor = FakeWsExecutor();
final wsDemux = FakeWsFrameDemux();
addTearDown(wsDemux.closeAll);
final ws = await NitroWebSocket.connect(
Uri.parse('wss://echo.example.com/socket'),
protocols: ['chat'],
pingInterval: const Duration(seconds: 30),
executor: wsExecutor,
demux: wsDemux,
);
final log = <String>[];
final done = Completer<void>();
ws.events.listen((event) {
switch (event) {
case TextDataReceived(:final text):
log.add('text $text');
case BinaryDataReceived(:final data):
log.add('binary ${data.length}');
case CloseReceived(:final code, :final reason):
log.add('closed $code $reason');
done.complete();
}
});