platform/packages/framework/test/streaming_test.dart

120 lines
3.2 KiB
Dart
Raw Normal View History

2017-08-15 23:01:16 +00:00
import 'dart:async';
import 'dart:convert';
import 'dart:io';
2021-05-14 10:34:09 +00:00
import 'package:angel3_container/mirrors.dart';
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
2017-09-22 14:53:49 +00:00
import 'package:logging/logging.dart';
2021-05-14 10:34:09 +00:00
import 'package:angel3_mock_request/angel3_mock_request.dart';
2020-02-05 23:02:46 +00:00
2017-08-15 23:01:16 +00:00
import 'package:test/test.dart';
2017-08-15 23:01:16 +00:00
import 'encoders_buffer_test.dart' show encodingTests;
2021-05-19 15:35:55 +00:00
void main() {
2021-03-20 08:11:18 +00:00
late Angel app;
late AngelHttp http;
2017-08-15 23:01:16 +00:00
setUp(() {
2019-05-02 22:48:31 +00:00
app = Angel(reflector: MirrorsReflector());
http = AngelHttp(app, useZone: true);
2017-09-22 14:53:49 +00:00
2019-05-02 22:48:31 +00:00
app.logger = Logger('streaming_test')
2017-09-22 14:53:49 +00:00
..onRecord.listen((rec) {
print(rec);
if (rec.stackTrace != null) print(rec.stackTrace);
});
2018-08-21 18:50:43 +00:00
app.encoders.addAll(
2017-08-15 23:01:16 +00:00
{
2018-06-08 07:06:26 +00:00
'deflate': zlib.encoder,
'gzip': gzip.encoder,
2017-08-15 23:01:16 +00:00
},
);
2018-08-21 18:50:43 +00:00
app.get('/hello', (req, res) {
2019-05-02 22:48:31 +00:00
return Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
2018-08-21 18:50:43 +00:00
.pipe(res);
2017-08-15 23:01:16 +00:00
});
app.get('/write', (req, res) async {
2017-08-15 23:01:16 +00:00
await res.addStream(
2019-05-02 22:48:31 +00:00
Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]));
2017-08-15 23:01:16 +00:00
res.write('bye');
await res.close();
});
app.get('/multiple', (req, res) async {
2017-08-15 23:01:16 +00:00
await res.addStream(
2019-05-02 22:48:31 +00:00
Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]));
await res.addStream(Stream<List<int>>.fromIterable(['bye'.codeUnits]));
2017-08-15 23:01:16 +00:00
await res.close();
});
app.get('/overwrite', (req, res) async {
2017-08-15 23:01:16 +00:00
res.statusCode = 32;
2019-05-02 22:48:31 +00:00
await Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
2017-08-15 23:01:16 +00:00
.pipe(res);
2017-09-22 04:48:22 +00:00
2019-05-02 22:48:31 +00:00
var f = Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
2018-08-21 18:50:43 +00:00
.pipe(res)
.then((_) => false)
.catchError((_) => true);
expect(f, completion(true));
2017-08-15 23:01:16 +00:00
});
2019-05-02 22:48:31 +00:00
app.get('/error', (req, res) => res.addError(StateError('wtf')));
2017-08-15 23:01:16 +00:00
2017-09-22 04:48:22 +00:00
app.errorHandler = (e, req, res) async {
2022-01-08 04:51:18 +00:00
stderr
..writeln(e.error)
..writeln(e.stackTrace);
2017-09-22 04:48:22 +00:00
};
2017-08-15 23:01:16 +00:00
});
2018-02-07 04:34:08 +00:00
tearDown(() => http.close());
2017-08-15 23:01:16 +00:00
2021-07-08 02:42:40 +00:00
void _expectHelloBye(String path) async {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse(path));
2021-06-26 13:13:43 +00:00
await (rq.close());
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2018-06-08 07:06:26 +00:00
var body = await rq.response.transform(utf8.decoder).join();
2017-08-15 23:01:16 +00:00
expect(body, 'Hello, world!bye');
}
test('write after addStream', () => _expectHelloBye('/write'));
test('multiple addStream', () => _expectHelloBye('/multiple'));
test('cannot write after close', () async {
2018-08-21 18:50:43 +00:00
try {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/overwrite'));
2022-02-20 01:48:12 +00:00
await rq.close();
2018-08-21 18:50:43 +00:00
await http.handleRequest(rq);
var body = await rq.response.transform(utf8.decoder).join();
2017-08-15 23:01:16 +00:00
2019-05-31 03:49:00 +00:00
if (rq.response.statusCode != 32) {
2018-08-21 18:50:43 +00:00
throw 'overwrite should throw error; response: $body';
2019-05-31 03:49:00 +00:00
}
2018-08-21 18:50:43 +00:00
} on StateError {
// Success
}
2017-08-15 23:01:16 +00:00
});
test('res => addError', () async {
try {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/error'));
2021-06-26 13:13:43 +00:00
await (rq.close());
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2018-06-08 07:06:26 +00:00
var body = await rq.response.transform(utf8.decoder).join();
2017-08-15 23:01:16 +00:00
throw 'addError should throw error; response: $body';
} on StateError {
// Should throw error...
}
});
encodingTests(() => app);
}