platform/packages/static/test/cache_test.dart

77 lines
2.3 KiB
Dart
Raw Normal View History

2018-10-21 00:24:44 +00:00
import 'dart:io' show HttpDate;
2017-02-27 00:19:34 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-11-13 21:25:17 +00:00
import 'package:angel_framework/http.dart';
2017-02-27 00:19:34 +00:00
import 'package:angel_static/angel_static.dart';
2017-09-23 21:57:54 +00:00
import 'package:file/local.dart';
2017-02-27 00:19:34 +00:00
import 'package:http/http.dart' show Client;
2017-09-24 05:02:10 +00:00
import 'package:logging/logging.dart';
2017-02-27 00:19:34 +00:00
import 'package:matcher/matcher.dart';
import 'package:test/test.dart';
2021-02-21 02:47:23 +00:00
void main() {
2017-02-27 00:19:34 +00:00
Angel app;
AngelHttp http;
2021-02-21 02:47:23 +00:00
var testDir = const LocalFileSystem().directory('test');
2017-02-27 00:19:34 +00:00
String url;
2021-02-21 02:47:23 +00:00
var client = Client();
2017-02-27 00:19:34 +00:00
setUp(() async {
2019-05-02 23:29:09 +00:00
app = Angel();
http = AngelHttp(app);
2017-02-27 00:19:34 +00:00
2018-08-28 14:58:28 +00:00
app.fallback(
2019-05-02 23:29:09 +00:00
CachingVirtualDirectory(app, const LocalFileSystem(),
2017-09-23 21:57:54 +00:00
source: testDir, maxAge: 350, onlyInProduction: false,
//publicPath: '/virtual',
indexFileNames: ['index.txt']).handleRequest,
);
2017-02-27 00:19:34 +00:00
2018-08-28 14:58:28 +00:00
app.get('*', (req, res) => 'Fallback');
2017-02-27 00:19:34 +00:00
app.dumpTree(showMatchers: true);
2019-05-02 23:29:09 +00:00
app.logger = Logger('angel_static')
2017-09-24 05:02:10 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
var server = await http.startServer();
2021-02-21 02:47:23 +00:00
url = 'http://${server.address.host}:${server.port}';
2017-02-27 00:19:34 +00:00
});
tearDown(() async {
2018-11-13 21:25:17 +00:00
if (http.server != null) await http.server.close(force: true);
2017-02-27 00:19:34 +00:00
});
test('sets etag, cache-control, expires, last-modified', () async {
2021-02-21 02:47:23 +00:00
var response = await client.get('$url');
2017-02-27 00:19:34 +00:00
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
print('Response headers: ${response.headers}');
expect(response.statusCode, equals(200));
expect(
2017-09-24 05:02:10 +00:00
['etag', 'cache-control', 'expires', 'last-modified'],
2017-02-27 00:19:34 +00:00
everyElement(predicate(
response.headers.containsKey, 'contained in response headers')));
});
test('if-modified-since', () async {
2021-02-21 02:47:23 +00:00
var response = await client.get('$url', headers: {
2017-09-23 21:57:54 +00:00
'if-modified-since':
2019-05-02 23:29:09 +00:00
HttpDate.format(DateTime.now().add(Duration(days: 365)))
2017-02-27 00:19:34 +00:00
});
print('Response status: ${response.statusCode}');
expect(response.statusCode, equals(304));
expect(
2017-09-23 21:57:54 +00:00
['cache-control', 'expires', 'last-modified'],
2017-02-27 00:19:34 +00:00
everyElement(predicate(
response.headers.containsKey, 'contained in response headers')));
});
}