diff --git a/framework/src/main/java/org/tron/core/services/http/Util.java b/framework/src/main/java/org/tron/core/services/http/Util.java index 186f77cf3e8..6874ec875d0 100644 --- a/framework/src/main/java/org/tron/core/services/http/Util.java +++ b/framework/src/main/java/org/tron/core/services/http/Util.java @@ -30,6 +30,7 @@ import org.apache.commons.lang3.StringUtils; import org.bouncycastle.util.encoders.Hex; import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.http.MimeTypes; import org.eclipse.jetty.util.MultiMap; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.UrlEncoded; @@ -78,8 +79,6 @@ public class Util { public static final String FUNCTION_SELECTOR = "function_selector"; public static final String FUNCTION_PARAMETER = "parameter"; public static final String CALL_DATA = "data"; - public static final String APPLICATION_FORM_URLENCODED = "application/x-www-form-urlencoded"; - public static final String APPLICATION_JSON = "application/json"; public static String printTransactionFee(String transactionFee) { JSONObject jsonObject = new JSONObject(); @@ -525,7 +524,6 @@ public static byte[] getAddress(HttpServletRequest request) throws Exception { private static String checkGetParam(HttpServletRequest request, String key) throws Exception { String method = request.getMethod(); - String value = null; if (HttpMethod.GET.toString().toUpperCase().equalsIgnoreCase(method)) { return request.getParameter(key); @@ -535,8 +533,10 @@ private static String checkGetParam(HttpServletRequest request, String key) thro if (StringUtils.isBlank(contentType)) { return null; } - if (APPLICATION_JSON.toLowerCase().contains(contentType)) { - value = getRequestValue(request); + if (contentType.contains(MimeTypes.Type.FORM_ENCODED.asString())) { + return request.getParameter(key); + } else { + String value = getRequestValue(request); if (StringUtils.isBlank(value)) { return null; } @@ -545,13 +545,10 @@ private static String checkGetParam(HttpServletRequest request, String key) thro if (jsonObject != null) { return jsonObject.getString(key); } - } else if (APPLICATION_FORM_URLENCODED.toLowerCase().contains(contentType)) { - return request.getParameter(key); - } else { return null; } } - return value; + return null; } public static String getRequestValue(HttpServletRequest request) throws IOException { diff --git a/framework/src/test/java/org/tron/core/services/http/GetBrokerageServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetBrokerageServletTest.java index c7fa8f7fc58..ffe8cc6c22e 100644 --- a/framework/src/test/java/org/tron/core/services/http/GetBrokerageServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/GetBrokerageServletTest.java @@ -52,6 +52,25 @@ public void getBrokerageValueByJsonTest() { } } + + @Test + public void getBrokerageByJsonUTF8Test() { + int expect = 20; + String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; + MockHttpServletRequest request = createRequest("application/json; charset=utf-8"); + request.setContent(jsonParam.getBytes()); + MockHttpServletResponse response = new MockHttpServletResponse(); + getBrokerageServlet.doPost(request, response); + try { + String contentAsString = response.getContentAsString(); + JSONObject result = JSONObject.parseObject(contentAsString); + int brokerage = (int)result.get("brokerage"); + Assert.assertEquals(expect, brokerage); + } catch (UnsupportedEncodingException e) { + Assert.fail(e.getMessage()); + } + } + @Test public void getBrokerageValueTest() { int expect = 20; diff --git a/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java b/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java index c8ae67a4a79..404e154a4c3 100644 --- a/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/GetRewardServletTest.java @@ -8,9 +8,11 @@ import java.io.UnsupportedEncodingException; import javax.annotation.Resource; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.junit.After; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -22,6 +24,7 @@ import org.tron.core.service.MortgageService; import org.tron.core.store.DelegationStore; +@Slf4j public class GetRewardServletTest extends BaseTest { @Resource @@ -52,6 +55,7 @@ public MockHttpServletRequest createRequest(String contentType) { return request; } + @Before public void init() { manager.getDynamicPropertiesStore().saveChangeDelegation(1); byte[] sr = decodeFromBase58Check("27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh"); @@ -61,7 +65,6 @@ public void init() { @Test public void getRewardValueByJsonTest() { - init(); int expect = 138181; String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; MockHttpServletRequest request = createRequest("application/json"); @@ -78,9 +81,26 @@ public void getRewardValueByJsonTest() { } } + @Test + public void getRewardByJsonUTF8Test() { + int expect = 138181; + String jsonParam = "{\"address\": \"27VZHn9PFZwNh7o2EporxmLkpe157iWZVkh\"}"; + MockHttpServletRequest request = createRequest("application/json; charset=utf-8"); + MockHttpServletResponse response = new MockHttpServletResponse(); + request.setContent(jsonParam.getBytes()); + try { + getRewardServlet.doPost(request, response); + String contentAsString = response.getContentAsString(); + JSONObject result = JSONObject.parseObject(contentAsString); + int reward = (int)result.get("reward"); + Assert.assertEquals(expect, reward); + } catch (UnsupportedEncodingException e) { + Assert.fail(e.getMessage()); + } + } + @Test public void getRewardValueTest() { - init(); int expect = 138181; MockHttpServletRequest request = createRequest("application/x-www-form-urlencoded"); MockHttpServletResponse response = new MockHttpServletResponse();