Run twice

This commit is contained in:
thosakwe 2017-06-15 23:30:54 -04:00
parent 21f031c207
commit ea3d209a93
6 changed files with 58 additions and 9 deletions

View file

@ -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);

View file

@ -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) =>

View file

@ -4,7 +4,7 @@ environment:
sdk: ">=1.19.0"
homepage: https://github.com/angel-dart/static
author: Tobe O <thosakwe@gmail.com>
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

View file

@ -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}');

1
test/foo.mustache Normal file
View file

@ -0,0 +1 @@
<h1>{{foo}}</h1>

View file

@ -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('<h1>bar</h1>'));
var response2 = await client2.get('/foo.html');
expect(response2, hasHeader(HttpHeaders.CONTENT_TYPE, ContentType.HTML.mimeType));
print('Response2: ${response2.body}');
expect(response2, hasBody('<h1>bar</h1>'));
});
}
class ExtensionTransformer implements FileTransformer {
@ -39,3 +65,20 @@ class ExtensionTransformer implements FileTransformer {
FutureOr<FileInfo> transform(FileInfo file) =>
file.changeText(file.extension).changeExtension('.ext');
}
class MustacheTransformer implements FileTransformer {
final Map<String, dynamic> locals;
MustacheTransformer(this.locals);
@override
FileInfo declareOutput(FileInfo file) =>
file.extension == '.mustache' ? file.changeExtension('.html') : null;
@override
FutureOr<FileInfo> 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);
}
}