fix: Set default request body to prevent NullPointerException

This commit is contained in:
Jiang Jining 2021-03-13 00:47:08 +08:00
parent 0f1d760dbe
commit 5834963fdd
2 changed files with 26 additions and 2 deletions

View File

@ -60,8 +60,13 @@ public class HttpProcessor extends CommonBasicProcessor {
}
// set default mediaType
if (!"GET".equals(httpParams.method) && StringUtils.isEmpty(httpParams.mediaType)) {
if (JSONValidator.from(httpParams.body).validate()) {
if (!"GET".equals(httpParams.method)) {
// set default request body
if (StringUtils.isEmpty(httpParams.body)) {
httpParams.body = new JSONObject().toJSONString();
omsLogger.warn("try to use default request body:{}", httpParams.body);
}
if (JSONValidator.from(httpParams.body).validate() && StringUtils.isEmpty(httpParams.mediaType)) {
httpParams.mediaType = "application/json";
omsLogger.warn("try to use 'application/json' as media type");
}

View File

@ -41,6 +41,25 @@ class HttpProcessorTest {
System.out.println(new HttpProcessor().process(TestUtils.genTaskContext(params.toJSONString())));
}
@Test
void testPostDefaultJson() throws Exception {
String url = "https://mock.uutool.cn/4f5qfgcdahj0?test=true";
JSONObject params = new JSONObject();
params.put("url", url);
params.put("method", "POST");
System.out.println(new HttpProcessor().process(TestUtils.genTaskContext(params.toJSONString())));
}
@Test
void testPostDefaultWithMediaType() throws Exception {
String url = "https://mock.uutool.cn/4f5qfgcdahj0?test=true";
JSONObject params = new JSONObject();
params.put("url", url);
params.put("method", "POST");
params.put("mediaType", "application/json");
System.out.println(new HttpProcessor().process(TestUtils.genTaskContext(params.toJSONString())));
}
@Test
void testTimeout() throws Exception {