fix patch check

This commit is contained in:
Looly 2021-07-09 20:29:47 +08:00
parent b38f40a6a3
commit d68cc83b7d
3 changed files with 25 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package cn.hutool.http; package cn.hutool.http;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
import cn.hutool.http.cookie.GlobalCookieManager; import cn.hutool.http.cookie.GlobalCookieManager;
@ -83,14 +84,23 @@ public class HttpGlobalConfig implements Serializable {
return; return;
} }
final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods"); final Field methodsField = ReflectUtil.getField(HttpURLConnection.class, "methods");
if (null != methodsField) { if (null == methodsField) {
throw new HttpException("None static field [methods] with Java version: [{}]", System.getProperty("java.version"));
}
// 去除final修饰 // 去除final修饰
ReflectUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL); ReflectUtil.setFieldValue(methodsField, "modifiers", methodsField.getModifiers() & ~Modifier.FINAL);
final String[] methods = { final String[] methods = {
"GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH" "GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE", "PATCH"
}; };
ReflectUtil.setFieldValue(null, methodsField, methods); ReflectUtil.setFieldValue(null, methodsField, methods);
// 检查注入是否成功
final Object staticFieldValue = ReflectUtil.getStaticFieldValue(methodsField);
if(false == ArrayUtil.equals(methods, staticFieldValue)){
throw new HttpException("Inject value to field [methods] failed!");
}
isAllowPatch = true; isAllowPatch = true;
} }
} }
}

View File

@ -248,6 +248,7 @@ public class HttpUtilTest {
@Test @Test
@Ignore @Ignore
public void patchTest() { public void patchTest() {
// 验证patch请求是否可用
String body = HttpRequest.patch("https://www.baidu.com").execute().body(); String body = HttpRequest.patch("https://www.baidu.com").execute().body();
Console.log(body); Console.log(body);
} }

View File

@ -26,6 +26,12 @@ public class SystemUtilTest {
Assert.assertNotNull(javaInfo); Assert.assertNotNull(javaInfo);
} }
@Test
public void getJavaRuntimeInfoTest() {
JavaRuntimeInfo info = SystemUtil.getJavaRuntimeInfo();
Assert.assertNotNull(info);
}
@Test @Test
public void getOsInfoTest() { public void getOsInfoTest() {
OsInfo osInfo = SystemUtil.getOsInfo(); OsInfo osInfo = SystemUtil.getOsInfo();