platform/test/transformer_test.dart

85 lines
2.5 KiB
Dart
Raw Normal View History

2017-06-16 02:05:06 +00:00
import 'dart:async';
2017-06-16 03:30:54 +00:00
import 'dart:convert';
2017-06-16 02:05:06 +00:00
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_static/angel_static.dart';
import 'package:angel_test/angel_test.dart';
2017-06-16 03:30:54 +00:00
import 'package:mustache4dart/mustache4dart.dart' as ms;
2017-06-16 02:05:06 +00:00
import 'package:test/test.dart';
main() {
2017-06-16 03:30:54 +00:00
TestClient client, client2;
2017-06-16 02:05:06 +00:00
setUp(() async {
var app = new Angel();
var vDir = new CachingVirtualDirectory(
source: new Directory('test'),
transformers: [new ExtensionTransformer()]);
await app.configure(vDir);
await vDir.transformersLoaded.then((map) {
print('Loaded transformer map: $map');
});
client = await connectTo(app);
2017-06-16 03:30:54 +00:00
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);
2017-06-16 02:05:06 +00:00
});
2017-06-16 03:30:54 +00:00
tearDown(() => client.close().then((_) => client2.close()));
2017-06-16 02:05:06 +00:00
test('foo', () async {
var response = await client.get('/index.ext');
print('Response: ${response.body}');
expect(response, hasBody('.txt'));
});
2017-06-16 03:30:54 +00:00
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>'));
});
2017-06-16 02:05:06 +00:00
}
class ExtensionTransformer implements FileTransformer {
@override
FileInfo declareOutput(FileInfo file) {
return file.extension == '.ext' ? null : file.changeExtension('.ext');
}
@override
FutureOr<FileInfo> transform(FileInfo file) =>
file.changeText(file.extension).changeExtension('.ext');
}
2017-06-16 03:30:54 +00:00
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);
}
}