platform/test/all_test.dart
2017-08-15 20:01:31 -04:00

100 lines
3.3 KiB
Dart

import 'dart:io';
import 'package:angel_diagnostics/angel_diagnostics.dart';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_static/angel_static.dart';
import 'package:http/http.dart' show Client;
import 'package:test/test.dart';
main() {
Angel app;
Directory testDir = new Directory('test');
String url;
Client client = new Client();
setUp(() async {
app = new Angel();
await app.configure(new VirtualDirectory(
debug: true,
source: testDir,
publicPath: '/virtual',
indexFileNames: ['index.txt']));
await app.configure(new VirtualDirectory(
debug: true,
source: testDir,
streamToIO: true,
indexFileNames: ['index.php', 'index.txt']));
app.after.add('Fallback');
app.dumpTree(showMatchers: true);
await app.configure(logRequests());
await app.startServer(InternetAddress.LOOPBACK_IP_V4, 0);
url = "http://${app.httpServer.address.host}:${app.httpServer.port}";
});
tearDown(() async {
if (app.httpServer != null) await app.httpServer.close(force: true);
});
test('can serve files, with correct Content-Type', () async {
var response = await client.get("$url/sample.txt");
expect(response.body, equals("Hello world"));
expect(response.headers[HttpHeaders.CONTENT_TYPE], contains("text/plain"));
});
test('can serve child directories', () async {
var response = await client.get("$url/nested");
expect(response.body, equals("Bird"));
expect(response.headers[HttpHeaders.CONTENT_TYPE], contains("text/plain"));
});
test('non-existent files are skipped', () async {
var response = await client.get("$url/nonexist.ent");
expect(response.body, equals('"Fallback"'));
});
test('can match index files', () async {
var response = await client.get(url);
expect(response.body, equals("index!"));
});
test('virtualRoots can match index', () async {
var response = await client.get("$url/virtual");
expect(response.body, equals("index!"));
});
test('chrome accept', () async {
var response = await client.get("$url/virtual", headers: {
HttpHeaders.ACCEPT:
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
});
expect(response.body, equals("index!"));
});
test('can gzip: just gzip', () async {
var response = await client
.get("$url/sample.txt", headers: {HttpHeaders.ACCEPT_ENCODING: 'gzip'});
expect(response.body, equals("Hello world"));
expect(response.headers[HttpHeaders.CONTENT_TYPE], contains("text/plain"));
expect(response.headers[HttpHeaders.CONTENT_ENCODING], 'gzip');
});
test('can gzip: wildcard', () async {
var response = await client
.get("$url/sample.txt", headers: {HttpHeaders.ACCEPT_ENCODING: 'foo, *'});
expect(response.body, equals("Hello world"));
expect(response.headers[HttpHeaders.CONTENT_TYPE], contains("text/plain"));
expect(response.headers[HttpHeaders.CONTENT_ENCODING], 'gzip');
});
test('can gzip: gzip and friends', () async {
var response = await client
.get("$url/sample.txt", headers: {HttpHeaders.ACCEPT_ENCODING: 'gzip, deflate, br'});
expect(response.body, equals("Hello world"));
expect(response.headers[HttpHeaders.CONTENT_TYPE], contains("text/plain"));
expect(response.headers[HttpHeaders.CONTENT_ENCODING], 'gzip');
});
}