-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to control the URL cache. (#1020)
- Loading branch information
1 parent
decefa6
commit 1251619
Showing
8 changed files
with
5,585 additions
and
4,720 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
pkgs/cupertino_http/example/integration_test/url_cache_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:io'; | ||
|
||
import 'package:cupertino_http/cupertino_http.dart'; | ||
import 'package:integration_test/integration_test.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
group('dataTaskWithCompletionHandler', () { | ||
late HttpServer server; | ||
var uncachedRequestCount = 0; | ||
|
||
setUp(() async { | ||
uncachedRequestCount = 0; | ||
server = (await HttpServer.bind('localhost', 0)) | ||
..listen((request) async { | ||
await request.drain<void>(); | ||
if (request.headers['if-none-match']?.first == '1234') { | ||
request.response.statusCode = 304; | ||
await request.response.close(); | ||
return; | ||
} | ||
++uncachedRequestCount; | ||
request.response.headers.set('Content-Type', 'text/plain'); | ||
request.response.headers.set('ETag', '1234'); | ||
request.response.write('Hello World'); | ||
await request.response.close(); | ||
}); | ||
}); | ||
tearDown(() { | ||
server.close(); | ||
}); | ||
|
||
Future<void> doRequest(URLSession session) { | ||
final request = | ||
URLRequest.fromUrl(Uri.parse('http://localhost:${server.port}')); | ||
final c = Completer<void>(); | ||
session.dataTaskWithCompletionHandler(request, (d, r, e) { | ||
c.complete(); | ||
}).resume(); | ||
return c.future; | ||
} | ||
|
||
test('no cache', () async { | ||
final config = URLSessionConfiguration.defaultSessionConfiguration() | ||
..cache = null; | ||
final session = URLSession.sessionWithConfiguration(config); | ||
|
||
await doRequest(session); | ||
await doRequest(session); | ||
|
||
expect(uncachedRequestCount, 2); | ||
}); | ||
|
||
test('with cache', () async { | ||
final config = URLSessionConfiguration.defaultSessionConfiguration() | ||
..cache = URLCache.withCapacity(memoryCapacity: 100000); | ||
final session = URLSession.sessionWithConfiguration(config); | ||
|
||
await doRequest(session); | ||
await doRequest(session); | ||
|
||
expect(uncachedRequestCount, 1); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.