platform/packages/cache/test/cache_test.dart

109 lines
3.4 KiB
Dart
Raw Normal View History

2018-04-02 02:29:36 +00:00
import 'dart:async';
2018-10-21 09:35:04 +00:00
import 'dart:io';
import 'package:angel3_cache/angel3_cache.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_test/angel3_test.dart';
2018-04-02 02:29:36 +00:00
import 'package:http/http.dart' as http;
import 'package:glob/glob.dart';
import 'package:test/test.dart';
main() async {
group('no timeout', () {
late TestClient client;
late DateTime lastModified;
late http.Response response1, response2;
2018-04-02 02:29:36 +00:00
setUp(() async {
var app = new Angel();
var cache = new ResponseCache()
..patterns.addAll([
new Glob('/*.txt'),
]);
2018-10-21 09:35:04 +00:00
app.fallback(cache.handleRequest);
2018-04-02 02:29:36 +00:00
2018-10-21 09:35:04 +00:00
app.get('/date.txt', (req, res) {
res
..useBuffer()
..write(new DateTime.now().toIso8601String());
});
2018-04-02 02:29:36 +00:00
2018-10-21 09:35:04 +00:00
app.addRoute('PURGE', '*', (req, res) {
cache.purge(req.uri!.path);
print('Purged ${req.uri!.path}');
2018-04-02 02:29:36 +00:00
});
app.responseFinalizers.add(cache.responseFinalizer);
var oldHandler = app.errorHandler;
app.errorHandler = (e, req, res) {
if (e.error == null) return oldHandler(e, req, res);
return Zone.current
.handleUncaughtError(e.error as Object, e.stackTrace!);
2018-04-02 02:29:36 +00:00
};
client = await connectTo(app);
2021-03-13 00:17:38 +00:00
response1 = await client.get(Uri.parse('/date.txt'));
response2 = await client.get(Uri.parse('/date.txt'));
2018-10-21 09:35:04 +00:00
print(response2.headers);
lastModified = HttpDate.parse(response2.headers['last-modified']!);
2018-04-02 02:29:36 +00:00
print('Response 1 status: ${response1.statusCode}');
print('Response 2 status: ${response2.statusCode}');
print('Response 1 body: ${response1.body}');
print('Response 2 body: ${response2.body}');
print('Response 1 headers: ${response1.headers}');
print('Response 2 headers: ${response2.headers}');
});
tearDown(() => client.close());
test('saves content', () async {
expect(response1.body, response2.body);
});
test('saves headers', () async {
response1.headers.forEach((k, v) {
expect(response2.headers, containsPair(k, v));
});
});
test('first response is normal', () {
expect(response1.statusCode, 200);
});
test('sends last-modified', () {
expect(response2.headers.keys, contains('last-modified'));
});
test('invalidate', () async {
2018-04-02 15:23:17 +00:00
await client.sendUnstreamed('PURGE', '/date.txt', {});
2021-03-13 00:17:38 +00:00
var response = await client.get(Uri.parse('/date.txt'));
2018-04-02 02:29:36 +00:00
print('Response after invalidation: ${response.body}');
expect(response.body, isNot(response1.body));
});
test('sends 304 on if-modified-since', () async {
2018-10-21 09:35:04 +00:00
var headers = {
'if-modified-since':
HttpDate.format(lastModified.add(const Duration(days: 1)))
};
2021-03-13 00:17:38 +00:00
var response = await client.get(Uri.parse('/date.txt'), headers: headers);
2018-04-02 15:23:17 +00:00
print('Sending headers: $headers');
2018-04-02 15:20:18 +00:00
print('Response (${response.statusCode}): ${response.headers}');
2018-04-02 02:29:36 +00:00
expect(response.statusCode, 304);
});
test('last-modified in the past', () async {
2021-03-13 00:17:38 +00:00
var response = await client.get(Uri.parse('/date.txt'), headers: {
2018-04-02 02:29:36 +00:00
'if-modified-since':
2018-10-21 09:35:04 +00:00
HttpDate.format(lastModified.subtract(const Duration(days: 10)))
2018-04-02 02:29:36 +00:00
});
print('Response: ${response.body}');
expect(response.statusCode, 200);
expect(response.body, isNot(response1.body));
});
});
group('with timeout', () {});
}