platform/framework/test/encoders_buffer_test.dart

113 lines
3 KiB
Dart
Raw Normal View History

2018-05-16 03:14:29 +00:00
import 'dart:async';
2018-08-21 18:50:43 +00:00
import 'dart:convert';
2018-06-08 07:06:26 +00:00
import 'dart:io' show BytesBuilder;
2018-08-21 18:50:43 +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';
2018-10-22 15:54:41 +00:00
import 'package:angel_framework/http.dart';
2017-08-15 23:01:16 +00:00
import 'package:mock_request/mock_request.dart';
import 'package:test/test.dart';
2018-05-16 03:14:29 +00:00
Future<List<int>> getBody(MockHttpResponse rs) async {
var list = await rs.toList();
2019-05-02 22:48:31 +00:00
var bb = BytesBuilder();
2018-05-16 03:14:29 +00:00
list.forEach(bb.add);
return bb.takeBytes();
}
2017-08-15 23:08:24 +00:00
main() {
2017-08-15 23:01:16 +00:00
Angel app;
setUp(() {
2019-05-02 22:48:31 +00:00
app = Angel(reflector: MirrorsReflector());
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
},
);
app.get('/hello', (req, res) {
2018-08-21 18:50:43 +00:00
res
..useBuffer()
..write('Hello, world!');
2017-08-15 23:01:16 +00:00
});
});
tearDown(() => app.close());
encodingTests(() => app);
}
void encodingTests(Angel getApp()) {
group('encoding', () {
Angel app;
2018-02-07 04:34:08 +00:00
AngelHttp http;
2017-08-15 23:01:16 +00:00
setUp(() {
app = getApp();
2019-05-02 22:48:31 +00:00
http = AngelHttp(app);
2017-08-15 23:01:16 +00:00
});
test('sends plaintext if no accept-encoding', () async {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/hello'));
2019-04-17 19:42:24 +00:00
await rq.close();
2017-08-15 23:01:16 +00:00
var rs = rq.response;
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2017-08-15 23:01:16 +00:00
2018-05-16 02:05:13 +00:00
var body = await rs.transform(utf8.decoder).join();
2017-08-15 23:01:16 +00:00
expect(body, 'Hello, world!');
});
test('encodes if wildcard', () async {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
2019-04-17 19:42:24 +00:00
..headers.set('accept-encoding', '*');
await rq.close();
2017-08-15 23:01:16 +00:00
var rs = rq.response;
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2017-08-15 23:01:16 +00:00
2018-05-16 03:14:29 +00:00
var body = await getBody(rs);
2018-11-11 01:07:09 +00:00
//print(rs.headers);
2018-06-08 07:06:26 +00:00
expect(rs.headers.value('content-encoding'), 'deflate');
expect(body, zlib.encode(utf8.encode('Hello, world!')));
2017-08-15 23:01:16 +00:00
});
test('encodes if wildcard + multiple', () async {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
2019-04-17 19:42:24 +00:00
..headers.set('accept-encoding', ['foo', 'bar', '*']);
await rq.close();
2017-08-15 23:01:16 +00:00
var rs = rq.response;
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2017-08-15 23:01:16 +00:00
2018-05-16 03:14:29 +00:00
var body = await getBody(rs);
2018-06-08 07:06:26 +00:00
expect(rs.headers.value('content-encoding'), 'deflate');
expect(body, zlib.encode(utf8.encode('Hello, world!')));
2017-08-15 23:01:16 +00:00
});
test('encodes if explicit', () async {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
2018-06-08 07:06:26 +00:00
..headers.set('accept-encoding', 'gzip');
2018-02-07 05:36:24 +00:00
await rq.close();
2017-08-15 23:01:16 +00:00
var rs = rq.response;
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2017-08-15 23:01:16 +00:00
2018-05-16 03:14:29 +00:00
var body = await getBody(rs);
2018-06-08 07:06:26 +00:00
expect(rs.headers.value('content-encoding'), 'gzip');
expect(body, gzip.encode(utf8.encode('Hello, world!')));
2017-08-15 23:01:16 +00:00
});
test('only uses one encoder', () async {
2019-05-02 22:48:31 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/hello'))
2018-06-08 07:06:26 +00:00
..headers.set('accept-encoding', ['gzip', 'deflate']);
2018-02-07 05:36:24 +00:00
await rq.close();
2017-08-15 23:01:16 +00:00
var rs = rq.response;
2018-02-07 04:34:08 +00:00
await http.handleRequest(rq);
2018-06-23 03:59:41 +00:00
2018-05-16 03:14:29 +00:00
var body = await getBody(rs);
2018-06-08 07:06:26 +00:00
expect(rs.headers.value('content-encoding'), 'gzip');
expect(body, gzip.encode(utf8.encode('Hello, world!')));
2017-08-15 23:01:16 +00:00
});
});
}