-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dio] Fix FormData encoding regression for maps with dynamic keys (#1757
) In relation to #1741 ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [x] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [x] I have updated the `CHANGELOG.md` in the corresponding package ### Additional context and info (if any) Fixes #1741 Fixes #1758 --------- Signed-off-by: Alex Li <github@alexv525.com> Co-authored-by: Alex Li <github@alexv525.com>
- Loading branch information
Showing
6 changed files
with
200 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
@TestOn('chrome') | ||
@TestOn('browser') | ||
import 'dart:typed_data'; | ||
|
||
import 'package:dio/browser.dart'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,197 @@ | ||
@TestOn('vm') | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:dio/dio.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import 'mock/adapters.dart'; | ||
|
||
void main() async { | ||
group(FormData, () { | ||
test('complex', () async { | ||
final fm = FormData.fromMap({ | ||
'name': 'wendux', | ||
'age': 25, | ||
'path': '/图片空间/地址', | ||
'file': MultipartFile.fromString( | ||
'hello world.', | ||
headers: { | ||
'test': <String>['a'] | ||
}, | ||
), | ||
'files': [ | ||
await MultipartFile.fromFile( | ||
'test/mock/_testfile', | ||
filename: '1.txt', | ||
headers: { | ||
'test': <String>['b'] | ||
}, | ||
), | ||
MultipartFile.fromFileSync( | ||
'test/mock/_testfile', | ||
filename: '2.txt', | ||
headers: { | ||
'test': <String>['c'] | ||
}, | ||
), | ||
] | ||
}); | ||
final fmStr = await fm.readAsBytes(); | ||
final f = File('test/mock/_formdata'); | ||
String content = f.readAsStringSync(); | ||
content = content.replaceAll('--dio-boundary-3788753558', fm.boundary); | ||
String actual = utf8.decode(fmStr, allowMalformed: true); | ||
|
||
actual = actual.replaceAll('\r\n', '\n'); | ||
content = content.replaceAll('\r\n', '\n'); | ||
|
||
expect(actual, content); | ||
expect(fm.readAsBytes(), throwsA(const TypeMatcher<StateError>())); | ||
|
||
final fm1 = FormData(); | ||
fm1.fields.add(MapEntry('name', 'wendux')); | ||
fm1.fields.add(MapEntry('age', '25')); | ||
fm1.fields.add(MapEntry('path', '/图片空间/地址')); | ||
fm1.files.add( | ||
MapEntry( | ||
'file', | ||
MultipartFile.fromString( | ||
test( | ||
'complex', | ||
() async { | ||
final fm = FormData.fromMap({ | ||
'name': 'wendux', | ||
'age': 25, | ||
'path': '/图片空间/地址', | ||
'file': MultipartFile.fromString( | ||
'hello world.', | ||
headers: { | ||
'test': <String>['a'] | ||
}, | ||
), | ||
), | ||
); | ||
fm1.files.add( | ||
MapEntry( | ||
'files', | ||
await MultipartFile.fromFile( | ||
'test/mock/_testfile', | ||
filename: '1.txt', | ||
headers: { | ||
'test': <String>['b'], | ||
}, | ||
'files': [ | ||
await MultipartFile.fromFile( | ||
'test/mock/_testfile', | ||
filename: '1.txt', | ||
headers: { | ||
'test': <String>['b'] | ||
}, | ||
), | ||
MultipartFile.fromFileSync( | ||
'test/mock/_testfile', | ||
filename: '2.txt', | ||
headers: { | ||
'test': <String>['c'] | ||
}, | ||
), | ||
] | ||
}); | ||
final fmStr = await fm.readAsBytes(); | ||
final f = File('test/mock/_formdata'); | ||
String content = f.readAsStringSync(); | ||
content = content.replaceAll('--dio-boundary-3788753558', fm.boundary); | ||
String actual = utf8.decode(fmStr, allowMalformed: true); | ||
|
||
actual = actual.replaceAll('\r\n', '\n'); | ||
content = content.replaceAll('\r\n', '\n'); | ||
|
||
expect(actual, content); | ||
expect(fm.readAsBytes(), throwsA(const TypeMatcher<StateError>())); | ||
|
||
final fm1 = FormData(); | ||
fm1.fields.add(MapEntry('name', 'wendux')); | ||
fm1.fields.add(MapEntry('age', '25')); | ||
fm1.fields.add(MapEntry('path', '/图片空间/地址')); | ||
fm1.files.add( | ||
MapEntry( | ||
'file', | ||
MultipartFile.fromString( | ||
'hello world.', | ||
headers: { | ||
'test': <String>['a'] | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
fm1.files.add( | ||
MapEntry( | ||
'files', | ||
await MultipartFile.fromFile( | ||
'test/mock/_testfile', | ||
filename: '2.txt', | ||
headers: { | ||
'test': <String>['c'], | ||
}, | ||
); | ||
fm1.files.add( | ||
MapEntry( | ||
'files', | ||
await MultipartFile.fromFile( | ||
'test/mock/_testfile', | ||
filename: '1.txt', | ||
headers: { | ||
'test': <String>['b'], | ||
}, | ||
), | ||
), | ||
); | ||
fm1.files.add( | ||
MapEntry( | ||
'files', | ||
await MultipartFile.fromFile( | ||
'test/mock/_testfile', | ||
filename: '2.txt', | ||
headers: { | ||
'test': <String>['c'], | ||
}, | ||
), | ||
), | ||
), | ||
); | ||
expect(fmStr.length, fm1.length); | ||
}, | ||
testOn: 'vm', | ||
); | ||
|
||
test('encodes maps correctly', () async { | ||
final fd = FormData.fromMap( | ||
{ | ||
'items': [ | ||
{'name': 'foo', 'value': 1}, | ||
{'name': 'bar', 'value': 2}, | ||
], | ||
'api': { | ||
'dest': '/', | ||
'data': { | ||
'a': 1, | ||
'b': 2, | ||
'c': 3, | ||
}, | ||
}, | ||
}, | ||
ListFormat.multiCompatible, | ||
); | ||
expect(fmStr.length, fm1.length); | ||
|
||
final data = await fd.readAsBytes(); | ||
final result = utf8.decode(data, allowMalformed: true); | ||
|
||
expect(result, contains('name="items[0][name]"')); | ||
expect(result, contains('name="items[0][value]"')); | ||
|
||
expect(result, contains('name="items[1][name]"')); | ||
expect(result, contains('name="items[1][value]"')); | ||
expect(result, contains('name="items[1][value]"')); | ||
|
||
expect(result, contains('name="api[dest]"')); | ||
expect(result, contains('name="api[data][a]"')); | ||
expect(result, contains('name="api[data][b]"')); | ||
expect(result, contains('name="api[data][c]"')); | ||
}); | ||
|
||
test('encodes maps correctly', () async { | ||
final fd = FormData.fromMap({ | ||
'items': [ | ||
{'name': 'foo', 'value': 1}, | ||
{'name': 'bar', 'value': 2}, | ||
], | ||
}); | ||
test('encodes dynamic Map correctly', () async { | ||
final dynamicData = <dynamic, dynamic>{ | ||
'a': 1, | ||
'b': 2, | ||
'c': 3, | ||
}; | ||
|
||
final request = { | ||
'api': { | ||
'dest': '/', | ||
'data': dynamicData, | ||
} | ||
}; | ||
|
||
final fd = FormData.fromMap(request); | ||
final data = await fd.readAsBytes(); | ||
final result = utf8.decode(data, allowMalformed: true); | ||
expect(result, contains('name="api[dest]"')); | ||
expect(result, contains('name="api[data][a]"')); | ||
expect(result, contains('name="api[data][b]"')); | ||
expect(result, contains('name="api[data][c]"')); | ||
}); | ||
|
||
test('posts maps correctly', () async { | ||
final fd = FormData.fromMap( | ||
{ | ||
'items': [ | ||
{'name': 'foo', 'value': 1}, | ||
{'name': 'bar', 'value': 2}, | ||
], | ||
'api': { | ||
'dest': '/', | ||
'data': { | ||
'a': 1, | ||
'b': 2, | ||
'c': 3, | ||
}, | ||
}, | ||
}, | ||
ListFormat.multiCompatible, | ||
); | ||
|
||
final dio = Dio() | ||
..options.baseUrl = EchoAdapter.mockBase | ||
..httpClientAdapter = EchoAdapter(); | ||
|
||
final response = await dio.post( | ||
'/post', | ||
data: fd, | ||
); | ||
|
||
final result = response.data; | ||
expect(result, contains('name="items[0][name]"')); | ||
expect(result, contains('name="items[0][value]"')); | ||
|
||
expect(result, contains('name="items[1][name]"')); | ||
expect(result, contains('name="items[1][value]"')); | ||
expect(result, contains('name="items[1][value]"')); | ||
|
||
expect(result, contains('name="api[dest]"')); | ||
expect(result, contains('name="api[data][a]"')); | ||
expect(result, contains('name="api[data][b]"')); | ||
expect(result, contains('name="api[data][c]"')); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters