Skip to content

Commit

Permalink
⚡️ Improve tests (cfug#55)
Browse files Browse the repository at this point in the history
- Remove redundant `#test` with namings.
- `assert\((.*)\, (.*)\)\;` -> `expect($1, $2);`.
- Add a 1-second delay for each `httpbin.org` test.
  • Loading branch information
AlexV525 authored Dec 29, 2022
1 parent c99ce95 commit 2b6bb61
Show file tree
Hide file tree
Showing 18 changed files with 357 additions and 331 deletions.
45 changes: 24 additions & 21 deletions dio/test/basic_test.dart
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
@TestOn('vm')
import 'dart:async';
import 'dart:io';

import 'package:diox/diox.dart';
import 'package:test/test.dart';

import 'mock/adapters.dart';

void main() {
test('#test headers', () {
test('test headers', () {
final headers = Headers.fromMap({
'set-cookie': ['k=v', 'k1=v1'],
'content-length': ['200'],
'test': ['1', '2'],
});
headers.add('SET-COOKIE', 'k2=v2');
assert(headers.value('content-length') == '200');
expect(headers.value('content-length'), '200');
expect(Future(() => headers.value('test')), throwsException);
assert(headers['set-cookie']?.length == 3);
expect(headers['set-cookie']?.length, 3);
headers.remove('set-cookie', 'k=v');
assert(headers['set-cookie']?.length == 2);
expect(headers['set-cookie']?.length, 2);
headers.removeAll('set-cookie');
assert(headers['set-cookie'] == null);
expect(headers['set-cookie'], isNull);
final ls = [];
headers.forEach((k, list) {
ls.addAll(list);
});
assert(ls.length == 3);
assert(headers.toString() == 'content-length: 200\ntest: 1\ntest: 2\n');
headers.forEach((k, list) => ls.addAll(list));
expect(ls.length, 3);
expect(headers.toString(), 'content-length: 200\ntest: 1\ntest: 2\n');
headers.set('content-length', '300');
assert(headers.value('content-length') == '300');
expect(headers.value('content-length'), '300');
headers.set('content-length', ['400']);
assert(headers.value('content-length') == '400');
expect(headers.value('content-length'), '400');

final headers1 = Headers();
headers1.set('xx', 'v');
assert(headers1.value('xx') == 'v');
expect(headers1.value('xx'), 'v');
headers1.clear();
assert(headers1.map.isEmpty == true);
expect(headers1.map.isEmpty, isTrue);
});

test('#send with an invalid URL', () async {
test('send with an invalid URL', () async {
await expectLater(
Dio().get('http://http.invalid'),
throwsA((e) => e is DioError && e.error is SocketException),
);
}, testOn: "vm");

test('#cancellation', () async {
test('cancellation', () async {
final dio = Dio();
final token = CancelToken();
Timer(Duration(milliseconds: 10), () {
Future.delayed(const Duration(milliseconds: 10), () {
token.cancel('cancelled');
dio.httpClientAdapter.close(force: true);
});
Expand All @@ -59,17 +60,19 @@ void main() {
);
});

test('#status error', () async {
final dio = Dio()..options.baseUrl = 'https://httpbin.org/status/';
test('status error', () async {
final dio = Dio()
..options.baseUrl = EchoAdapter.mockBase
..httpClientAdapter = EchoAdapter();
await expectLater(
dio.get('401'),
dio.get('/401'),
throwsA((e) =>
e is DioError &&
e.type == DioErrorType.badResponse &&
e.response!.statusCode == 401),
);
final r = await dio.get(
'401',
'/401',
options: Options(validateStatus: (status) => true),
);
expect(r.statusCode, 401);
Expand Down
2 changes: 1 addition & 1 deletion dio/test/dio_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:diox/diox.dart';
import 'package:test/test.dart';

void main() {
test('#assureResponse', () {
test('assureResponse', () {
final untypedResponse = Response<dynamic>(
requestOptions: RequestOptions(path: ''),
data: null,
Expand Down
16 changes: 8 additions & 8 deletions dio/test/download_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'utils.dart';
void main() {
setUp(startServer);
tearDown(stopServer);
test('#test download1', () async {
test('download1', () async {
const savePath = 'test/_download_test.md';
final dio = Dio();
dio.options.baseUrl = serverUrl.toString();
Expand All @@ -26,7 +26,7 @@ void main() {
f.deleteSync(recursive: false);
});

test('#test download2', () async {
test('download2', () async {
const savePath = 'test/_download_test.md';
final dio = Dio();
dio.options.baseUrl = serverUrl.toString();
Expand All @@ -40,25 +40,25 @@ void main() {
f.deleteSync(recursive: false);
});

test('#test download error', () async {
test('download error', () async {
const savePath = 'test/_download_test.md';
final dio = Dio();
dio.options.baseUrl = serverUrl.toString();
Response response = await dio
.download('/error', savePath)
.catchError((e) => (e as DioError).response!);
assert(response.data == 'error');
expect(response.data, 'error');
response = await dio
.download(
'/error',
savePath,
options: Options(receiveDataWhenStatusError: false),
)
.catchError((e) => (e as DioError).response!);
assert(response.data == null);
expect(response.data, null);
});

test('#test download timeout', () async {
test('download timeout', () async {
const savePath = 'test/_download_test.md';
final dio = Dio(BaseOptions(
receiveTimeout: Duration(milliseconds: 1),
Expand All @@ -72,7 +72,7 @@ void main() {
//print(r);
});

test('#test download cancellation', () async {
test('download cancellation', () async {
const savePath = 'test/_download_test.md';
final cancelToken = CancelToken();
Future.delayed(Duration(milliseconds: 100), () {
Expand All @@ -91,7 +91,7 @@ void main() {
//print(r);
});

test('Test `savePath` types', () async {
test('`savePath` types', () async {
Object? error;
final dio = Dio()
..options.baseUrl = EchoAdapter.mockBase
Expand Down
16 changes: 8 additions & 8 deletions dio/test/encoding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,45 @@ void main() {
}
}
};
test('#url encode default ', () {
test('default ', () {
// a=你好&b=5&b=6&c[d]=8&c[e][a]=5&c[e][b]=66&c[e][b]=8
final result =
'a=%E4%BD%A0%E5%A5%BD&b=5&b=6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D=66&c%5Be%5D%5Bb%5D=8';
expect(Transformer.urlEncodeMap(data), result);
});
test('#url encode csv', () {
test('csv', () {
// a=你好&b=5,6&c[d]=8&c[e][a]=5&c[e][b]=66,8
final result =
'a=%E4%BD%A0%E5%A5%BD&b=5%2C6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D=66%2C8';
expect(Transformer.urlEncodeMap(data, ListFormat.csv), result);
});
test('#url encode ssv', () {
test('ssv', () {
// a=你好&b=5+6&c[d]=8&c[e][a]=5&c[e][b]=66+8
final result =
'a=%E4%BD%A0%E5%A5%BD&b=5+6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D=66+8';
expect(Transformer.urlEncodeMap(data, ListFormat.ssv), result);
});
test('#url encode tsv', () {
test('tsv', () {
// a=你好&b=5\t6&c[d]=8&c[e][a]=5&c[e][b]=66\t8
final result =
'a=%E4%BD%A0%E5%A5%BD&b=5%5Ct6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D=66%5Ct8';
expect(Transformer.urlEncodeMap(data, ListFormat.tsv), result);
});
test('#url encode pipe', () {
test('pipe', () {
//a=你好&b=5|6&c[d]=8&c[e][a]=5&c[e][b]=66|8
final result =
'a=%E4%BD%A0%E5%A5%BD&b=5%7C6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D=66%7C8';
expect(Transformer.urlEncodeMap(data, ListFormat.pipes), result);
});

test('#url encode multi', () {
test('multi', () {
//a=你好&b[]=5&b[]=6&c[d]=8&c[e][a]=5&c[e][b][]=66&c[e][b][]=8
final result =
'a=%E4%BD%A0%E5%A5%BD&b%5B%5D=5&b%5B%5D=6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D%5B%5D=66&c%5Be%5D%5Bb%5D%5B%5D=8';
expect(Transformer.urlEncodeMap(data, ListFormat.multiCompatible), result);
});

test('#url encode multi2', () {
test('multi2', () {
final data = {
'a': 'string',
'b': 'another_string',
Expand All @@ -62,7 +62,7 @@ void main() {
expect(Transformer.urlEncodeMap(data, ListFormat.multiCompatible), result);
});

test('#url encode custom', () {
test('custom', () {
//a=你好&b=5|6&c[d]=8&c[e][a]=5&c[e][b]=foo,bar&c[e][c]=foo+bar&c[e][d][]=foo&c[e][d][]=bar&c[e][e]=foo\tbar
final result =
'a=%E4%BD%A0%E5%A5%BD&b=5%7C6&c%5Bd%5D=8&c%5Be%5D%5Ba%5D=5&c%5Be%5D%5Bb%5D=foo%2Cbar&c%5Be%5D%5Bc%5D=foo+bar&c%5Be%5D%5Bd%5D%5B%5D=foo&c%5Be%5D%5Bd%5D%5B%5D=bar&c%5Be%5D%5Be%5D=foo%5Ctbar';
Expand Down
25 changes: 13 additions & 12 deletions dio/test/exception_test.dart
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
@TestOn('vm')
import 'dart:io';

import 'package:diox/diox.dart';
import 'package:diox/io.dart';
import 'package:test/test.dart';

void main() {
test('catch DioError', () async {
dynamic error;

DioError? error;
try {
await Dio().get('https://does.not.exist');
fail('did not throw');
} on DioError catch (e) {
error = e;
}

expect(error, isNotNull);
expect(error is DioError, isTrue);
});

test('catch DioError as Exception', () async {
dynamic error;

DioError? error;
try {
await Dio().get('https://does.not.exist');
fail('did not throw');
} on DioError catch (e) {
error = e;
}

expect(error, isNotNull);
expect(error is DioError, isTrue);
});

test('catch sslerror: hostname mismatch', () async {
dynamic error;

test('catch DioError: hostname mismatch', () async {
DioError? error;
try {
await Dio().get('https://wrong.host.badssl.com/');
fail('did not throw');
} on DioError catch (e) {
error = e;
}
expect(error, isNotNull);
expect(error is DioError, isTrue);
expect(error.error, isA<HandshakeException>());
expect((error.error as HandshakeException).osError, isNotNull);
expect(
((error.error as HandshakeException).osError as OSError).message,
contains('Hostname mismatch'),
);
});

test('allow badssl', () async {
Expand Down
4 changes: 2 additions & 2 deletions dio/test/formdata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:diox/diox.dart';
import 'package:test/test.dart';

void main() async {
test('#test FormData', () async {
test('FormData', () async {
final fm = FormData.fromMap({
'name': 'wendux',
'age': 25,
Expand Down Expand Up @@ -67,6 +67,6 @@ void main() async {
'test': <String>['c']
}),
));
assert(fmStr.length == fm1.length);
expect(fmStr.length, fm1.length);
});
}
Loading

0 comments on commit 2b6bb61

Please sign in to comment.