添加表单字段参数验证和测试
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
This commit is contained in:
@@ -75,6 +75,12 @@ public class CommonUploadParam implements Serializable {
|
|||||||
* @return 当前对象,支持链式调用
|
* @return 当前对象,支持链式调用
|
||||||
*/
|
*/
|
||||||
public CommonUploadParam addFormField(String fieldName, String fieldValue) {
|
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) {
|
if (this.formFields == null) {
|
||||||
this.formFields = new HashMap<>();
|
this.formFields = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,4 +95,25 @@ public class CommonUploadParamTest {
|
|||||||
Assert.assertTrue(str.contains("name:media"));
|
Assert.assertTrue(str.contains("name:media"));
|
||||||
Assert.assertTrue(str.contains("formFields:"));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user