Skip to content

Commit

Permalink
Adjust test setting custom MultipartFile.contentType (#1939)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->
Test for #1938

### 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
- [ ] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [ ] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

<!-- Provide more context and info about the PR. -->
  • Loading branch information
kuhnroyal authored Aug 16, 2023
1 parent c7cf974 commit 3ad3947
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 94 deletions.
97 changes: 4 additions & 93 deletions dio/test/formdata_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:io';

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

import 'mock/adapters.dart';
Expand Down Expand Up @@ -35,6 +36,7 @@ void main() async {
headers: {
'test': <String>['c']
},
contentType: MediaType.parse('text/plain'),
),
]
});
Expand Down Expand Up @@ -86,6 +88,7 @@ void main() async {
headers: {
'test': <String>['c'],
},
contentType: MediaType.parse('text/plain'),
),
),
);
Expand Down Expand Up @@ -121,6 +124,7 @@ void main() async {
headers: {
'test': <String>['c']
},
contentType: MediaType.parse('text/plain'),
),
]
});
Expand Down Expand Up @@ -148,99 +152,6 @@ void main() async {
testOn: 'vm',
);

// Cloned multipart files should be able to be read again and be the same
// as the original ones.
test(
'complex cloning MultipartFile',
() async {
final multipartFile1 = MultipartFile.fromString(
'hello world.',
headers: {
'test': <String>['a']
},
);
final multipartFile2 = await MultipartFile.fromFile(
'test/mock/_testfile',
filename: '1.txt',
headers: {
'test': <String>['b']
},
);
final multipartFile3 = MultipartFile.fromFileSync(
'test/mock/_testfile',
filename: '2.txt',
headers: {
'test': <String>['c']
},
);

final fm = FormData.fromMap({
'name': 'wendux',
'age': 25,
'path': '/图片空间/地址',
'file': multipartFile1,
'files': [
multipartFile2,
multipartFile3,
]
});
final fmStr = await fm.readAsBytes();

// Files are finalized after being read.
try {
multipartFile1.finalize();
fail('Should not be able to finalize a file twice.');
} catch (e) {
expect(e, isA<StateError>());
expect(
(e as StateError).message,
'The MultipartFile has already been finalized. This typically '
'means you are using the same MultipartFile in repeated requests.',
);
}

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',
multipartFile1.clone(),
),
);
fm1.files.add(
MapEntry(
'files',
multipartFile2.clone(),
),
);
fm1.files.add(
MapEntry(
'files',
multipartFile3.clone(),
),
);
expect(fmStr.length, fm1.length);

// The cloned multipart files should be able to be read again.
expect(fm.files[0].value.isFinalized, true);
expect(fm.files[1].value.isFinalized, true);
expect(fm.files[2].value.isFinalized, true);
expect(fm1.files[0].value.isFinalized, false);
expect(fm1.files[1].value.isFinalized, false);
expect(fm1.files[2].value.isFinalized, false);

// The cloned multipart files' properties should be the same as the
// original ones.
expect(fm1.files[0].value.filename, multipartFile1.filename);
expect(fm1.files[0].value.contentType, multipartFile1.contentType);
expect(fm1.files[0].value.length, multipartFile1.length);
expect(fm1.files[0].value.headers, multipartFile1.headers);
},
testOn: 'vm',
);

test('encodes maps correctly', () async {
final fd = FormData.fromMap(
{
Expand Down
2 changes: 1 addition & 1 deletion dio/test/mock/_formdata
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test: b

----dio-boundary-3788753558
content-disposition: form-data; name="files"; filename="2.txt"
content-type: application/octet-stream
content-type: text/plain
test: c

你好世界,
Expand Down
114 changes: 114 additions & 0 deletions dio/test/multipart_file_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import 'package:dio/dio.dart';
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';

void main() async {
group(MultipartFile, () {
test(
'fromFile sets correct content-type',
() async {
final mediaType = MediaType.parse('text/plain');
final file = await MultipartFile.fromFile(
'test/mock/_testfile',
filename: '1.txt',
contentType: mediaType,
);
expect(file.contentType, mediaType);
},
testOn: 'vm',
);

// Cloned multipart files should be able to be read again and be the same
// as the original ones.
test(
'complex cloning MultipartFile',
() async {
final multipartFile1 = MultipartFile.fromString(
'hello world.',
headers: {
'test': <String>['a']
},
);
final multipartFile2 = await MultipartFile.fromFile(
'test/mock/_testfile',
filename: '1.txt',
headers: {
'test': <String>['b']
},
);
final multipartFile3 = MultipartFile.fromFileSync(
'test/mock/_testfile',
filename: '2.txt',
headers: {
'test': <String>['c']
},
);

final fm = FormData.fromMap({
'name': 'wendux',
'age': 25,
'path': '/图片空间/地址',
'file': multipartFile1,
'files': [
multipartFile2,
multipartFile3,
]
});
final fmStr = await fm.readAsBytes();

// Files are finalized after being read.
try {
multipartFile1.finalize();
fail('Should not be able to finalize a file twice.');
} catch (e) {
expect(e, isA<StateError>());
expect(
(e as StateError).message,
'The MultipartFile has already been finalized. This typically '
'means you are using the same MultipartFile in repeated requests.',
);
}

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',
multipartFile1.clone(),
),
);
fm1.files.add(
MapEntry(
'files',
multipartFile2.clone(),
),
);
fm1.files.add(
MapEntry(
'files',
multipartFile3.clone(),
),
);
expect(fmStr.length, fm1.length);

// The cloned multipart files should be able to be read again.
expect(fm.files[0].value.isFinalized, true);
expect(fm.files[1].value.isFinalized, true);
expect(fm.files[2].value.isFinalized, true);
expect(fm1.files[0].value.isFinalized, false);
expect(fm1.files[1].value.isFinalized, false);
expect(fm1.files[2].value.isFinalized, false);

// The cloned multipart files' properties should be the same as the
// original ones.
expect(fm1.files[0].value.filename, multipartFile1.filename);
expect(fm1.files[0].value.contentType, multipartFile1.contentType);
expect(fm1.files[0].value.length, multipartFile1.length);
expect(fm1.files[0].value.headers, multipartFile1.headers);
},
testOn: 'vm',
);
});
}

0 comments on commit 3ad3947

Please sign in to comment.