1
0
mirror of synced 2026-02-07 19:27:50 +08:00

添加表单字段参数验证和测试

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-19 03:59:24 +00:00
parent 7e34702177
commit 9330dd535a
2 changed files with 27 additions and 0 deletions

View File

@@ -75,6 +75,12 @@ public class CommonUploadParam implements Serializable {
* @return 当前对象,支持链式调用
*/
public CommonUploadParam addFormField(String fieldName, String fieldValue) {
if (fieldName == null || fieldName.trim().isEmpty()) {
throw new IllegalArgumentException("表单字段名不能为空");
}
if (fieldValue == null) {
throw new IllegalArgumentException("表单字段值不能为null");
}
if (this.formFields == null) {
this.formFields = new HashMap<>();
}

View File

@@ -95,4 +95,25 @@ public class CommonUploadParamTest {
Assert.assertTrue(str.contains("name:media"));
Assert.assertTrue(str.contains("formFields:"));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testAddFormFieldWithNullFieldName() {
File file = new File("test.txt");
CommonUploadParam param = CommonUploadParam.fromFile("media", file);
param.addFormField(null, "value");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testAddFormFieldWithEmptyFieldName() {
File file = new File("test.txt");
CommonUploadParam param = CommonUploadParam.fromFile("media", file);
param.addFormField("", "value");
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testAddFormFieldWithNullFieldValue() {
File file = new File("test.txt");
CommonUploadParam param = CommonUploadParam.fromFile("media", file);
param.addFormField("fieldName", null);
}
}