[gardening] Fix standalone_2/io/http_compression_test.

Rewrite the test to use async/await and fix Dart 2 typing issues in it.

Change-Id: If071ffbf05443d66220897d30835d9499e248d3c
Reviewed-on: https://dart-review.googlesource.com/c/86927
Auto-Submit: Vyacheslav Egorov <vegorov@google.com>
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Vyacheslav Egorov <vegorov@google.com>
This commit is contained in:
Vyacheslav Egorov 2018-12-11 18:57:55 +00:00 committed by commit-bot@chromium.org
parent 237fcba9b6
commit 3a4ca65f6b
2 changed files with 74 additions and 95 deletions

View file

@ -11,112 +11,94 @@ import 'package:expect/expect.dart';
import 'dart:io';
import 'dart:typed_data';
void testServerCompress({bool clientAutoUncompress: true}) {
void test(List<int> data) {
HttpServer.bind("127.0.0.1", 0).then((server) {
server.autoCompress = true;
server.listen((request) {
request.response.add(data);
request.response.close();
});
var client = new HttpClient();
client.autoUncompress = clientAutoUncompress;
client.get("127.0.0.1", server.port, "/").then((request) {
request.headers.set(HttpHeaders.acceptEncodingHeader, "gzip,deflate");
return request.close();
}).then((response) {
Expect.equals(
"gzip", response.headers.value(HttpHeaders.contentEncodingHeader));
response.fold([], (list, b) {
list.addAll(b);
return list;
}).then((list) {
if (clientAutoUncompress) {
Expect.listEquals(data, list);
} else {
Expect.listEquals(data, gzip.decode(list));
}
server.close();
client.close();
});
});
void testServerCompress({bool clientAutoUncompress: true}) async {
void test(List<int> data) async {
final server = await HttpServer.bind("127.0.0.1", 0);
server.autoCompress = true;
server.listen((request) {
request.response.add(data);
request.response.close();
});
var client = new HttpClient();
client.autoUncompress = clientAutoUncompress;
final request = await client.get("127.0.0.1", server.port, "/");
request.headers.set(HttpHeaders.acceptEncodingHeader, "gzip,deflate");
final response = await request.close();
Expect.equals(
"gzip", response.headers.value(HttpHeaders.contentEncodingHeader));
final list = await response.fold(<int>[], (list, b) => list..addAll(b));
if (clientAutoUncompress) {
Expect.listEquals(data, list);
} else {
Expect.listEquals(data, gzip.decode(list));
}
server.close();
client.close();
}
test("My raw server provided data".codeUnits);
await test("My raw server provided data".codeUnits);
var longBuffer = new Uint8List(1024 * 1024);
for (int i = 0; i < longBuffer.length; i++) {
longBuffer[i] = i & 0xFF;
}
test(longBuffer);
await test(longBuffer);
}
void testAcceptEncodingHeader() {
void test(String encoding, bool valid) {
HttpServer.bind("127.0.0.1", 0).then((server) {
server.autoCompress = true;
server.listen((request) {
request.response.write("data");
request.response.close();
});
var client = new HttpClient();
client.get("127.0.0.1", server.port, "/").then((request) {
request.headers.set(HttpHeaders.acceptEncodingHeader, encoding);
return request.close();
}).then((response) {
Expect.equals(
valid,
("gzip" ==
response.headers.value(HttpHeaders.contentEncodingHeader)));
response.listen((_) {}, onDone: () {
server.close();
client.close();
});
});
});
}
test('gzip', true);
test('deflate', false);
test('gzip, deflate', true);
test('gzip ,deflate', true);
test('gzip , deflate', true);
test('deflate,gzip', true);
test('deflate, gzip', true);
test('deflate ,gzip', true);
test('deflate , gzip', true);
test('abc,deflate , gzip,def,,,ghi ,jkl', true);
test('xgzip', false);
test('gzipx;', false);
}
void testDisableCompressTest() {
HttpServer.bind("127.0.0.1", 0).then((server) {
Expect.equals(false, server.autoCompress);
void testAcceptEncodingHeader() async {
void test(String encoding, bool valid) async {
final server = await HttpServer.bind("127.0.0.1", 0);
server.autoCompress = true;
server.listen((request) {
Expect.equals(
'gzip', request.headers.value(HttpHeaders.acceptEncodingHeader));
request.response.write("data");
request.response.close();
});
var client = new HttpClient();
client
.get("127.0.0.1", server.port, "/")
.then((request) => request.close())
.then((response) {
Expect.equals(
null, response.headers.value(HttpHeaders.contentEncodingHeader));
response.listen((_) {}, onDone: () {
server.close();
client.close();
});
});
});
final request = await client.get("127.0.0.1", server.port, "/");
request.headers.set(HttpHeaders.acceptEncodingHeader, encoding);
final response = await request.close();
Expect.equals(valid,
("gzip" == response.headers.value(HttpHeaders.contentEncodingHeader)));
await response.listen((_) {}).asFuture();
server.close();
client.close();
}
await test('gzip', true);
await test('deflate', false);
await test('gzip, deflate', true);
await test('gzip ,deflate', true);
await test('gzip , deflate', true);
await test('deflate,gzip', true);
await test('deflate, gzip', true);
await test('deflate ,gzip', true);
await test('deflate , gzip', true);
await test('abc,deflate , gzip,def,,,ghi ,jkl', true);
await test('xgzip', false);
await test('gzipx;', false);
}
void main() {
testServerCompress();
testServerCompress(clientAutoUncompress: false);
testAcceptEncodingHeader();
testDisableCompressTest();
void testDisableCompressTest() async {
final server = await HttpServer.bind("127.0.0.1", 0);
Expect.equals(false, server.autoCompress);
server.listen((request) {
Expect.equals(
'gzip', request.headers.value(HttpHeaders.acceptEncodingHeader));
request.response.write("data");
request.response.close();
});
final client = new HttpClient();
final request = await client.get("127.0.0.1", server.port, "/");
final response = await request.close();
Expect.equals(
null, response.headers.value(HttpHeaders.contentEncodingHeader));
await response.listen((_) {}).asFuture();
server.close();
client.close();
}
void main() async {
await testServerCompress();
await testServerCompress(clientAutoUncompress: false);
await testAcceptEncodingHeader();
await testDisableCompressTest();
}

View file

@ -19,7 +19,6 @@ io/file_error_test: RuntimeError
io/file_test: RuntimeError
io/http_auth_digest_test: RuntimeError
io/http_auth_test: RuntimeError
io/http_compression_test: RuntimeError
io/http_cookie_date_test: CompileTimeError
io/http_headers_test: CompileTimeError
io/http_parser_test: CompileTimeError
@ -102,7 +101,6 @@ io/compile_all_test: RuntimeError # Issue 32338
io/file_fuzz_test: RuntimeError, Pass
io/http_client_connect_test: Skip # Flaky.
io/http_close_test: Crash
io/http_compression_test: RuntimeError
io/http_content_length_test: Skip # Flaky.
io/http_cookie_test: Skip # Flaky
io/http_proxy_advanced_test: Skip # Flaky
@ -154,7 +152,6 @@ io/web_socket_ping_test: Crash, Pass
[ $runtime == vm && $strong && ($compiler == dartk || $compiler == dartkb) ]
io/http_client_request_test: Pass, Timeout
io/http_compression_test: RuntimeError
io/platform_resolved_executable_test/03: Pass, RuntimeError
io/platform_resolved_executable_test/04: Pass, RuntimeError
io/secure_builtin_roots_test: Pass, Timeout