Skip to content

Commit b058532

Browse files
authored
Add background sessions tests (#765)
1 parent e6dc3e6 commit b058532

File tree

3 files changed

+64
-35
lines changed

3 files changed

+64
-35
lines changed

pkgs/cupertino_http/example/integration_test/url_session_delegate_test.dart

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'package:cupertino_http/cupertino_http.dart';
99
import 'package:integration_test/integration_test.dart';
1010
import 'package:test/test.dart';
1111

12-
void testOnComplete() {
12+
void testOnComplete(URLSessionConfiguration config) {
1313
group('onComplete', () {
1414
late HttpServer server;
1515

@@ -32,9 +32,8 @@ void testOnComplete() {
3232
late URLSession actualSession;
3333
late URLSessionTask actualTask;
3434

35-
final session = URLSession.sessionWithConfiguration(
36-
URLSessionConfiguration.defaultSessionConfiguration(),
37-
onComplete: (s, t, e) {
35+
final session =
36+
URLSession.sessionWithConfiguration(config, onComplete: (s, t, e) {
3837
actualSession = s;
3938
actualTask = t;
4039
actualError = e;
@@ -57,9 +56,8 @@ void testOnComplete() {
5756
late URLSession actualSession;
5857
late URLSessionTask actualTask;
5958

60-
final session = URLSession.sessionWithConfiguration(
61-
URLSessionConfiguration.defaultSessionConfiguration(),
62-
onComplete: (s, t, e) {
59+
final session =
60+
URLSession.sessionWithConfiguration(config, onComplete: (s, t, e) {
6361
actualSession = s;
6462
actualTask = t;
6563
actualError = e;
@@ -72,12 +70,17 @@ void testOnComplete() {
7270
await c.future;
7371
expect(actualSession, session);
7472
expect(actualTask, task);
75-
expect(actualError!.code, -1003); // kCFURLErrorCannotFindHost
73+
expect(
74+
actualError!.code,
75+
anyOf(
76+
-1001, // kCFURLErrorTimedOut
77+
-1003, // kCFURLErrorCannotFindHost
78+
));
7679
});
7780
});
7881
}
7982

80-
void testOnResponse() {
83+
void testOnResponse(URLSessionConfiguration config) {
8184
group('onResponse', () {
8285
late HttpServer server;
8386

@@ -100,9 +103,8 @@ void testOnResponse() {
100103
late URLSession actualSession;
101104
late URLSessionTask actualTask;
102105

103-
final session = URLSession.sessionWithConfiguration(
104-
URLSessionConfiguration.defaultSessionConfiguration(),
105-
onResponse: (s, t, r) {
106+
final session =
107+
URLSession.sessionWithConfiguration(config, onResponse: (s, t, r) {
106108
actualSession = s;
107109
actualTask = t;
108110
actualResponse = r as HTTPURLResponse;
@@ -124,8 +126,7 @@ void testOnResponse() {
124126
final c = Completer<void>();
125127
var called = false;
126128

127-
final session = URLSession.sessionWithConfiguration(
128-
URLSessionConfiguration.defaultSessionConfiguration(),
129+
final session = URLSession.sessionWithConfiguration(config,
129130
onComplete: (session, task, error) => c.complete(),
130131
onResponse: (s, t, r) {
131132
called = true;
@@ -142,7 +143,7 @@ void testOnResponse() {
142143
});
143144
}
144145

145-
void testOnData() {
146+
void testOnData(URLSessionConfiguration config) {
146147
group('onData', () {
147148
late HttpServer server;
148149

@@ -165,8 +166,7 @@ void testOnData() {
165166
late URLSession actualSession;
166167
late URLSessionTask actualTask;
167168

168-
final session = URLSession.sessionWithConfiguration(
169-
URLSessionConfiguration.defaultSessionConfiguration(),
169+
final session = URLSession.sessionWithConfiguration(config,
170170
onComplete: (s, t, r) => c.complete(),
171171
onData: (s, t, d) {
172172
actualSession = s;
@@ -185,7 +185,7 @@ void testOnData() {
185185
});
186186
}
187187

188-
void testOnFinishedDownloading() {
188+
void testOnFinishedDownloading(URLSessionConfiguration config) {
189189
group('onFinishedDownloading', () {
190190
late HttpServer server;
191191

@@ -208,8 +208,7 @@ void testOnFinishedDownloading() {
208208
late URLSessionDownloadTask actualTask;
209209
late String actualContent;
210210

211-
final session = URLSession.sessionWithConfiguration(
212-
URLSessionConfiguration.defaultSessionConfiguration(),
211+
final session = URLSession.sessionWithConfiguration(config,
213212
onComplete: (s, t, r) => c.complete(),
214213
onFinishedDownloading: (s, t, uri) {
215214
actualSession = s;
@@ -228,7 +227,7 @@ void testOnFinishedDownloading() {
228227
});
229228
}
230229

231-
void testOnRedirect() {
230+
void testOnRedirect(URLSessionConfiguration config) {
232231
group('onRedirect', () {
233232
late HttpServer redirectServer;
234233

@@ -257,7 +256,6 @@ void testOnRedirect() {
257256
});
258257

259258
test('disallow redirect', () async {
260-
final config = URLSessionConfiguration.defaultSessionConfiguration();
261259
final session = URLSession.sessionWithConfiguration(config,
262260
onRedirect:
263261
(redirectSession, redirectTask, redirectResponse, newRequest) =>
@@ -283,7 +281,6 @@ void testOnRedirect() {
283281
});
284282

285283
test('use preposed redirect request', () async {
286-
final config = URLSessionConfiguration.defaultSessionConfiguration();
287284
final session = URLSession.sessionWithConfiguration(config,
288285
onRedirect:
289286
(redirectSession, redirectTask, redirectResponse, newRequest) =>
@@ -307,7 +304,6 @@ void testOnRedirect() {
307304
});
308305

309306
test('use custom redirect request', () async {
310-
final config = URLSessionConfiguration.defaultSessionConfiguration();
311307
final session = URLSession.sessionWithConfiguration(
312308
config,
313309
onRedirect: (redirectSession, redirectTask, redirectResponse,
@@ -334,7 +330,6 @@ void testOnRedirect() {
334330
});
335331

336332
test('exception in http redirection', () async {
337-
final config = URLSessionConfiguration.defaultSessionConfiguration();
338333
final session = URLSession.sessionWithConfiguration(
339334
config,
340335
onRedirect:
@@ -363,7 +358,6 @@ void testOnRedirect() {
363358
}, skip: 'Error not set for redirect exceptions.');
364359

365360
test('3 redirects', () async {
366-
final config = URLSessionConfiguration.defaultSessionConfiguration();
367361
var redirectCounter = 0;
368362
final session = URLSession.sessionWithConfiguration(
369363
config,
@@ -415,7 +409,6 @@ void testOnRedirect() {
415409
test('allow too many redirects', () async {
416410
// The Foundation URL Loading System limits the number of redirects
417411
// even when a redirect delegate is present and allows the redirect.
418-
final config = URLSessionConfiguration.defaultSessionConfiguration();
419412
final session = URLSession.sessionWithConfiguration(
420413
config,
421414
onRedirect:
@@ -445,9 +438,31 @@ void testOnRedirect() {
445438
void main() {
446439
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
447440

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+
});
453468
}

pkgs/cupertino_http/example/integration_test/url_session_test.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void main() {
219219
group('sharedSession', () {
220220
final session = URLSession.sharedSession();
221221

222-
test('configration', () {
222+
test('configuration', () {
223223
expect(session.configuration, isA<URLSessionConfiguration>());
224224
});
225225

@@ -231,7 +231,20 @@ void main() {
231231
..allowsCellularAccess = false;
232232
final session = URLSession.sessionWithConfiguration(config);
233233

234-
test('configration', () {
234+
test('configuration', () {
235+
expect(session.configuration.allowsCellularAccess, false);
236+
});
237+
238+
testURLSession(session);
239+
});
240+
241+
group('backgroundSession', () {
242+
final config =
243+
URLSessionConfiguration.backgroundSession('backgroundSession')
244+
..allowsCellularAccess = false;
245+
final session = URLSession.sessionWithConfiguration(config);
246+
247+
test('configuration', () {
235248
expect(session.configuration.allowsCellularAccess, false);
236249
});
237250

pkgs/cupertino_http/lib/cupertino_http.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,8 @@ class URLSession extends _ObjectHolder<ncb.NSURLSession> {
841841
/// a follow-up request that would honor the server's redirect. If the return
842842
/// value of this function is `null` then the redirect will not occur.
843843
/// Otherwise, the returned [URLRequest] (usually `newRequest`) will be
844-
/// executed. See
844+
/// executed. [onRedirect] will not be called for background sessions, which
845+
/// automatically follow redirects. See
845846
/// [URLSession:task:willPerformHTTPRedirection:newRequest:completionHandler:](https://developer.apple.com/documentation/foundation/nsurlsessiontaskdelegate/1411626-urlsession)
846847
///
847848
/// If [onResponse] is set then it will be called whenever a valid response

0 commit comments

Comments
 (0)