platform/packages/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';
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';
import 'package:angel3_mock_request/angel3_mock_request.dart';
2017-08-15 23:01:16 +00:00
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();
}
2021-05-14 10:34:09 +00:00
void main() {
2021-03-20 08:11:18 +00:00
late Angel app;
2017-08-15 23:01:16 +00:00
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);
}
2021-05-19 15:35:55 +00:00
void encodingTests(Angel Function() getApp) {
2017-08-15 23:01:16 +00:00
group('encoding', () {
Angel app;
2021-03-20 08:11:18 +00:00
late 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 {
2021-04-10 11:23:57 +00:00
var rq = MockHttpRequest('GET', Uri.parse('/hello'));
rq.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
});
});
}