Skip to content

Commit

Permalink
fix cfug#316
Browse files Browse the repository at this point in the history
  • Loading branch information
wendux committed May 29, 2019
1 parent b6bdf11 commit a8877d1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
17 changes: 7 additions & 10 deletions example/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ main() async {
"param2": "-1",
"param3": "-1",
"param4": "-1",
"param5": "-1",
"param8":{
"a":"b",
"b":"c"
},
"param5": null,
"param8": {"a": "b", "b": "c"},
"music": new UploadFileInfo(new File("./example/bee.mp4"), "be.mp4"),
});
var v=File("./example/audio.m4a");
print(v.lengthSync());
var t= await formData.asBytesAsync();

var t = await formData.asBytesAsync();
print("formStreamSize = ${t.length}");
print("formData.length = ${formData.length}");
print(formData.length==t.length);
print(formData);
print(formData.length == t.length);
//print(formData);

}
32 changes: 22 additions & 10 deletions package_src/lib/src/form_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ class FormData extends MapMixin<String, dynamic> {
int get _fileFieldLength {
if (_fileFieldLen == null) {
_fileFieldLen = _chunkHeader(
StringBuffer(),
"",
UploadFileInfo(null, "", contentType: ContentType.text),
).length -
StringBuffer(),
"",
UploadFileInfo(null, "", contentType: ContentType.text),
).length -
utf8.encode(ContentType.text.mimeType).length;
}
return _fileFieldLen;
Expand Down Expand Up @@ -120,6 +120,7 @@ class FormData extends MapMixin<String, dynamic> {
writeMapLength(buf, key, value) {
value.keys.toList().forEach((mapKey) {
var nestedKey = '${key}[${mapKey}]';
if (value[mapKey]) return;
if (value[mapKey] is Map) {
writeMapLength(buf, nestedKey, value[mapKey]);
} else if (value[mapKey] is UploadFileInfo) {
Expand All @@ -136,18 +137,21 @@ class FormData extends MapMixin<String, dynamic> {
}

_map.forEach((key, value) {
if (value == null) return;
if (value is UploadFileInfo || value is List) {
fileMap[key] = value;
return;
} else if (value is Map) {
value.keys.toList().forEach((mapKey) {
var nestedKey = '${key}[${mapKey}]';
if (value[mapKey] == null) return;
if (value[mapKey] is Map) {
writeMapLength(buf, nestedKey, value[mapKey]);
} else if (value[mapKey] is UploadFileInfo) {
len += _fileFieldLength;
_fileInfo(nestedKey, value[mapKey]);
len += value[mapKey].bytes?.length ?? value[mapKey].file.lengthSync();
len +=
value[mapKey].bytes?.length ?? value[mapKey].file.lengthSync();
len += lineSplitLen;
} else {
len += _textFieldLength;
Expand All @@ -162,6 +166,7 @@ class FormData extends MapMixin<String, dynamic> {
});

fileMap.forEach((key, fileInfo) {
if (fileInfo == null) return;
if (fileInfo is UploadFileInfo) {
len += _fileFieldLength;
_fileInfo(key, fileInfo);
Expand All @@ -177,6 +182,7 @@ class FormData extends MapMixin<String, dynamic> {
} else if (e is Map) {
e.keys.toList().forEach((mapKey) {
var nestedKey = '${key}[][${mapKey}]';
if (e[mapKey] == null) return;
if (e[mapKey] is Map) {
writeMapLength(buf, nestedKey, e[mapKey]);
} else if (e[mapKey] is UploadFileInfo) {
Expand Down Expand Up @@ -215,6 +221,7 @@ class FormData extends MapMixin<String, dynamic> {
var fileMap = new Map<String, dynamic>();
StringBuffer data = new StringBuffer();
_map.forEach((key, value) {
if (value == null) return;
if (value is UploadFileInfo || value is List) {
// If file, add it to `fileMap`, we handle it later.
fileMap[key] = value;
Expand Down Expand Up @@ -251,12 +258,14 @@ class FormData extends MapMixin<String, dynamic> {
}
return bytes;
}

handleMapField(List<int> bytes, String key, dynamic value) {
if (value == null) return;
StringBuffer buffer = new StringBuffer();
if (value is Map) {
value.keys.toList().forEach((mapKey) {
var nestedKey = '${key}[${mapKey}]';
if (value[mapKey] == null) return;
if (value[mapKey] is Map) {
handleMapField(bytes, nestedKey, value[mapKey]);
} else if (value[mapKey] is UploadFileInfo) {
Expand Down Expand Up @@ -293,10 +302,10 @@ class FormData extends MapMixin<String, dynamic> {
}

List<int> _chunkHeader(
StringBuffer buffer,
String key,
UploadFileInfo fileInfo,
) {
StringBuffer buffer,
String key,
UploadFileInfo fileInfo,
) {
buffer.clear();
buffer.write(boundary);
_writeln(buffer);
Expand Down Expand Up @@ -333,6 +342,7 @@ class FormData extends MapMixin<String, dynamic> {
}

for (var entry in _map.entries) {
if(entry.value==null) continue;
if (entry.value is UploadFileInfo || entry.value is List) {
// If file, add it to `fileMap`, we handle it later.
fileMap[entry.key] = entry.value;
Expand All @@ -347,12 +357,14 @@ class FormData extends MapMixin<String, dynamic> {
}

for (var entry in fileMap.entries) {
if(entry.value==null) continue;
if (entry.value is UploadFileInfo) {
await for (var chunk in addFile(entry.key, entry.value)) {
yield chunk;
}
} else {
for (var info in entry.value) {
if(info==null) continue;
if (info is UploadFileInfo) {
await for (var chunk in addFile(entry.key, info)) {
yield chunk;
Expand Down
40 changes: 20 additions & 20 deletions package_src/test/dio_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,28 +67,26 @@ void main() {
dio.options.baseUrl = MockAdapter.mockBase;
dio.options.headers = {'User-Agent': 'dartisan', 'XX': '8'};
dio.httpClientAdapter = MockAdapter();

});
test('test', () async {
// dio.options.responseType=ResponseType.json // Default
Response<Map>r0 =await dio.get("/test");
expect(r0.data is Map,true);
Response<String>r =await dio.get<String>("/test");
expect(r.data is String,true);
dio.options.responseType=ResponseType.plain;
r =await dio.get("/test");
expect(r.data is String,true);
Response<Map>r2 =await dio.get<Map>("/test");
expect(r2.data is Map,true);

Response<Map> r0 = await dio.get("/test");
expect(r0.data is Map, true);
Response<String> r = await dio.get<String>("/test");
expect(r.data is String, true);
dio.options.responseType = ResponseType.plain;
r = await dio.get("/test");
expect(r.data is String, true);
Response<Map> r2 = await dio.get<Map>("/test");
expect(r2.data is Map, true);
});
});

group('download', () {
test("test", () async {
var dio = new Dio();
dio.options.baseUrl = MockAdapter.mockBase;
dio.httpClientAdapter=MockAdapter();
dio.httpClientAdapter = MockAdapter();
await dio.download("/download", "../download.md",
options: Options(
headers: {HttpHeaders.acceptEncodingHeader: "*"}), // disable gzip
Expand All @@ -108,17 +106,20 @@ void main() {
var dio = new Dio();
dio.interceptors.add(LogInterceptor(requestBody: true));
dio.options.baseUrl = MockAdapter.mockBase;
dio.httpClientAdapter=MockAdapter();
dio.httpClientAdapter = MockAdapter();
FormData formData = FormData.from({
"name": "wendux",
"age": 25,
"other": {"a": 1, "b": 2}
});
formData.remove("name");
formData["xx"] = 9;
formData.add(
"file",
UploadFileInfo(File("./pubspec.yaml"), "pubspec.yaml"),
);
var t = await formData.asBytesAsync();
expect(formData.length, t.length);
await dio.post("/test", data: formData);
formData.clear();
expect(formData.length, 0);
Expand All @@ -127,11 +128,11 @@ void main() {
var dio = new Dio();
dio.interceptors.add(LogInterceptor(requestBody: true));
dio.options.baseUrl = MockAdapter.mockBase;
dio.httpClientAdapter=MockAdapter();
dio.httpClientAdapter = MockAdapter();
FormData formData = FormData.from({
"name": "wendux",
"tag":["a","b","c"],
"student":{"a":"b","c": "d"},
"tag": ["a", "b", "c"],
"student": {"a": "b", "c": "d"},
"age": 25,
});
formData.remove("name");
Expand Down Expand Up @@ -186,7 +187,7 @@ void main() {
group('transfomer', () {
test("test", () async {
var dio = new Dio();
dio.httpClientAdapter=MockAdapter();
dio.httpClientAdapter = MockAdapter();
dio.options.baseUrl = MockAdapter.mockBase;
dio.transformer = new MyTransformer();
// Response response = await dio.get("https://www.baidu.com");
Expand Down Expand Up @@ -221,7 +222,7 @@ void main() {
setUp(() {
dio = new Dio();
dio.options.baseUrl = MockAdapter.mockBase;
dio.httpClientAdapter=MockAdapter();
dio.httpClientAdapter = MockAdapter();
dio.interceptors
.add(InterceptorsWrapper(onRequest: (RequestOptions options) async {
switch (options.path) {
Expand Down Expand Up @@ -250,8 +251,7 @@ void main() {
});

test('TestRI', () async {
Response
response = await dio.get("/fakepath1");
Response response = await dio.get("/fakepath1");
expect(response.data, "fake data");
response = await dio.get("/fakepath2");
expect(response.data["errCode"], 0);
Expand Down

0 comments on commit a8877d1

Please sign in to comment.