platform/test/cache_test.dart

74 lines
2.2 KiB
Dart
Raw Normal View History

2017-02-27 00:19:34 +00:00
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_static/angel_static.dart';
2017-09-23 21:57:54 +00:00
import 'package:file/file.dart';
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';
main() {
Angel app;
2017-09-23 21:57:54 +00:00
Directory testDir = const LocalFileSystem().directory('test');
2017-02-27 00:19:34 +00:00
String url;
Client client = new Client();
setUp(() async {
2017-09-23 21:57:54 +00:00
app = new Angel();
2017-02-27 00:19:34 +00:00
2017-09-23 21:57:54 +00:00
app.use(
new CachingVirtualDirectory(app, const LocalFileSystem(),
source: testDir, maxAge: 350, onlyInProduction: false,
//publicPath: '/virtual',
indexFileNames: ['index.txt']).handleRequest,
);
2017-02-27 00:19:34 +00:00
app.get('*', 'Fallback');
app.dumpTree(showMatchers: true);
2017-09-24 05:02:10 +00:00
app.logger = new Logger('angel_static')
..onRecord.listen((rec) {
print(rec);
if (rec.error != null) print(rec.error);
if (rec.stackTrace != null) print(rec.stackTrace);
});
2017-09-23 21:57:54 +00:00
var server = await app.startServer();
url = "http://${server.address.host}:${server.port}";
2017-02-27 00:19:34 +00:00
});
tearDown(() async {
if (app.httpServer != null) await app.httpServer.close(force: true);
});
test('sets etag, cache-control, expires, last-modified', () async {
var response = await client.get("$url");
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 {
var response = await client.get("$url", headers: {
2017-09-23 21:57:54 +00:00
'if-modified-since':
2017-06-16 03:30:54 +00:00
formatDateForHttp(new DateTime.now().add(new 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')));
});
}