platform/packages/static/test/all_test.dart

83 lines
2.5 KiB
Dart
Raw Normal View History

2016-04-21 22:44:05 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-11-13 21:25:17 +00:00
import 'package:angel_framework/http.dart';
2016-04-21 22:44:05 +00:00
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';
2016-04-21 22:44:05 +00:00
import 'package:http/http.dart' show Client;
2017-09-23 21:57:54 +00:00
import 'package:logging/logging.dart';
2016-04-21 22:44:05 +00:00
import 'package:test/test.dart';
2021-02-21 02:47:23 +00:00
void main() {
Angel app;
AngelHttp http;
2021-02-21 02:47:23 +00:00
var testDir = const LocalFileSystem().directory('test');
String url;
2021-02-21 02:47:23 +00:00
var client = Client();
setUp(() async {
2019-05-02 23:29:09 +00:00
app = Angel();
http = AngelHttp(app);
app.logger = Logger('angel')..onRecord.listen(print);
2018-08-28 14:58:28 +00:00
app.fallback(
2019-05-02 23:29:09 +00:00
VirtualDirectory(app, const LocalFileSystem(),
2017-09-23 21:57:54 +00:00
source: testDir,
publicPath: '/virtual',
indexFileNames: ['index.txt']).handleRequest,
);
2018-08-28 14:58:28 +00:00
app.fallback(
2019-05-02 23:29:09 +00:00
VirtualDirectory(app, const LocalFileSystem(),
2017-09-23 21:57:54 +00:00
source: testDir,
2018-08-28 14:58:28 +00:00
useBuffer: true,
2017-09-23 21:57:54 +00:00
indexFileNames: ['index.php', 'index.txt']).handleRequest,
);
2018-08-28 14:58:28 +00:00
app.fallback((req, res) => 'Fallback');
2016-11-23 20:14:05 +00:00
2016-11-28 00:51:47 +00:00
app.dumpTree(showMatchers: true);
var server = await http.startServer();
2021-02-21 02:47:23 +00:00
url = 'http://${server.address.host}:${server.port}';
});
tearDown(() async {
2018-11-13 21:25:17 +00:00
if (http.server != null) await http.server.close(force: true);
});
test('can serve files, with correct Content-Type', () async {
2021-03-08 13:22:29 +00:00
var response = await client.get(Uri.parse('$url/sample.txt'));
2021-02-21 02:47:23 +00:00
expect(response.body, equals('Hello world'));
expect(response.headers['content-type'], contains('text/plain'));
});
2017-02-22 23:43:27 +00:00
test('can serve child directories', () async {
2021-03-08 13:22:29 +00:00
var response = await client.get(Uri.parse('$url/nested'));
2021-02-21 02:47:23 +00:00
expect(response.body, equals('Bird'));
expect(response.headers['content-type'], contains('text/plain'));
2017-02-22 23:43:27 +00:00
});
test('non-existent files are skipped', () async {
2021-03-08 13:22:29 +00:00
var response = await client.get(Uri.parse('$url/nonexist.ent'));
expect(response.body, equals('"Fallback"'));
});
test('can match index files', () async {
2021-03-08 13:22:29 +00:00
var response = await client.get(Uri.parse(url));
2021-02-21 02:47:23 +00:00
expect(response.body, equals('index!'));
});
test('virtualRoots can match index', () async {
2021-03-08 13:22:29 +00:00
var response = await client.get(Uri.parse('$url/virtual'));
2021-02-21 02:47:23 +00:00
expect(response.body, equals('index!'));
2016-04-21 22:44:05 +00:00
});
2017-06-16 03:13:01 +00:00
test('chrome accept', () async {
2021-03-08 13:22:29 +00:00
var response = await client.get(Uri.parse('$url/virtual'), headers: {
2017-09-23 21:57:54 +00:00
'accept':
2017-06-16 03:13:01 +00:00
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
});
2021-02-21 02:47:23 +00:00
expect(response.body, equals('index!'));
2017-06-16 03:13:01 +00:00
});
2016-11-23 09:41:41 +00:00
}