2017-08-15 23:01:16 +00:00
|
|
|
import 'dart:async';
|
2018-08-20 20:21:06 +00:00
|
|
|
import 'dart:convert';
|
2018-06-08 07:06:26 +00:00
|
|
|
import 'dart:io' show stderr;
|
2018-08-20 20:21:06 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2018-08-19 15:33:25 +00:00
|
|
|
import 'package:angel_container/mirrors.dart';
|
2017-08-15 23:01:16 +00:00
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2017-09-22 14:53:49 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2017-08-15 23:01:16 +00:00
|
|
|
import 'package:mock_request/mock_request.dart';
|
|
|
|
import 'package:test/test.dart';
|
2018-08-20 20:21:06 +00:00
|
|
|
|
2017-08-15 23:01:16 +00:00
|
|
|
import 'encoders_buffer_test.dart' show encodingTests;
|
|
|
|
|
2017-08-15 23:08:24 +00:00
|
|
|
main() {
|
2017-08-15 23:01:16 +00:00
|
|
|
Angel app;
|
2018-02-07 04:34:08 +00:00
|
|
|
AngelHttp http;
|
2017-08-15 23:01:16 +00:00
|
|
|
|
|
|
|
setUp(() {
|
2018-08-20 02:40:11 +00:00
|
|
|
app = new Angel(reflector: MirrorsReflector());
|
2018-02-07 04:34:08 +00:00
|
|
|
http = new AngelHttp(app);
|
2017-09-22 14:53:49 +00:00
|
|
|
|
|
|
|
app.logger = new Logger('streaming_test')
|
|
|
|
..onRecord.listen((rec) {
|
|
|
|
print(rec);
|
|
|
|
if (rec.stackTrace != null) print(rec.stackTrace);
|
|
|
|
});
|
|
|
|
|
2017-08-15 23:01:16 +00:00
|
|
|
app.injectEncoders(
|
|
|
|
{
|
2018-06-08 07:06:26 +00:00
|
|
|
'deflate': zlib.encoder,
|
|
|
|
'gzip': gzip.encoder,
|
2017-08-15 23:01:16 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
app.get('/hello', (req, res) {
|
2017-08-15 23:01:16 +00:00
|
|
|
new Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]).pipe(res);
|
|
|
|
});
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
app.get('/write', (req, res) async {
|
2017-08-15 23:01:16 +00:00
|
|
|
await res.addStream(
|
|
|
|
new Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]));
|
|
|
|
res.write('bye');
|
|
|
|
await res.close();
|
|
|
|
});
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
app.get('/multiple', (req, res) async {
|
2017-08-15 23:01:16 +00:00
|
|
|
await res.addStream(
|
|
|
|
new Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits]));
|
2017-09-22 14:03:23 +00:00
|
|
|
await res
|
|
|
|
.addStream(new Stream<List<int>>.fromIterable(['bye'.codeUnits]));
|
2017-08-15 23:01:16 +00:00
|
|
|
await res.close();
|
|
|
|
});
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
app.get('/overwrite', (req, res) async {
|
2017-08-15 23:01:16 +00:00
|
|
|
res.statusCode = 32;
|
|
|
|
await new Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
|
|
|
|
.pipe(res);
|
2017-09-22 04:48:22 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
await new Stream<List<int>>.fromIterable(['Hello, world!'.codeUnits])
|
|
|
|
.pipe(res);
|
|
|
|
throw new Exception('Should throw on rewrite...');
|
|
|
|
} on StateError {
|
|
|
|
// Yay!!!
|
|
|
|
}
|
2017-08-15 23:01:16 +00:00
|
|
|
});
|
|
|
|
|
2018-08-20 20:21:06 +00:00
|
|
|
app.get('/error', (req, res) => res.addError(new StateError('wtf')));
|
2017-08-15 23:01:16 +00:00
|
|
|
|
2017-09-22 04:48:22 +00:00
|
|
|
app.errorHandler = (e, req, res) async {
|
2017-09-22 14:03:23 +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
|
|
|
|
|
|
|
_expectHelloBye(String path) async {
|
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse(path))..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 {
|
2017-09-22 14:03:23 +00:00
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/overwrite'))..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
|
|
|
|
|
|
|
if (rq.response.statusCode != 32)
|
|
|
|
throw 'overwrite should throw error; response: $body';
|
|
|
|
});
|
|
|
|
|
|
|
|
test('res => addError', () async {
|
|
|
|
try {
|
2017-09-22 14:03:23 +00:00
|
|
|
var rq = new MockHttpRequest('GET', Uri.parse('/error'))..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);
|
|
|
|
}
|