2021-05-15 13:28:26 +00:00
|
|
|
import 'package:angel3_framework/angel3_framework.dart';
|
|
|
|
import 'package:angel3_framework/http.dart';
|
|
|
|
import 'package:angel3_static/angel3_static.dart';
|
2017-09-23 21:57:54 +00:00
|
|
|
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() {
|
2016-11-23 17:22:23 +00:00
|
|
|
Angel app;
|
2021-05-01 03:53:04 +00:00
|
|
|
late AngelHttp http;
|
2021-02-21 02:47:23 +00:00
|
|
|
var testDir = const LocalFileSystem().directory('test');
|
2021-05-01 03:53:04 +00:00
|
|
|
late String url;
|
2021-02-21 02:47:23 +00:00
|
|
|
var client = Client();
|
2016-11-23 17:22:23 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2019-05-02 23:29:09 +00:00
|
|
|
app = Angel();
|
|
|
|
http = AngelHttp(app);
|
|
|
|
app.logger = Logger('angel')..onRecord.listen(print);
|
2016-11-23 17:22:23 +00:00
|
|
|
|
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,
|
|
|
|
);
|
2016-11-23 17:22:23 +00:00
|
|
|
|
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,
|
|
|
|
);
|
2016-11-23 17:22:23 +00:00
|
|
|
|
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);
|
2016-11-23 17:22:23 +00:00
|
|
|
|
2018-07-09 17:38:22 +00:00
|
|
|
var server = await http.startServer();
|
2021-02-21 02:47:23 +00:00
|
|
|
url = 'http://${server.address.host}:${server.port}';
|
2016-11-23 17:22:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() async {
|
2021-05-01 03:53:04 +00:00
|
|
|
if (http.server != null) await http.server!.close(force: true);
|
2016-11-23 17:22:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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'));
|
2016-11-23 17:22:23 +00:00
|
|
|
});
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2016-11-23 17:22:23 +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'));
|
2016-11-23 17:22:23 +00:00
|
|
|
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!'));
|
2016-11-23 17:22:23 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
}
|