|
| 1 | +package org.tron.core.services.http; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertFalse; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.junit.Assert.fail; |
| 7 | + |
| 8 | +import com.alibaba.fastjson.JSONObject; |
| 9 | +import java.io.UnsupportedEncodingException; |
| 10 | +import javax.annotation.Resource; |
| 11 | +import org.junit.After; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 15 | +import org.springframework.mock.web.MockHttpServletResponse; |
| 16 | +import org.tron.common.BaseTest; |
| 17 | +import org.tron.common.TestConstants; |
| 18 | +import org.tron.core.config.args.Args; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration tests for GetBlockServlet. This servlet delegates both doGet and doPost to |
| 22 | + * a private handle() method whose parseParams() reads visible from either URL query (GET) |
| 23 | + * or JSON body (POST). The int64_as_string flag follows the same split (URL on GET, body |
| 24 | + * on POST) but is read via the centralized RateLimiterServlet.service / PostParams paths |
| 25 | + * rather than parseParams itself. |
| 26 | + */ |
| 27 | +public class GetBlockServletTest extends BaseTest { |
| 28 | + |
| 29 | + @Resource |
| 30 | + private GetBlockServlet getBlockServlet; |
| 31 | + |
| 32 | + static { |
| 33 | + Args.setParam( |
| 34 | + new String[]{ |
| 35 | + "--output-directory", dbPath(), |
| 36 | + }, TestConstants.TEST_CONF |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + @Before |
| 41 | + public void clearBefore() { |
| 42 | + JsonFormat.clearInt64AsString(); |
| 43 | + } |
| 44 | + |
| 45 | + @After |
| 46 | + public void clearAfter() { |
| 47 | + JsonFormat.clearInt64AsString(); |
| 48 | + } |
| 49 | + |
| 50 | + private MockHttpServletRequest createGetRequest() { |
| 51 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 52 | + request.setMethod("GET"); |
| 53 | + return request; |
| 54 | + } |
| 55 | + |
| 56 | + private MockHttpServletRequest createPostJsonRequest(String jsonBody) { |
| 57 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 58 | + request.setMethod("POST"); |
| 59 | + request.setContentType("application/json"); |
| 60 | + request.setCharacterEncoding("UTF-8"); |
| 61 | + request.setContent(jsonBody.getBytes()); |
| 62 | + return request; |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testGetDefaultKeepsInt64AsNumber() { |
| 67 | + // Default GET: parseParams reads visible/int64_as_string from URL query, both absent. |
| 68 | + // Response int64 fields (timestamp, number) must remain unquoted. |
| 69 | + MockHttpServletRequest request = createGetRequest(); |
| 70 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 71 | + getBlockServlet.doGet(request, response); |
| 72 | + try { |
| 73 | + String body = response.getContentAsString(); |
| 74 | + assertNotNull(JSONObject.parseObject(body)); |
| 75 | + if (body.contains("\"timestamp\"")) { |
| 76 | + assertTrue("timestamp should be unquoted by default, got: " + body, |
| 77 | + body.matches("(?s).*\"timestamp\"\\s*:\\s*\\d+.*")); |
| 78 | + } |
| 79 | + } catch (UnsupportedEncodingException e) { |
| 80 | + fail(e.getMessage()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testGetInt64AsStringFromQuery() { |
| 86 | + // GET URL query is read by RateLimiterServlet.service in production; tests bypass |
| 87 | + // service, so we simulate that step here. |
| 88 | + MockHttpServletRequest request = createGetRequest(); |
| 89 | + request.addParameter("int64_as_string", "true"); |
| 90 | + JsonFormat.setInt64AsString(Util.getInt64AsString(request)); |
| 91 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 92 | + getBlockServlet.doGet(request, response); |
| 93 | + try { |
| 94 | + String body = response.getContentAsString(); |
| 95 | + assertNotNull(JSONObject.parseObject(body)); |
| 96 | + if (body.contains("\"timestamp\"")) { |
| 97 | + assertTrue("timestamp should be quoted when int64_as_string=true, got: " + body, |
| 98 | + body.matches("(?s).*\"timestamp\"\\s*:\\s*\"\\d+\".*")); |
| 99 | + } |
| 100 | + } catch (UnsupportedEncodingException e) { |
| 101 | + fail(e.getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testPostInt64AsStringFromBody() { |
| 107 | + // POST branch delegates to PostParams.getPostParams which reads int64_as_string |
| 108 | + // from the JSON body (identical semantics to the visible flag). |
| 109 | + MockHttpServletRequest request = createPostJsonRequest( |
| 110 | + "{\"int64_as_string\": true}"); |
| 111 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 112 | + getBlockServlet.doPost(request, response); |
| 113 | + try { |
| 114 | + String body = response.getContentAsString(); |
| 115 | + assertNotNull(JSONObject.parseObject(body)); |
| 116 | + if (body.contains("\"timestamp\"")) { |
| 117 | + assertTrue("timestamp should be quoted when body carries int64_as_string=true, got: " |
| 118 | + + body, body.matches("(?s).*\"timestamp\"\\s*:\\s*\"\\d+\".*")); |
| 119 | + } |
| 120 | + } catch (UnsupportedEncodingException e) { |
| 121 | + fail(e.getMessage()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testPostInt64AsStringFalseKeepsNumber() { |
| 127 | + // Regression: explicitly false behaves like the default. |
| 128 | + MockHttpServletRequest request = createPostJsonRequest( |
| 129 | + "{\"int64_as_string\": false}"); |
| 130 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 131 | + getBlockServlet.doPost(request, response); |
| 132 | + try { |
| 133 | + String body = response.getContentAsString(); |
| 134 | + assertNotNull(JSONObject.parseObject(body)); |
| 135 | + if (body.contains("\"timestamp\"")) { |
| 136 | + assertTrue("timestamp should be unquoted when int64_as_string=false, got: " + body, |
| 137 | + body.matches("(?s).*\"timestamp\"\\s*:\\s*\\d+.*")); |
| 138 | + } |
| 139 | + } catch (UnsupportedEncodingException e) { |
| 140 | + fail(e.getMessage()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testPostIgnoresUrlQuery() { |
| 146 | + // Mirror visible semantics: on POST, URL query parameters are NOT read; |
| 147 | + // only the JSON body controls the flag. |
| 148 | + MockHttpServletRequest request = createPostJsonRequest("{}"); |
| 149 | + request.addParameter("int64_as_string", "true"); // must be ignored |
| 150 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 151 | + getBlockServlet.doPost(request, response); |
| 152 | + try { |
| 153 | + String body = response.getContentAsString(); |
| 154 | + if (body.contains("\"timestamp\"")) { |
| 155 | + assertFalse("POST URL query must not leak into body-parsed flag, got: " + body, |
| 156 | + body.matches("(?s).*\"timestamp\"\\s*:\\s*\"\\d+\".*")); |
| 157 | + } |
| 158 | + } catch (UnsupportedEncodingException e) { |
| 159 | + fail(e.getMessage()); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments