Hijack tests broken
This commit is contained in:
parent
1c00345e4e
commit
1923132532
2 changed files with 20 additions and 15 deletions
|
@ -32,21 +32,24 @@ Future<shelf.Request> convertRequest(RequestContext req, ResponseContext res,
|
|||
requestedUri = Uri.parse('http://${req.hostname}');
|
||||
requestedUri = requestedUri.replace(path: req.uri.path);
|
||||
|
||||
onHijack = (void hijack(StreamChannel<List<int>> channel)) {
|
||||
Future.sync(res.detach).then((_) {
|
||||
onHijack = (void Function(StreamChannel<List<int>> channel) hijack) async {
|
||||
try {
|
||||
print('a');
|
||||
await res.detach();
|
||||
print('b');
|
||||
var ctrl = StreamChannelController<List<int>>();
|
||||
if (req.hasParsedBody) {
|
||||
req.body.listen(ctrl.local.sink.add,
|
||||
onError: ctrl.local.sink.addError, onDone: ctrl.local.sink.close);
|
||||
} else {
|
||||
ctrl.local.sink.close();
|
||||
await ctrl.local.sink.close();
|
||||
}
|
||||
ctrl.local.stream.pipe(res);
|
||||
scheduleMicrotask(() => ctrl.local.stream.pipe(res));
|
||||
hijack(ctrl.foreign);
|
||||
}).catchError((e, st) {
|
||||
app.logger?.severe('An error occurred while hijacking a shelf request', e,
|
||||
st as StackTrace);
|
||||
});
|
||||
} catch (e, st) {
|
||||
app.logger
|
||||
?.severe('An error occurred while hijacking a shelf request', e, st);
|
||||
}
|
||||
};
|
||||
|
||||
var url = req.uri;
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:angel_client/io.dart' as c;
|
||||
import 'package:angel_framework/angel_framework.dart';
|
||||
import 'package:angel_framework/http.dart';
|
||||
import 'package:angel_shelf/angel_shelf.dart';
|
||||
import 'package:angel_test/angel_test.dart';
|
||||
import 'package:charcode/charcode.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:pretty_logging/pretty_logging.dart';
|
||||
import 'package:shelf/shelf.dart' as shelf;
|
||||
|
@ -13,7 +13,7 @@ import 'package:stream_channel/stream_channel.dart';
|
|||
import 'package:test/test.dart';
|
||||
|
||||
main() {
|
||||
c.Angel client;
|
||||
http.Client client;
|
||||
HttpServer server;
|
||||
String url;
|
||||
|
||||
|
@ -27,6 +27,7 @@ main() {
|
|||
}
|
||||
|
||||
setUp(() async {
|
||||
client = http.Client();
|
||||
var handler = shelf.Pipeline().addHandler((shelf.Request request) {
|
||||
if (request.url.path == 'two') {
|
||||
return shelf.Response(200, body: json.encode(2));
|
||||
|
@ -36,11 +37,13 @@ main() {
|
|||
return shelf.Response.notModified(headers: {'foo': 'bar'});
|
||||
} else if (request.url.path == 'hijack') {
|
||||
request.hijack((StreamChannel<List<int>> channel) {
|
||||
print('a');
|
||||
var sink = channel.sink;
|
||||
sink.add(utf8.encode('HTTP/1.1 200 OK\r\n'));
|
||||
sink.add([$lf]);
|
||||
sink.add(utf8.encode(json.encode({'error': 'crime'})));
|
||||
sink.close();
|
||||
print('b');
|
||||
});
|
||||
return null;
|
||||
} else if (request.url.path == 'throw') {
|
||||
|
@ -52,12 +55,11 @@ main() {
|
|||
|
||||
var logger = Logger.detached('angel_shelf')..onRecord.listen(prettyLog);
|
||||
var app = Angel(logger: logger);
|
||||
var http = AngelHttp(app);
|
||||
var httpDriver = AngelHttp(app);
|
||||
app.get('/angel', (req, res) => 'Angel');
|
||||
app.fallback(embedShelf(handler, throwOnNullResponse: true));
|
||||
|
||||
server = await http.startServer(InternetAddress.loopbackIPv4, 0);
|
||||
client = c.Rest(url = http.uri.toString());
|
||||
server = await httpDriver.startServer(InternetAddress.loopbackIPv4, 0);
|
||||
});
|
||||
|
||||
tearDown(() async {
|
||||
|
@ -84,7 +86,7 @@ main() {
|
|||
test('shelf can hijack', () async {
|
||||
try {
|
||||
var client = HttpClient();
|
||||
var rq = await client.openUrl('GET', Uri.parse('$url/hijack'));
|
||||
var rq = await client.openUrl('GET', Uri.parse(_path('/hijack')));
|
||||
var rs = await rq.close();
|
||||
var body = await rs.cast<List<int>>().transform(utf8.decoder).join();
|
||||
print('Response: $body');
|
||||
|
@ -94,7 +96,7 @@ main() {
|
|||
print(st);
|
||||
rethrow;
|
||||
}
|
||||
}, skip: '');
|
||||
});
|
||||
|
||||
test('shelf can set status code', () async {
|
||||
var response = await client.get(_path('/status'));
|
||||
|
|
Loading…
Reference in a new issue