platform/packages/framework/test/404_hole_test.dart

81 lines
2.1 KiB
Dart
Raw Normal View History

2019-05-02 22:37:32 +00:00
import 'dart:async';
2021-05-14 10:34:09 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
2019-05-02 22:37:32 +00:00
import 'package:charcode/ascii.dart';
import 'package:http/io_client.dart' as http;
import 'package:logging/logging.dart';
import 'package:test/test.dart';
import 'pretty_log.dart';
void main() {
2021-03-20 08:11:18 +00:00
late http.IOClient client;
late AngelHttp driver;
late Logger logger;
2019-05-02 22:37:32 +00:00
setUp(() async {
client = http.IOClient();
hierarchicalLoggingEnabled = true;
logger = Logger.detached('404_hole')
..level = Level.ALL
..onRecord.listen(prettyLog);
var app = Angel(logger: logger);
app.fallback(hello);
app.fallback(throw404);
// The error handler in the boilerplate.
var oldErrorHandler = app.errorHandler;
app.errorHandler = (e, req, res) async {
if (req.accepts('text/html', strict: true)) {
if (e.statusCode == 404 && req.accepts('text/html', strict: true)) {
await res
.render('error', {'message': 'No file exists at ${req.uri}.'});
} else {
await res.render('error', {'message': e.message});
}
} else {
return await oldErrorHandler(e, req, res);
}
};
driver = AngelHttp(app);
await driver.startServer();
});
tearDown(() {
logger.clearListeners();
client.close();
scheduleMicrotask(driver.close);
});
test('does not continue processing after streaming', () async {
var url = driver.uri.replace(path: '/hey');
2021-07-08 02:42:40 +00:00
for (var i = 0; i < 100; i++) {
2019-05-02 22:37:32 +00:00
var r = await client.get(url);
print('#$i: ${r.statusCode}: ${r.body}');
expect(r.statusCode, 200);
expect(r.body, 'Hello!');
}
});
}
/// Simulate angel_static
Future<void> hello(RequestContext req, ResponseContext res) {
if (req.path == 'hey') {
var bytes = [$H, $e, $l, $l, $o, $exclamation];
var s = Stream<List<int>>.fromIterable([bytes]);
return s.pipe(res);
} else {
return Future.value();
2019-05-02 22:37:32 +00:00
}
}
/// 404
void throw404(RequestContext req, ResponseContext res) {
2019-05-02 22:56:43 +00:00
Zone.current
.handleUncaughtError('This 404 should not occur.', StackTrace.current);
2019-05-02 22:37:32 +00:00
throw AngelHttpException.notFound();
}