platform/test/encoders_buffer_test.dart

106 lines
2.9 KiB
Dart
Raw Normal View History

2018-05-16 03:14:29 +00:00
import 'dart:async';
2018-06-08 07:06:26 +00:00
import 'dart:io' show BytesBuilder;
2017-08-15 23:01:16 +00:00
import 'package:angel_framework/angel_framework.dart';
2018-05-16 02:05:13 +00:00
import 'package:dart2_constant/convert.dart';
2018-06-08 07:06:26 +00:00
import 'package:dart2_constant/io.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();
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(() {
app = new Angel();
app.injectEncoders(
{
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', (res) {
res.write('Hello, world!');
});
});
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-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
});
});
}