platform/test/encoders_buffer_test.dart

111 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';
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();
var bb = new BytesBuilder();
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(() {
2018-08-20 02:40:11 +00:00
app = new 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();
2018-02-07 04:34:08 +00:00
http = new AngelHttp(app);
2017-08-15 23:01:16 +00:00
});
test('sends plaintext if no accept-encoding', () async {
var rq = new MockHttpRequest('GET', Uri.parse('/hello'))..close();
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 {
var rq = new MockHttpRequest('GET', Uri.parse('/hello'))
2018-06-08 07:06:26 +00:00
..headers.set('accept-encoding', '*')
2017-08-15 23:01:16 +00:00
..close();
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-08-21 18:50:43 +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 {
var rq = new MockHttpRequest('GET', Uri.parse('/hello'))
2018-06-08 07:06:26 +00:00
..headers.set('accept-encoding', ['foo', 'bar', '*'])
2017-08-15 23:01:16 +00:00
..close();
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 {
var rq = new 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 {
var rq = new 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
});
});
}