2017-08-15 23:01:16 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'package:angel_framework/angel_framework.dart';
|
2018-05-16 02:05:13 +00:00
|
|
|
import 'package:dart2_constant/convert.dart';
|
2017-08-15 23:01:16 +00:00
|
|
|
import 'package:mock_request/mock_request.dart';
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
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(
|
|
|
|
{
|
|
|
|
'deflate': ZLIB.encoder,
|
|
|
|
'gzip': GZIP.encoder,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
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'))
|
|
|
|
..headers.set(HttpHeaders.ACCEPT_ENCODING, '*')
|
|
|
|
..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
|
|
|
|
|
|
|
var body = await rs.fold<List<int>>([], (out, list) => []..addAll(list));
|
|
|
|
expect(rs.headers.value(HttpHeaders.CONTENT_ENCODING), 'deflate');
|
2018-05-16 02:05:13 +00:00
|
|
|
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'))
|
|
|
|
..headers.set(HttpHeaders.ACCEPT_ENCODING, ['foo', 'bar', '*'])
|
|
|
|
..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
|
|
|
|
|
|
|
var body = await rs.fold<List<int>>([], (out, list) => []..addAll(list));
|
|
|
|
expect(rs.headers.value(HttpHeaders.CONTENT_ENCODING), 'deflate');
|
2018-05-16 02:05:13 +00:00
|
|
|
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-02-07 05:36:24 +00:00
|
|
|
..headers.set(HttpHeaders.ACCEPT_ENCODING, 'gzip');
|
|
|
|
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
|
|
|
|
|
|
|
var body = await rs.fold<List<int>>([], (out, list) => []..addAll(list));
|
|
|
|
expect(rs.headers.value(HttpHeaders.CONTENT_ENCODING), 'gzip');
|
2018-05-16 02:05:13 +00:00
|
|
|
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-02-07 05:36:24 +00:00
|
|
|
..headers.set(HttpHeaders.ACCEPT_ENCODING, ['gzip', 'deflate']);
|
|
|
|
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
|
|
|
|
|
|
|
var body = await rs.fold<List<int>>([], (out, list) => []..addAll(list));
|
|
|
|
expect(rs.headers.value(HttpHeaders.CONTENT_ENCODING), 'gzip');
|
2018-05-16 02:05:13 +00:00
|
|
|
expect(body, GZIP.encode(utf8.encode('Hello, world!')));
|
2017-08-15 23:01:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|