From ea3d209a936c8e86d80669c6edf2b1b87e724332 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Thu, 15 Jun 2017 23:30:54 -0400 Subject: [PATCH] Run twice --- lib/src/cache.dart | 7 +++--- lib/src/file_info.dart | 7 ++++-- pubspec.yaml | 3 ++- test/cache_test.dart | 2 +- test/foo.mustache | 1 + test/transformer_test.dart | 47 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 test/foo.mustache diff --git a/lib/src/cache.dart b/lib/src/cache.dart index 8e093733..599ebf0f 100644 --- a/lib/src/cache.dart +++ b/lib/src/cache.dart @@ -130,7 +130,7 @@ class CachingVirtualDirectory extends VirtualDirectory { } } - if (req.headers[HttpHeaders.IF_MODIFIED_SINCE] != null) { + if (req.headers.ifModifiedSince != null) { try { var ifModifiedSince = req.headers.ifModifiedSince; @@ -225,11 +225,12 @@ class CachingVirtualDirectory extends VirtualDirectory { } } - if (req.headers[HttpHeaders.IF_MODIFIED_SINCE] != null) { + if (req.headers.ifModifiedSince != null) { try { var ifModifiedSince = req.headers.ifModifiedSince; - if (ifModifiedSince.compareTo(fileInfo.lastModified) >= 0) { + if (fileInfo.lastModified != null && + ifModifiedSince.compareTo(fileInfo.lastModified) >= 0) { res.statusCode = HttpStatus.NOT_MODIFIED; setCachedHeaders(fileInfo.lastModified, req, res); diff --git a/lib/src/file_info.dart b/lib/src/file_info.dart index 2e1a6c16..095bd198 100644 --- a/lib/src/file_info.dart +++ b/lib/src/file_info.dart @@ -69,8 +69,11 @@ class _FileInfoImpl implements FileInfo { String get extension => p.extension(filename); @override - FileInfo changeFilename(String newFilename) => - new _FileInfoImpl(getContent, newFilename, mimeType, lastModified); + FileInfo changeFilename(String newFilename) => new _FileInfoImpl( + getContent, + newFilename, + lookupMimeType(newFilename) ?? mimeType ?? 'application/octet-stream', + lastModified); @override FileInfo changeExtension(String newExtension) => diff --git a/pubspec.yaml b/pubspec.yaml index 013b9cf7..4e4bebf2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ environment: sdk: ">=1.19.0" homepage: https://github.com/angel-dart/static author: Tobe O -version: 1.2.2+3 +version: 1.2.2+4 dependencies: angel_framework: ^1.0.0-dev cli_util: ^0.1.1 @@ -18,4 +18,5 @@ dev_dependencies: angel_diagnostics: ^1.0.0 angel_test: ^1.0.0 http: ^0.11.3 + mustache4dart: ^1.1.0 test: ^0.12.13 diff --git a/test/cache_test.dart b/test/cache_test.dart index b7ed5432..83225fa0 100644 --- a/test/cache_test.dart +++ b/test/cache_test.dart @@ -53,7 +53,7 @@ main() { test('if-modified-since', () async { var response = await client.get("$url", headers: { HttpHeaders.IF_MODIFIED_SINCE: - formatDateForHttp(new DateTime.now()..add(new Duration(days: 365))) + formatDateForHttp(new DateTime.now().add(new Duration(days: 365))) }); print('Response status: ${response.statusCode}'); diff --git a/test/foo.mustache b/test/foo.mustache new file mode 100644 index 00000000..f0521e3f --- /dev/null +++ b/test/foo.mustache @@ -0,0 +1 @@ +

{{foo}}

\ No newline at end of file diff --git a/test/transformer_test.dart b/test/transformer_test.dart index 69b43548..495fdf64 100644 --- a/test/transformer_test.dart +++ b/test/transformer_test.dart @@ -1,12 +1,14 @@ import 'dart:async'; +import 'dart:convert'; import 'dart:io'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_static/angel_static.dart'; import 'package:angel_test/angel_test.dart'; +import 'package:mustache4dart/mustache4dart.dart' as ms; import 'package:test/test.dart'; main() { - TestClient client; + TestClient client, client2; setUp(() async { var app = new Angel(); @@ -18,15 +20,39 @@ main() { print('Loaded transformer map: $map'); }); client = await connectTo(app); + + var app2 = new Angel(); + var vDir2 = new CachingVirtualDirectory( + source: new Directory('test'), + transformers: [ + new MustacheTransformer({'foo': 'bar'}) + ]); + await app2.configure(vDir2); + await vDir2.transformersLoaded.then((map) { + print('Loaded transformer map2: $map'); + }); + client2 = await connectTo(app2); }); - tearDown(() => client.close()); + tearDown(() => client.close().then((_) => client2.close())); test('foo', () async { var response = await client.get('/index.ext'); print('Response: ${response.body}'); expect(response, hasBody('.txt')); }); + + test('request twice in a row', () async { + var response = await client2.get('/foo.html'); + print('Response: ${response.body}'); + print('Response headers: ${response.headers}'); + expect(response, hasBody('

bar

')); + + var response2 = await client2.get('/foo.html'); + expect(response2, hasHeader(HttpHeaders.CONTENT_TYPE, ContentType.HTML.mimeType)); + print('Response2: ${response2.body}'); + expect(response2, hasBody('

bar

')); + }); } class ExtensionTransformer implements FileTransformer { @@ -39,3 +65,20 @@ class ExtensionTransformer implements FileTransformer { FutureOr transform(FileInfo file) => file.changeText(file.extension).changeExtension('.ext'); } + +class MustacheTransformer implements FileTransformer { + final Map locals; + + MustacheTransformer(this.locals); + + @override + FileInfo declareOutput(FileInfo file) => + file.extension == '.mustache' ? file.changeExtension('.html') : null; + + @override + FutureOr transform(FileInfo file) async { + var template = await file.content.transform(UTF8.decoder).join(); + var compiled = ms.render(template, locals ?? {}); + return file.changeExtension('.html').changeText(compiled); + } +}