@@ -9,7 +9,7 @@ import 'package:cupertino_http/cupertino_http.dart';
9
9
import 'package:integration_test/integration_test.dart' ;
10
10
import 'package:test/test.dart' ;
11
11
12
- void testOnComplete () {
12
+ void testOnComplete (URLSessionConfiguration config ) {
13
13
group ('onComplete' , () {
14
14
late HttpServer server;
15
15
@@ -32,9 +32,8 @@ void testOnComplete() {
32
32
late URLSession actualSession;
33
33
late URLSessionTask actualTask;
34
34
35
- final session = URLSession .sessionWithConfiguration (
36
- URLSessionConfiguration .defaultSessionConfiguration (),
37
- onComplete: (s, t, e) {
35
+ final session =
36
+ URLSession .sessionWithConfiguration (config, onComplete: (s, t, e) {
38
37
actualSession = s;
39
38
actualTask = t;
40
39
actualError = e;
@@ -57,9 +56,8 @@ void testOnComplete() {
57
56
late URLSession actualSession;
58
57
late URLSessionTask actualTask;
59
58
60
- final session = URLSession .sessionWithConfiguration (
61
- URLSessionConfiguration .defaultSessionConfiguration (),
62
- onComplete: (s, t, e) {
59
+ final session =
60
+ URLSession .sessionWithConfiguration (config, onComplete: (s, t, e) {
63
61
actualSession = s;
64
62
actualTask = t;
65
63
actualError = e;
@@ -72,12 +70,17 @@ void testOnComplete() {
72
70
await c.future;
73
71
expect (actualSession, session);
74
72
expect (actualTask, task);
75
- expect (actualError! .code, - 1003 ); // kCFURLErrorCannotFindHost
73
+ expect (
74
+ actualError! .code,
75
+ anyOf (
76
+ - 1001 , // kCFURLErrorTimedOut
77
+ - 1003 , // kCFURLErrorCannotFindHost
78
+ ));
76
79
});
77
80
});
78
81
}
79
82
80
- void testOnResponse () {
83
+ void testOnResponse (URLSessionConfiguration config ) {
81
84
group ('onResponse' , () {
82
85
late HttpServer server;
83
86
@@ -100,9 +103,8 @@ void testOnResponse() {
100
103
late URLSession actualSession;
101
104
late URLSessionTask actualTask;
102
105
103
- final session = URLSession .sessionWithConfiguration (
104
- URLSessionConfiguration .defaultSessionConfiguration (),
105
- onResponse: (s, t, r) {
106
+ final session =
107
+ URLSession .sessionWithConfiguration (config, onResponse: (s, t, r) {
106
108
actualSession = s;
107
109
actualTask = t;
108
110
actualResponse = r as HTTPURLResponse ;
@@ -124,8 +126,7 @@ void testOnResponse() {
124
126
final c = Completer <void >();
125
127
var called = false ;
126
128
127
- final session = URLSession .sessionWithConfiguration (
128
- URLSessionConfiguration .defaultSessionConfiguration (),
129
+ final session = URLSession .sessionWithConfiguration (config,
129
130
onComplete: (session, task, error) => c.complete (),
130
131
onResponse: (s, t, r) {
131
132
called = true ;
@@ -142,7 +143,7 @@ void testOnResponse() {
142
143
});
143
144
}
144
145
145
- void testOnData () {
146
+ void testOnData (URLSessionConfiguration config ) {
146
147
group ('onData' , () {
147
148
late HttpServer server;
148
149
@@ -165,8 +166,7 @@ void testOnData() {
165
166
late URLSession actualSession;
166
167
late URLSessionTask actualTask;
167
168
168
- final session = URLSession .sessionWithConfiguration (
169
- URLSessionConfiguration .defaultSessionConfiguration (),
169
+ final session = URLSession .sessionWithConfiguration (config,
170
170
onComplete: (s, t, r) => c.complete (),
171
171
onData: (s, t, d) {
172
172
actualSession = s;
@@ -185,7 +185,7 @@ void testOnData() {
185
185
});
186
186
}
187
187
188
- void testOnFinishedDownloading () {
188
+ void testOnFinishedDownloading (URLSessionConfiguration config ) {
189
189
group ('onFinishedDownloading' , () {
190
190
late HttpServer server;
191
191
@@ -208,8 +208,7 @@ void testOnFinishedDownloading() {
208
208
late URLSessionDownloadTask actualTask;
209
209
late String actualContent;
210
210
211
- final session = URLSession .sessionWithConfiguration (
212
- URLSessionConfiguration .defaultSessionConfiguration (),
211
+ final session = URLSession .sessionWithConfiguration (config,
213
212
onComplete: (s, t, r) => c.complete (),
214
213
onFinishedDownloading: (s, t, uri) {
215
214
actualSession = s;
@@ -228,7 +227,7 @@ void testOnFinishedDownloading() {
228
227
});
229
228
}
230
229
231
- void testOnRedirect () {
230
+ void testOnRedirect (URLSessionConfiguration config ) {
232
231
group ('onRedirect' , () {
233
232
late HttpServer redirectServer;
234
233
@@ -257,7 +256,6 @@ void testOnRedirect() {
257
256
});
258
257
259
258
test ('disallow redirect' , () async {
260
- final config = URLSessionConfiguration .defaultSessionConfiguration ();
261
259
final session = URLSession .sessionWithConfiguration (config,
262
260
onRedirect:
263
261
(redirectSession, redirectTask, redirectResponse, newRequest) =>
@@ -283,7 +281,6 @@ void testOnRedirect() {
283
281
});
284
282
285
283
test ('use preposed redirect request' , () async {
286
- final config = URLSessionConfiguration .defaultSessionConfiguration ();
287
284
final session = URLSession .sessionWithConfiguration (config,
288
285
onRedirect:
289
286
(redirectSession, redirectTask, redirectResponse, newRequest) =>
@@ -307,7 +304,6 @@ void testOnRedirect() {
307
304
});
308
305
309
306
test ('use custom redirect request' , () async {
310
- final config = URLSessionConfiguration .defaultSessionConfiguration ();
311
307
final session = URLSession .sessionWithConfiguration (
312
308
config,
313
309
onRedirect: (redirectSession, redirectTask, redirectResponse,
@@ -334,7 +330,6 @@ void testOnRedirect() {
334
330
});
335
331
336
332
test ('exception in http redirection' , () async {
337
- final config = URLSessionConfiguration .defaultSessionConfiguration ();
338
333
final session = URLSession .sessionWithConfiguration (
339
334
config,
340
335
onRedirect:
@@ -363,7 +358,6 @@ void testOnRedirect() {
363
358
}, skip: 'Error not set for redirect exceptions.' );
364
359
365
360
test ('3 redirects' , () async {
366
- final config = URLSessionConfiguration .defaultSessionConfiguration ();
367
361
var redirectCounter = 0 ;
368
362
final session = URLSession .sessionWithConfiguration (
369
363
config,
@@ -415,7 +409,6 @@ void testOnRedirect() {
415
409
test ('allow too many redirects' , () async {
416
410
// The Foundation URL Loading System limits the number of redirects
417
411
// even when a redirect delegate is present and allows the redirect.
418
- final config = URLSessionConfiguration .defaultSessionConfiguration ();
419
412
final session = URLSession .sessionWithConfiguration (
420
413
config,
421
414
onRedirect:
@@ -445,9 +438,31 @@ void testOnRedirect() {
445
438
void main () {
446
439
IntegrationTestWidgetsFlutterBinding .ensureInitialized ();
447
440
448
- testOnComplete ();
449
- testOnResponse ();
450
- testOnData ();
451
- testOnRedirect ();
452
- testOnFinishedDownloading ();
441
+ group ('backgroundSession' , () {
442
+ final config =
443
+ URLSessionConfiguration .backgroundSession ('backgroundSession' );
444
+ testOnComplete (config);
445
+ testOnResponse (config);
446
+ testOnData (config);
447
+ // onRedirect is not called for background sessions.
448
+ testOnFinishedDownloading (config);
449
+ });
450
+
451
+ group ('defaultSessionConfiguration' , () {
452
+ final config = URLSessionConfiguration .defaultSessionConfiguration ();
453
+ testOnComplete (config);
454
+ testOnResponse (config);
455
+ testOnData (config);
456
+ testOnRedirect (config);
457
+ testOnFinishedDownloading (config);
458
+ });
459
+
460
+ group ('ephemeralSessionConfiguration' , () {
461
+ final config = URLSessionConfiguration .ephemeralSessionConfiguration ();
462
+ testOnComplete (config);
463
+ testOnResponse (config);
464
+ testOnData (config);
465
+ testOnRedirect (config);
466
+ testOnFinishedDownloading (config);
467
+ });
453
468
}
0 commit comments