mirror of
https://gitee.com/openharmony/request_request
synced 2025-02-21 07:41:24 +00:00
【Bug修复】解决异常入参片段上传的 crash 问题
Signed-off-by: 王柄涵 <wangbinghan1@huawei.com>
This commit is contained in:
parent
a16b8b4232
commit
be3d854983
@ -393,11 +393,7 @@ void JsInitialize::ParseSaveas(napi_env env, napi_value jsConfig, Config &config
|
||||
|
||||
int64_t JsInitialize::ParseBegins(napi_env env, napi_value jsConfig)
|
||||
{
|
||||
if (!NapiUtils::HasNamedProperty(env, jsConfig, "begins")) {
|
||||
return 0;
|
||||
}
|
||||
napi_value value = NapiUtils::GetNamedProperty(env, jsConfig, "begins");
|
||||
int64_t size = NapiUtils::Convert2Int64(env, value);
|
||||
int64_t size = NapiUtils::Convert2Int64(env, jsConfig, "begins");
|
||||
return size >= 0 ? size : 0;
|
||||
}
|
||||
|
||||
@ -406,8 +402,7 @@ int64_t JsInitialize::ParseEnds(napi_env env, napi_value jsConfig)
|
||||
if (!NapiUtils::HasNamedProperty(env, jsConfig, "ends")) {
|
||||
return -1;
|
||||
}
|
||||
napi_value value = NapiUtils::GetNamedProperty(env, jsConfig, "ends");
|
||||
return NapiUtils::Convert2Int64(env, value);
|
||||
return NapiUtils::Convert2Int64(env, jsConfig, "ends");
|
||||
}
|
||||
|
||||
bool JsInitialize::ParseDescription(napi_env env, napi_value jsConfig, std::string &description)
|
||||
|
@ -56,6 +56,20 @@ describe('RequestUploadTest', async function () {
|
||||
type: 'txt'
|
||||
}
|
||||
|
||||
let File1 = {
|
||||
filename: 'test1',
|
||||
name: 'test',
|
||||
uri: 'internal://cache/test1.txt',
|
||||
type: 'txt'
|
||||
}
|
||||
|
||||
let File2 = {
|
||||
filename: 'test2',
|
||||
name: 'test',
|
||||
uri: 'internal://cache/test2.txt',
|
||||
type: 'txt'
|
||||
}
|
||||
|
||||
let uploadConfig = {
|
||||
url: 'http://127.0.0.1',
|
||||
header: {
|
||||
@ -76,6 +90,45 @@ describe('RequestUploadTest', async function () {
|
||||
data: [RequestData]
|
||||
};
|
||||
|
||||
let uploadConfig2 = {
|
||||
url: 'http://127.0.0.1',
|
||||
header: {
|
||||
headers: 'http'
|
||||
},
|
||||
index: 1,
|
||||
begins: 50,
|
||||
ends: 100,
|
||||
method: 'POST',
|
||||
files: [File1, File2],
|
||||
data: [RequestData]
|
||||
};
|
||||
|
||||
let uploadConfig3 = {
|
||||
url: 'http://127.0.0.1',
|
||||
header: {
|
||||
headers: 'http'
|
||||
},
|
||||
index: 1,
|
||||
begins: "50",
|
||||
ends: 100,
|
||||
method: 'POST',
|
||||
files: [File1, File2],
|
||||
data: [RequestData]
|
||||
};
|
||||
|
||||
let uploadConfig4 = {
|
||||
url: 'http://127.0.0.1',
|
||||
header: {
|
||||
headers: 'http'
|
||||
},
|
||||
index: 1,
|
||||
begins: undefined,
|
||||
ends: 100,
|
||||
method: 'POST',
|
||||
files: [File1, File2],
|
||||
data: [RequestData]
|
||||
};
|
||||
|
||||
/**
|
||||
* @tc.number SUB_REQUEST_UPLOAD_API_0001
|
||||
* @tc.name Test requestUploadTest type = TIMER_TYPE_REALTIME
|
||||
@ -354,5 +407,131 @@ describe('RequestUploadTest', async function () {
|
||||
done();
|
||||
});
|
||||
|
||||
/**
|
||||
* @tc.number SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001
|
||||
* @tc.name Test function of partial upload.
|
||||
* @tc.desc Test if the partial upload success or fail.
|
||||
* @tc.size : MEDIUM
|
||||
* @tc.type : Function
|
||||
* @tc.level : Level 0
|
||||
*/
|
||||
it('SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001', 0, async function (done) {
|
||||
console.info("====>-----------------------SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 is starting-----------------------");
|
||||
try {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 UploadConfig2 = " + JSON.stringify(uploadConfig2));
|
||||
// @ts-ignore
|
||||
await request.uploadFile(globalThis.abilityContext, uploadConfig2).then((uploadTask) => {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 uploadTask = " + uploadTask);
|
||||
expect(true).assertEqual((uploadTask != undefined));
|
||||
let complete_callback = async (tasks) => {
|
||||
try {
|
||||
uploadTask.off('complete');
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 tasks0 = " + JSON.stringify(tasks[0])
|
||||
+ ", path = " + tasks[0].path + ", responseCode" + tasks[0].responseCode + ", message = " + tasks[0].message);
|
||||
let bool = await uploadTask.delete();
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 remove result = " + bool);
|
||||
done();
|
||||
} catch (err) {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 fail = " + JSON.stringify(err));
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 headerReceive success");
|
||||
uploadTask.on('complete', complete_callback);
|
||||
|
||||
}).catch((err) => {
|
||||
console.error("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 catch err: " + JSON.stringify(err));
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 fail error: " + JSON.stringify(err));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @tc.number SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002
|
||||
* @tc.name Test function of partial upload.
|
||||
* @tc.desc Test if the partial upload with abnormal input success or fail.
|
||||
* @tc.size : MEDIUM
|
||||
* @tc.type : Function
|
||||
* @tc.level : Level 0
|
||||
*/
|
||||
it('SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002', 0, async function (done) {
|
||||
console.info("====>-----------------------SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 is starting-----------------------");
|
||||
try {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 UploadConfig3 = " + JSON.stringify(UploadConfig3));
|
||||
// @ts-ignore
|
||||
await request.uploadFile(globalThis.abilityContext, UploadConfig3).then((uploadTask) => {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 uploadTask = " + uploadTask);
|
||||
expect(true).assertEqual((uploadTask != undefined));
|
||||
let complete_callback = async (tasks) => {
|
||||
try {
|
||||
uploadTask.off('complete');
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 tasks0 = " + JSON.stringify(tasks[0])
|
||||
+ ", path = " + tasks[0].path + ", responseCode" + tasks[0].responseCode + ", message = " + tasks[0].message);
|
||||
let bool = await uploadTask.delete();
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 remove result = " + bool);
|
||||
done();
|
||||
} catch (err) {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 fail = " + JSON.stringify(err));
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 headerReceive success");
|
||||
uploadTask.on('complete', complete_callback);
|
||||
|
||||
}).catch((err) => {
|
||||
console.error("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 catch err: " + JSON.stringify(err));
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0002 fail error: " + JSON.stringify(err));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @tc.number SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003
|
||||
* @tc.name Test function of partial upload.
|
||||
* @tc.desc Test if the partial upload with abnormal input success or fail.
|
||||
* @tc.size : MEDIUM
|
||||
* @tc.type : Function
|
||||
* @tc.level : Level 0
|
||||
*/
|
||||
it('SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003', 0, async function (done) {
|
||||
console.info("====>-----------------------SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 is starting-----------------------");
|
||||
try {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 UploadConfig4 = " + JSON.stringify(UploadConfig4));
|
||||
// @ts-ignore
|
||||
await request.uploadFile(globalThis.abilityContext, UploadConfig4).then((uploadTask) => {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 uploadTask = " + uploadTask);
|
||||
expect(true).assertEqual((uploadTask != undefined));
|
||||
let complete_callback = async (tasks) => {
|
||||
try {
|
||||
uploadTask.off('complete');
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0001 tasks0 = " + JSON.stringify(tasks[0])
|
||||
+ ", path = " + tasks[0].path + ", responseCode" + tasks[0].responseCode + ", message = " + tasks[0].message);
|
||||
let bool = await uploadTask.delete();
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 remove result = " + bool);
|
||||
done();
|
||||
} catch (err) {
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 fail = " + JSON.stringify(err));
|
||||
done();
|
||||
}
|
||||
}
|
||||
|
||||
console.info("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 headerReceive success");
|
||||
uploadTask.on('complete', complete_callback);
|
||||
|
||||
}).catch((err) => {
|
||||
console.error("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 catch err: " + JSON.stringify(err));
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("====>SUB_REQUEST_UPLOADFILE_API_PARTIAL_0003 fail error: " + JSON.stringify(err));
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
console.log(TAG + "*************Unit Test End*************");
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user