Refactor tests to async/await (#322)
diff --git a/test/html/client_test.dart b/test/html/client_test.dart
index cd55d1d..ffbbff6 100644
--- a/test/html/client_test.dart
+++ b/test/html/client_test.dart
@@ -11,18 +11,20 @@
import 'utils.dart';
void main() {
- test('#send a StreamedRequest', () {
+ test('#send a StreamedRequest', () async {
var client = BrowserClient();
var request = http.StreamedRequest('POST', echoUrl);
- expect(
- client.send(request).then((response) {
- return response.stream.bytesToString();
- }).whenComplete(client.close),
- completion(equals('{"hello": "world"}')));
+ var responseFuture = client.send(request);
+ request.sink
+ ..add('{"hello": "world"}'.codeUnits)
+ ..close();
- request.sink.add('{"hello": "world"}'.codeUnits);
- request.sink.close();
+ var response = await responseFuture;
+ var bytesString = await response.stream.bytesToString();
+ client.close();
+
+ expect(bytesString, equals('{"hello": "world"}'));
}, skip: 'Need to fix server tests for browser');
test('#send with an invalid URL', () {
diff --git a/test/html/streamed_request_test.dart b/test/html/streamed_request_test.dart
index b2e7790..2213084 100644
--- a/test/html/streamed_request_test.dart
+++ b/test/html/streamed_request_test.dart
@@ -12,27 +12,26 @@
void main() {
group('contentLength', () {
- test("works when it's set", () {
+ test("works when it's set", () async {
var request = http.StreamedRequest('POST', echoUrl)
..contentLength = 10
..sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
..sink.close();
- return BrowserClient().send(request).then((response) {
- expect(response.stream.toBytes(),
- completion(equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])));
- });
+ final response = await BrowserClient().send(request);
+
+ expect(await response.stream.toBytes(),
+ equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
});
- test("works when it's not set", () {
+ test("works when it's not set", () async {
var request = http.StreamedRequest('POST', echoUrl);
request.sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
request.sink.close();
- return BrowserClient().send(request).then((response) {
- expect(response.stream.toBytes(),
- completion(equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])));
- });
+ final response = await BrowserClient().send(request);
+ expect(await response.stream.toBytes(),
+ equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
});
}, skip: 'Need to fix server tests for browser');
}
diff --git a/test/io/client_test.dart b/test/io/client_test.dart
index a77a026..f8c83f7 100644
--- a/test/io/client_test.dart
+++ b/test/io/client_test.dart
@@ -13,99 +13,97 @@
import 'utils.dart';
void main() {
+ setUp(startServer);
+
tearDown(stopServer);
- test('#send a StreamedRequest', () {
+ test('#send a StreamedRequest', () async {
+ var client = http.Client();
+ var request = http.StreamedRequest('POST', serverUrl)
+ ..headers[HttpHeaders.contentTypeHeader] =
+ 'application/json; charset=utf-8'
+ ..headers[HttpHeaders.userAgentHeader] = 'Dart';
+
+ var responseFuture = client.send(request);
+ request
+ ..sink.add('{"hello": "world"}'.codeUnits)
+ ..sink.close();
+
+ var response = await responseFuture;
+
+ expect(response.request, equals(request));
+ expect(response.statusCode, equals(200));
+ expect(response.headers['single'], equals('value'));
+ // dart:io internally normalizes outgoing headers so that they never
+ // have multiple headers with the same name, so there's no way to test
+ // whether we handle that case correctly.
+
+ var bytesString = await response.stream.bytesToString();
+ client.close();
expect(
- startServer().then((_) {
- var client = http.Client();
- var request = http.StreamedRequest('POST', serverUrl);
- request.headers[HttpHeaders.contentTypeHeader] =
- 'application/json; charset=utf-8';
- request.headers[HttpHeaders.userAgentHeader] = 'Dart';
-
- expect(
- client.send(request).then((response) {
- expect(response.request, equals(request));
- expect(response.statusCode, equals(200));
- expect(response.headers['single'], equals('value'));
- // dart:io internally normalizes outgoing headers so that they never
- // have multiple headers with the same name, so there's no way to test
- // whether we handle that case correctly.
-
- return response.stream.bytesToString();
- }).whenComplete(client.close),
- completion(parse(equals({
- 'method': 'POST',
- 'path': '/',
- 'headers': {
- 'content-type': ['application/json; charset=utf-8'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'transfer-encoding': ['chunked']
- },
- 'body': '{"hello": "world"}'
- }))));
-
- request.sink.add('{"hello": "world"}'.codeUnits);
- request.sink.close();
- }),
- completes);
+ bytesString,
+ parse(equals({
+ 'method': 'POST',
+ 'path': '/',
+ 'headers': {
+ 'content-type': ['application/json; charset=utf-8'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'transfer-encoding': ['chunked']
+ },
+ 'body': '{"hello": "world"}'
+ })));
});
- test('#send a StreamedRequest with a custom client', () {
+ test('#send a StreamedRequest with a custom client', () async {
+ var ioClient = HttpClient();
+ var client = http_io.IOClient(ioClient);
+ var request = http.StreamedRequest('POST', serverUrl)
+ ..headers[HttpHeaders.contentTypeHeader] =
+ 'application/json; charset=utf-8'
+ ..headers[HttpHeaders.userAgentHeader] = 'Dart';
+
+ var responseFuture = client.send(request);
+ request
+ ..sink.add('{"hello": "world"}'.codeUnits)
+ ..sink.close();
+
+ var response = await responseFuture;
+
+ expect(response.request, equals(request));
+ expect(response.statusCode, equals(200));
+ expect(response.headers['single'], equals('value'));
+ // dart:io internally normalizes outgoing headers so that they never
+ // have multiple headers with the same name, so there's no way to test
+ // whether we handle that case correctly.
+
+ var bytesString = await response.stream.bytesToString();
+ client.close();
expect(
- startServer().then((_) {
- var ioClient = HttpClient();
- var client = http_io.IOClient(ioClient);
- var request = http.StreamedRequest('POST', serverUrl);
- request.headers[HttpHeaders.contentTypeHeader] =
- 'application/json; charset=utf-8';
- request.headers[HttpHeaders.userAgentHeader] = 'Dart';
-
- expect(
- client.send(request).then((response) {
- expect(response.request, equals(request));
- expect(response.statusCode, equals(200));
- expect(response.headers['single'], equals('value'));
- // dart:io internally normalizes outgoing headers so that they never
- // have multiple headers with the same name, so there's no way to test
- // whether we handle that case correctly.
-
- return response.stream.bytesToString();
- }).whenComplete(client.close),
- completion(parse(equals({
- 'method': 'POST',
- 'path': '/',
- 'headers': {
- 'content-type': ['application/json; charset=utf-8'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'transfer-encoding': ['chunked']
- },
- 'body': '{"hello": "world"}'
- }))));
-
- request.sink.add('{"hello": "world"}'.codeUnits);
- request.sink.close();
- }),
- completes);
+ bytesString,
+ parse(equals({
+ 'method': 'POST',
+ 'path': '/',
+ 'headers': {
+ 'content-type': ['application/json; charset=utf-8'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'transfer-encoding': ['chunked']
+ },
+ 'body': '{"hello": "world"}'
+ })));
});
test('#send with an invalid URL', () {
- expect(
- startServer().then((_) {
- var client = http.Client();
- var url = Uri.parse('http://http.invalid');
- var request = http.StreamedRequest('POST', url);
- request.headers[HttpHeaders.contentTypeHeader] =
- 'application/json; charset=utf-8';
+ var client = http.Client();
+ var url = Uri.parse('http://http.invalid');
+ var request = http.StreamedRequest('POST', url);
+ request.headers[HttpHeaders.contentTypeHeader] =
+ 'application/json; charset=utf-8';
- expect(client.send(request), throwsSocketException);
+ expect(client.send(request), throwsSocketException);
- request.sink.add('{"hello": "world"}'.codeUnits);
- request.sink.close();
- }),
- completes);
+ request.sink.add('{"hello": "world"}'.codeUnits);
+ request.sink.close();
});
}
diff --git a/test/io/http_test.dart b/test/io/http_test.dart
index 41656a4..ac96010 100644
--- a/test/io/http_test.dart
+++ b/test/io/http_test.dart
@@ -11,559 +11,434 @@
main() {
group('http.', () {
+ setUp(startServer);
+
tearDown(stopServer);
- test('head', () {
- expect(
- startServer().then((_) {
- expect(
- http.head(serverUrl).then((response) {
- expect(response.statusCode, equals(200));
- expect(response.body, equals(''));
- }),
- completes);
- }),
- completes);
+ test('head', () async {
+ var response = await http.head(serverUrl);
+ expect(response.statusCode, equals(200));
+ expect(response.body, equals(''));
});
- test('get', () {
+ test('get', () async {
+ var response = await http.get(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.get(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'GET',
- 'path': '/',
- 'headers': {
- 'content-length': ['0'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'GET',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['0'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ })));
});
- test('post', () {
+ test('post', () async {
+ var response = await http.post(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'Content-Type': 'text/plain',
+ 'User-Agent': 'Dart'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.post(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'Content-Type': 'text/plain',
- 'User-Agent': 'Dart'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'POST',
- 'path': '/',
- 'headers': {
- 'accept-encoding': ['gzip'],
- 'content-length': ['0'],
- 'content-type': ['text/plain'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- }
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'POST',
+ 'path': '/',
+ 'headers': {
+ 'accept-encoding': ['gzip'],
+ 'content-length': ['0'],
+ 'content-type': ['text/plain'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ }
+ })));
});
- test('post with string', () {
+ test('post with string', () async {
+ var response = await http.post(serverUrl,
+ headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ },
+ body: 'request body');
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http
- .post(serverUrl,
- headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- },
- body: 'request body')
- .then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'POST',
- 'path': '/',
- 'headers': {
- 'content-type': ['text/plain; charset=utf-8'],
- 'content-length': ['12'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': 'request body'
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'POST',
+ 'path': '/',
+ 'headers': {
+ 'content-type': ['text/plain; charset=utf-8'],
+ 'content-length': ['12'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': 'request body'
+ })));
});
- test('post with bytes', () {
+ test('post with bytes', () async {
+ var response = await http.post(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ }, body: [
+ 104,
+ 101,
+ 108,
+ 108,
+ 111
+ ]);
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.post(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }, body: [
- 104,
- 101,
- 108,
- 108,
- 111
- ]).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'POST',
- 'path': '/',
- 'headers': {
- 'content-length': ['5'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': [104, 101, 108, 108, 111]
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'POST',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['5'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': [104, 101, 108, 108, 111]
+ })));
});
- test('post with fields', () {
+ test('post with fields', () async {
+ var response = await http.post(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ }, body: {
+ 'some-field': 'value',
+ 'other-field': 'other value'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.post(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }, body: {
- 'some-field': 'value',
- 'other-field': 'other value'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'POST',
- 'path': '/',
- 'headers': {
- 'content-type': [
- 'application/x-www-form-urlencoded; charset=utf-8'
- ],
- 'content-length': ['40'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': 'some-field=value&other-field=other+value'
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'POST',
+ 'path': '/',
+ 'headers': {
+ 'content-type': [
+ 'application/x-www-form-urlencoded; charset=utf-8'
+ ],
+ 'content-length': ['40'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': 'some-field=value&other-field=other+value'
+ })));
});
- test('put', () {
+ test('put', () async {
+ var response = await http.put(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'Content-Type': 'text/plain',
+ 'User-Agent': 'Dart'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.put(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'Content-Type': 'text/plain',
- 'User-Agent': 'Dart'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PUT',
- 'path': '/',
- 'headers': {
- 'accept-encoding': ['gzip'],
- 'content-length': ['0'],
- 'content-type': ['text/plain'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- }
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PUT',
+ 'path': '/',
+ 'headers': {
+ 'accept-encoding': ['gzip'],
+ 'content-length': ['0'],
+ 'content-type': ['text/plain'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ }
+ })));
});
- test('put with string', () {
+ test('put with string', () async {
+ var response = await http.put(serverUrl,
+ headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ },
+ body: 'request body');
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http
- .put(serverUrl,
- headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- },
- body: 'request body')
- .then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PUT',
- 'path': '/',
- 'headers': {
- 'content-type': ['text/plain; charset=utf-8'],
- 'content-length': ['12'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': 'request body'
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PUT',
+ 'path': '/',
+ 'headers': {
+ 'content-type': ['text/plain; charset=utf-8'],
+ 'content-length': ['12'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': 'request body'
+ })));
});
- test('put with bytes', () {
+ test('put with bytes', () async {
+ var response = await http.put(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ }, body: [
+ 104,
+ 101,
+ 108,
+ 108,
+ 111
+ ]);
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.put(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }, body: [
- 104,
- 101,
- 108,
- 108,
- 111
- ]).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PUT',
- 'path': '/',
- 'headers': {
- 'content-length': ['5'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': [104, 101, 108, 108, 111]
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PUT',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['5'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': [104, 101, 108, 108, 111]
+ })));
});
- test('put with fields', () {
+ test('put with fields', () async {
+ var response = await http.put(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ }, body: {
+ 'some-field': 'value',
+ 'other-field': 'other value'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.put(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }, body: {
- 'some-field': 'value',
- 'other-field': 'other value'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PUT',
- 'path': '/',
- 'headers': {
- 'content-type': [
- 'application/x-www-form-urlencoded; charset=utf-8'
- ],
- 'content-length': ['40'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': 'some-field=value&other-field=other+value'
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PUT',
+ 'path': '/',
+ 'headers': {
+ 'content-type': [
+ 'application/x-www-form-urlencoded; charset=utf-8'
+ ],
+ 'content-length': ['40'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': 'some-field=value&other-field=other+value'
+ })));
});
- test('patch', () {
+ test('patch', () async {
+ var response = await http.patch(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'Content-Type': 'text/plain',
+ 'User-Agent': 'Dart'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.patch(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'Content-Type': 'text/plain',
- 'User-Agent': 'Dart'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PATCH',
- 'path': '/',
- 'headers': {
- 'accept-encoding': ['gzip'],
- 'content-length': ['0'],
- 'content-type': ['text/plain'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- }
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PATCH',
+ 'path': '/',
+ 'headers': {
+ 'accept-encoding': ['gzip'],
+ 'content-length': ['0'],
+ 'content-type': ['text/plain'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ }
+ })));
});
- test('patch with string', () {
+ test('patch with string', () async {
+ var response = await http.patch(serverUrl,
+ headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ },
+ body: 'request body');
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http
- .patch(serverUrl,
- headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- },
- body: 'request body')
- .then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PATCH',
- 'path': '/',
- 'headers': {
- 'content-type': ['text/plain; charset=utf-8'],
- 'content-length': ['12'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': 'request body'
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PATCH',
+ 'path': '/',
+ 'headers': {
+ 'content-type': ['text/plain; charset=utf-8'],
+ 'content-length': ['12'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': 'request body'
+ })));
});
- test('patch with bytes', () {
+ test('patch with bytes', () async {
+ var response = await http.patch(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ }, body: [
+ 104,
+ 101,
+ 108,
+ 108,
+ 111
+ ]);
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.patch(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }, body: [
- 104,
- 101,
- 108,
- 108,
- 111
- ]).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PATCH',
- 'path': '/',
- 'headers': {
- 'content-length': ['5'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': [104, 101, 108, 108, 111]
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PATCH',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['5'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': [104, 101, 108, 108, 111]
+ })));
});
- test('patch with fields', () {
+ test('patch with fields', () async {
+ var response = await http.patch(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ }, body: {
+ 'some-field': 'value',
+ 'other-field': 'other value'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.patch(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }, body: {
- 'some-field': 'value',
- 'other-field': 'other value'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'PATCH',
- 'path': '/',
- 'headers': {
- 'content-type': [
- 'application/x-www-form-urlencoded; charset=utf-8'
- ],
- 'content-length': ['40'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- 'body': 'some-field=value&other-field=other+value'
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'PATCH',
+ 'path': '/',
+ 'headers': {
+ 'content-type': [
+ 'application/x-www-form-urlencoded; charset=utf-8'
+ ],
+ 'content-length': ['40'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ 'body': 'some-field=value&other-field=other+value'
+ })));
});
- test('delete', () {
+ test('delete', () async {
+ var response = await http.delete(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ });
+ expect(response.statusCode, equals(200));
expect(
- startServer().then((_) {
- expect(
- http.delete(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }).then((response) {
- expect(response.statusCode, equals(200));
- expect(
- response.body,
- parse(equals({
- 'method': 'DELETE',
- 'path': '/',
- 'headers': {
- 'content-length': ['0'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- }
- })));
- }),
- completes);
- }),
- completes);
+ response.body,
+ parse(equals({
+ 'method': 'DELETE',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['0'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ }
+ })));
});
- test('read', () {
+ test('read', () async {
+ var response = await http.read(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ });
expect(
- startServer().then((_) {
- expect(
- http.read(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }).then((val) => val),
- completion(parse(equals({
- 'method': 'GET',
- 'path': '/',
- 'headers': {
- 'content-length': ['0'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- }))));
- }),
- completes);
+ response,
+ parse(equals({
+ 'method': 'GET',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['0'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ })));
});
test('read throws an error for a 4** status code', () {
- expect(
- startServer().then((_) {
- expect(
- http.read(serverUrl.resolve('/error')), throwsClientException);
- }),
- completes);
+ expect(http.read(serverUrl.resolve('/error')), throwsClientException);
});
- test('readBytes', () {
- expect(
- startServer().then((_) {
- var future = http.readBytes(serverUrl, headers: {
- 'X-Random-Header': 'Value',
- 'X-Other-Header': 'Other Value',
- 'User-Agent': 'Dart'
- }).then((bytes) => String.fromCharCodes(bytes));
+ test('readBytes', () async {
+ var bytes = await http.readBytes(serverUrl, headers: {
+ 'X-Random-Header': 'Value',
+ 'X-Other-Header': 'Other Value',
+ 'User-Agent': 'Dart'
+ });
- expect(
- future,
- completion(parse(equals({
- 'method': 'GET',
- 'path': '/',
- 'headers': {
- 'content-length': ['0'],
- 'accept-encoding': ['gzip'],
- 'user-agent': ['Dart'],
- 'x-random-header': ['Value'],
- 'x-other-header': ['Other Value']
- },
- }))));
- }),
- completes);
+ expect(
+ String.fromCharCodes(bytes),
+ parse(equals({
+ 'method': 'GET',
+ 'path': '/',
+ 'headers': {
+ 'content-length': ['0'],
+ 'accept-encoding': ['gzip'],
+ 'user-agent': ['Dart'],
+ 'x-random-header': ['Value'],
+ 'x-other-header': ['Other Value']
+ },
+ })));
});
test('readBytes throws an error for a 4** status code', () {
expect(
- startServer().then((_) {
- expect(http.readBytes(serverUrl.resolve('/error')),
- throwsClientException);
- }),
- completes);
+ http.readBytes(serverUrl.resolve('/error')), throwsClientException);
});
});
}
diff --git a/test/io/multipart_test.dart b/test/io/multipart_test.dart
index 3ac9acc..9128b2e 100644
--- a/test/io/multipart_test.dart
+++ b/test/io/multipart_test.dart
@@ -4,7 +4,6 @@
@TestOn('vm')
-import 'dart:async';
import 'dart:io';
import 'package:http/http.dart' as http;
@@ -21,17 +20,14 @@
tearDown(() => tempDir.deleteSync(recursive: true));
- test('with a file from disk', () {
- expect(
- Future.sync(() {
- var filePath = path.join(tempDir.path, 'test-file');
- File(filePath).writeAsStringSync('hello');
- return http.MultipartFile.fromPath('file', filePath);
- }).then((file) {
- var request = http.MultipartRequest('POST', dummyUrl);
- request.files.add(file);
+ test('with a file from disk', () async {
+ var filePath = path.join(tempDir.path, 'test-file');
+ File(filePath).writeAsStringSync('hello');
+ var file = await http.MultipartFile.fromPath('file', filePath);
+ var request = http.MultipartRequest('POST', dummyUrl);
+ request.files.add(file);
- expect(request, bodyMatches('''
+ expect(request, bodyMatches('''
--{{boundary}}
content-type: application/octet-stream
content-disposition: form-data; name="file"; filename="test-file"
@@ -39,7 +35,5 @@
hello
--{{boundary}}--
'''));
- }),
- completes);
});
}
diff --git a/test/io/streamed_request_test.dart b/test/io/streamed_request_test.dart
index 960c684..9dd5d3e 100644
--- a/test/io/streamed_request_test.dart
+++ b/test/io/streamed_request_test.dart
@@ -12,48 +12,41 @@
import 'utils.dart';
void main() {
- group('contentLength', () {
- test('controls the Content-Length header', () {
- return startServer().then((_) {
- var request = http.StreamedRequest('POST', serverUrl)
- ..contentLength = 10
- ..sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
- ..sink.close();
+ setUp(startServer);
- return request.send();
- }).then((response) {
- expect(
- utf8.decodeStream(response.stream),
- completion(parse(containsPair(
- 'headers', containsPair('content-length', ['10'])))));
- }).whenComplete(stopServer);
+ tearDown(stopServer);
+
+ group('contentLength', () {
+ test('controls the Content-Length header', () async {
+ var request = http.StreamedRequest('POST', serverUrl)
+ ..contentLength = 10
+ ..sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
+ ..sink.close();
+
+ var response = await request.send();
+ expect(
+ await utf8.decodeStream(response.stream),
+ parse(
+ containsPair('headers', containsPair('content-length', ['10']))));
});
- test('defaults to sending no Content-Length', () {
- return startServer().then((_) {
- var request = http.StreamedRequest('POST', serverUrl);
- request.sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
- request.sink.close();
+ test('defaults to sending no Content-Length', () async {
+ var request = http.StreamedRequest('POST', serverUrl);
+ request.sink.add([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
+ request.sink.close();
- return request.send();
- }).then((response) {
- expect(
- utf8.decodeStream(response.stream),
- completion(parse(
- containsPair('headers', isNot(contains('content-length'))))));
- }).whenComplete(stopServer);
+ var response = await request.send();
+ expect(await utf8.decodeStream(response.stream),
+ parse(containsPair('headers', isNot(contains('content-length')))));
});
});
// Regression test.
- test('.send() with a response with no content length', () {
- return startServer().then((_) {
- var request =
- http.StreamedRequest('GET', serverUrl.resolve('/no-content-length'));
- request.sink.close();
- return request.send();
- }).then((response) {
- expect(utf8.decodeStream(response.stream), completion(equals('body')));
- }).whenComplete(stopServer);
+ test('.send() with a response with no content length', () async {
+ var request =
+ http.StreamedRequest('GET', serverUrl.resolve('/no-content-length'));
+ request.sink.close();
+ var response = await request.send();
+ expect(await utf8.decodeStream(response.stream), equals('body'));
});
}
diff --git a/test/io/utils.dart b/test/io/utils.dart
index 7e94396..d057fa4 100644
--- a/test/io/utils.dart
+++ b/test/io/utils.dart
@@ -8,6 +8,7 @@
import 'package:http/http.dart';
import 'package:http/src/utils.dart';
+import 'package:pedantic/pedantic.dart';
import 'package:test/test.dart';
export '../utils.dart';
@@ -19,18 +20,17 @@
Uri get serverUrl => Uri.parse('http://localhost:${_server.port}');
/// Starts a new HTTP server.
-Future startServer() {
- return HttpServer.bind('localhost', 0).then((s) {
- _server = s;
- s.listen((request) {
+Future<void> startServer() async {
+ _server = (await HttpServer.bind('localhost', 0))
+ ..listen((request) async {
var path = request.uri.path;
var response = request.response;
if (path == '/error') {
response
..statusCode = 400
- ..contentLength = 0
- ..close();
+ ..contentLength = 0;
+ unawaited(response.close());
return;
}
@@ -40,8 +40,8 @@
..statusCode = 302
..headers
.set('location', serverUrl.resolve('/loop?${n + 1}').toString())
- ..contentLength = 0
- ..close();
+ ..contentLength = 0;
+ unawaited(response.close());
return;
}
@@ -49,8 +49,8 @@
response
..statusCode = 302
..headers.set('location', serverUrl.resolve('/').toString())
- ..contentLength = 0
- ..close();
+ ..contentLength = 0;
+ unawaited(response.close());
return;
}
@@ -58,54 +58,52 @@
response
..statusCode = 200
..contentLength = -1
- ..write('body')
- ..close();
+ ..write('body');
+ unawaited(response.close());
return;
}
- ByteStream(request).toBytes().then((requestBodyBytes) {
- var encodingName = request.uri.queryParameters['response-encoding'];
- var outputEncoding = encodingName == null
- ? ascii
- : requiredEncodingForCharset(encodingName);
+ var requestBodyBytes = await ByteStream(request).toBytes();
+ var encodingName = request.uri.queryParameters['response-encoding'];
+ var outputEncoding = encodingName == null
+ ? ascii
+ : requiredEncodingForCharset(encodingName);
- response.headers.contentType =
- ContentType('application', 'json', charset: outputEncoding.name);
- response.headers.set('single', 'value');
+ response.headers.contentType =
+ ContentType('application', 'json', charset: outputEncoding.name);
+ response.headers.set('single', 'value');
- dynamic requestBody;
- if (requestBodyBytes.isEmpty) {
- requestBody = null;
- } else if (request.headers.contentType?.charset != null) {
- var encoding =
- requiredEncodingForCharset(request.headers.contentType.charset);
- requestBody = encoding.decode(requestBodyBytes);
- } else {
- requestBody = requestBodyBytes;
- }
+ dynamic requestBody;
+ if (requestBodyBytes.isEmpty) {
+ requestBody = null;
+ } else if (request.headers.contentType?.charset != null) {
+ var encoding =
+ requiredEncodingForCharset(request.headers.contentType.charset);
+ requestBody = encoding.decode(requestBodyBytes);
+ } else {
+ requestBody = requestBodyBytes;
+ }
- var content = <String, dynamic>{
- 'method': request.method,
- 'path': request.uri.path,
- 'headers': {}
- };
- if (requestBody != null) content['body'] = requestBody;
- request.headers.forEach((name, values) {
- // These headers are automatically generated by dart:io, so we don't
- // want to test them here.
- if (name == 'cookie' || name == 'host') return;
+ var content = <String, dynamic>{
+ 'method': request.method,
+ 'path': request.uri.path,
+ 'headers': {}
+ };
+ if (requestBody != null) content['body'] = requestBody;
+ request.headers.forEach((name, values) {
+ // These headers are automatically generated by dart:io, so we don't
+ // want to test them here.
+ if (name == 'cookie' || name == 'host') return;
- content['headers'][name] = values;
- });
-
- var body = json.encode(content);
- response
- ..contentLength = body.length
- ..write(body)
- ..close();
+ content['headers'][name] = values;
});
+
+ var body = json.encode(content);
+ response
+ ..contentLength = body.length
+ ..write(body);
+ unawaited(response.close());
});
- });
}
/// Stops the current HTTP server.
diff --git a/test/mock_client_test.dart b/test/mock_client_test.dart
index 7ab382d..cb7f152 100644
--- a/test/mock_client_test.dart
+++ b/test/mock_client_test.dart
@@ -12,49 +12,35 @@
import 'utils.dart';
void main() {
- test('handles a request', () {
- var client = MockClient((request) {
- return Future.value(http.Response(json.encode(request.bodyFields), 200,
- request: request, headers: {'content-type': 'application/json'}));
- });
+ test('handles a request', () async {
+ var client = MockClient((request) async => http.Response(
+ json.encode(request.bodyFields), 200,
+ request: request, headers: {'content-type': 'application/json'}));
+ var response = await client.post('http://example.com/foo',
+ body: {'field1': 'value1', 'field2': 'value2'});
expect(
- client.post('http://example.com/foo', body: {
- 'field1': 'value1',
- 'field2': 'value2'
- }).then((response) => response.body),
- completion(parse(equals({'field1': 'value1', 'field2': 'value2'}))));
+ response.body, parse(equals({'field1': 'value1', 'field2': 'value2'})));
});
- test('handles a streamed request', () {
- var client = MockClient.streaming((request, bodyStream) {
- return bodyStream.bytesToString().then((bodyString) {
- var controller = StreamController<List<int>>(sync: true);
- Future.sync(() {
- controller
- ..add('Request body was "$bodyString"'.codeUnits)
- ..close();
- });
-
- return http.StreamedResponse(controller.stream, 200);
- });
+ test('handles a streamed request', () async {
+ var client = MockClient.streaming((request, bodyStream) async {
+ var bodyString = await bodyStream.bytesToString();
+ var stream =
+ Stream.fromIterable(['Request body was "$bodyString"'.codeUnits]);
+ return http.StreamedResponse(stream, 200);
});
var uri = Uri.parse('http://example.com/foo');
var request = http.Request('POST', uri)..body = 'hello, world';
- var future = client
- .send(request)
- .then(http.Response.fromStream)
- .then((response) => response.body);
- expect(future, completion(equals('Request body was "hello, world"')));
+ var streamedResponse = await client.send(request);
+ var response = await http.Response.fromStream(streamedResponse);
+ expect(response.body, equals('Request body was "hello, world"'));
});
- test('handles a request with no body', () {
- var client = MockClient((request) {
- return Future.value(http.Response('you did it', 200));
- });
+ test('handles a request with no body', () async {
+ var client = MockClient((_) async => http.Response('you did it', 200));
- expect(client.read('http://example.com/foo'),
- completion(equals('you did it')));
+ expect(await client.read('http://example.com/foo'), equals('you did it'));
});
}
diff --git a/test/response_test.dart b/test/response_test.dart
index 9e55a83..532b227 100644
--- a/test/response_test.dart
+++ b/test/response_test.dart
@@ -5,6 +5,7 @@
import 'dart:async';
import 'package:http/http.dart' as http;
+import 'package:pedantic/pedantic.dart';
import 'package:test/test.dart';
void main() {
@@ -48,31 +49,26 @@
});
group('.fromStream()', () {
- test('sets body', () {
+ test('sets body', () async {
var controller = StreamController<List<int>>(sync: true);
var streamResponse =
http.StreamedResponse(controller.stream, 200, contentLength: 13);
- var future = http.Response.fromStream(streamResponse)
- .then((response) => response.body);
- expect(future, completion(equals('Hello, world!')));
-
controller
..add([72, 101, 108, 108, 111, 44, 32])
- ..add([119, 111, 114, 108, 100, 33])
- ..close();
+ ..add([119, 111, 114, 108, 100, 33]);
+ unawaited(controller.close());
+ var response = await http.Response.fromStream(streamResponse);
+ expect(response.body, equals('Hello, world!'));
});
- test('sets bodyBytes', () {
+ test('sets bodyBytes', () async {
var controller = StreamController<List<int>>(sync: true);
var streamResponse =
http.StreamedResponse(controller.stream, 200, contentLength: 5);
- var future = http.Response.fromStream(streamResponse)
- .then((response) => response.bodyBytes);
- expect(future, completion(equals([104, 101, 108, 108, 111])));
-
- controller
- ..add([104, 101, 108, 108, 111])
- ..close();
+ controller.add([104, 101, 108, 108, 111]);
+ unawaited(controller.close());
+ var response = await http.Response.fromStream(streamResponse);
+ expect(response.bodyBytes, equals([104, 101, 108, 108, 111]));
});
});
}
diff --git a/test/utils.dart b/test/utils.dart
index 44cf12d..cd6190c 100644
--- a/test/utils.dart
+++ b/test/utils.dart
@@ -85,19 +85,20 @@
bool matches(item, Map matchState) {
if (item is! http.MultipartRequest) return false;
- var future = item.finalize().toBytes().then((bodyBytes) {
- var body = utf8.decode(bodyBytes);
- var contentType = MediaType.parse(item.headers['content-type']);
- var boundary = contentType.parameters['boundary'];
- var expected = cleanUpLiteral(_pattern)
- .replaceAll('\n', '\r\n')
- .replaceAll('{{boundary}}', boundary);
+ return completes.matches(_checks(item), matchState);
+ }
- expect(body, equals(expected));
- expect(item.contentLength, equals(bodyBytes.length));
- });
+ Future<void> _checks(http.MultipartRequest item) async {
+ var bodyBytes = await item.finalize().toBytes();
+ var body = utf8.decode(bodyBytes);
+ var contentType = MediaType.parse(item.headers['content-type']);
+ var boundary = contentType.parameters['boundary'];
+ var expected = cleanUpLiteral(_pattern)
+ .replaceAll('\n', '\r\n')
+ .replaceAll('{{boundary}}', boundary);
- return completes.matches(future, matchState);
+ expect(body, equals(expected));
+ expect(item.contentLength, equals(bodyBytes.length));
}
@override