Skip to content

Commit

Permalink
Revert changes to List<int> body data handling (cfug#1860)
Browse files Browse the repository at this point in the history
A previous change to improve the performance of binary uploads
accidentally broke the functionality of uploading `List<int>` json
bodies.
Fixes cfug#1858
  • Loading branch information
kuhnroyal authored Jun 10, 2023
1 parent 57c57d3 commit 22fdb85
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
5 changes: 4 additions & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ See the [Migration Guide][] for the complete breaking changes list.**

*None.*

## 5.2.0+2

- Revert changes to handling of `List<int>` body data.

## 5.2.0+1

- Fix `DioErrorType` deprecation hint.
Expand All @@ -20,7 +24,6 @@ See the [Migration Guide][] for the complete breaking changes list.**
Dio 6.0.0 - Please use the replacement `IOHttpClientAdapter.createHttpClient` instead.
- Using `CancelToken` no longer closes and re-creates `HttpClient` for each request when `IOHttpClientAdapter` is used.
- Fix timeout handling for browser `receiveTimeout`.
- Using `CancelToken` no longer closes and re-creates `HttpClient` for each request when `IOHttpClientAdapter` is used.
- Improve performance when sending binary data (`List<int>`/`Uint8List`).

## 5.1.2
Expand Down
3 changes: 0 additions & 3 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,6 @@ abstract class DioMixin implements Dio {
if (data is Uint8List) {
// Handle binary data which does not need to be transformed
bytes = data;
} else if (data is List<int>) {
// Handle binary data which does not need to be transformed
bytes = Uint8List.fromList(data);
} else {
// Call request transformer for anything else
final transformed = await transformer.transformRequest(options);
Expand Down
36 changes: 26 additions & 10 deletions dio/test/upload_test.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'mock/http_mock.mocks.dart';

void main() {
late Dio dio;

setUp(() {
dio = Dio()..options.baseUrl = 'https://httpbun.com/';
});

test('binary data should not be transformed', () async {
final bytes = List.generate(1024, (index) => index);
final transformer = MockTransformer();
when(transformer.transformResponse(any, any)).thenAnswer(
(i) => i.positionalArguments[1],
);
test('Uint8List should not be transformed', () async {
final bytes = Uint8List.fromList(List.generate(10, (index) => index));
final transformer = dio.transformer = _TestTransformer();
final r = await dio.put(
'/put',
data: bytes,
);
verifyNever(transformer.transformRequest(any));
expect(transformer.requestTransformed, isFalse);
expect(r.statusCode, 200);
});

test('List<int> should be transformed', () async {
final ints = List.generate(10, (index) => index);
final transformer = dio.transformer = _TestTransformer();
final r = await dio.put(
'/put',
data: ints,
);
expect(transformer.requestTransformed, isTrue);
expect(r.data['data'], ints.toString());
});

test('stream', () async {
const str = 'hello 😌';
final bytes = utf8.encode(str).toList();
Expand Down Expand Up @@ -96,3 +102,13 @@ void main() {
testOn: 'vm',
);
}

class _TestTransformer extends BackgroundTransformer {
bool requestTransformed = false;

@override
Future<String> transformRequest(RequestOptions options) async {
requestTransformed = true;
return super.transformRequest(options);
}
}

0 comments on commit 22fdb85

Please sign in to comment.