Skip to content

Commit 7df020e

Browse files
committed
refactor(http): replace reflection with @VisibleForTesting accessor and fix comment attribution
Add getMaxRequestSize()/setMaxRequestSize() to HttpService so tests use compile-safe accessors instead of Field.setAccessible(true). Correct comments attributing exception swallowing to RateLimiterServlet when it is actually jsonrpc4j that silently absorbs the BadMessageException.
1 parent 64ad6df commit 7df020e

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

framework/src/main/java/org/tron/common/application/HttpService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package org.tron.common.application;
1717

18+
import com.google.common.annotations.VisibleForTesting;
1819
import java.util.concurrent.CompletableFuture;
1920
import lombok.extern.slf4j.Slf4j;
2021
import org.eclipse.jetty.server.ConnectionLimit;
@@ -32,6 +33,16 @@ public abstract class HttpService extends AbstractService {
3233

3334
protected long maxRequestSize = 4 * 1024 * 1024; // 4MB
3435

36+
@VisibleForTesting
37+
public long getMaxRequestSize() {
38+
return this.maxRequestSize;
39+
}
40+
41+
@VisibleForTesting
42+
public void setMaxRequestSize(long maxRequestSize) {
43+
this.maxRequestSize = maxRequestSize;
44+
}
45+
3546
@Override
3647
public void innerStart() throws Exception {
3748
if (this.apiServer != null) {

framework/src/test/java/org/tron/core/jsonrpc/JsonrpcServiceTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import com.google.protobuf.ByteString;
1010
import io.prometheus.client.CollectorRegistry;
1111
import java.io.ByteArrayInputStream;
12-
import java.lang.reflect.Field;
12+
1313
import java.util.ArrayList;
1414
import java.util.Collections;
1515
import java.util.List;
@@ -1109,16 +1109,14 @@ public void testWeb3ClientVersion() {
11091109
*
11101110
* Covers: normal request no regression, Content-Length oversized 413,
11111111
* and chunked oversized handled gracefully (body truncated, 200 + empty body
1112-
* because RateLimiterServlet absorbs the BadMessageException).
1112+
* because jsonrpc4j absorbs the BadMessageException).
11131113
*/
11141114
@Test
11151115
public void testJsonRpcSizeLimitIntegration() {
11161116
long testLimit = 1024;
11171117
try {
1118-
Field field = HttpService.class.getDeclaredField("maxRequestSize");
1119-
field.setAccessible(true);
1120-
long originalLimit = field.getLong(fullNodeJsonRpcHttpService);
1121-
field.setLong(fullNodeJsonRpcHttpService, testLimit);
1118+
long originalLimit = fullNodeJsonRpcHttpService.getMaxRequestSize();
1119+
fullNodeJsonRpcHttpService.setMaxRequestSize(testLimit);
11221120

11231121
fullNodeJsonRpcHttpService.start();
11241122
String url = "http://127.0.0.1:"
@@ -1151,7 +1149,7 @@ public void testJsonRpcSizeLimitIntegration() {
11511149
resp.close();
11521150

11531151
// Chunked oversized → BadMessageException thrown during body read,
1154-
// absorbed by RateLimiterServlet catch(Exception) → 200 with empty body.
1152+
// absorbed by jsonrpc4j catch(Exception) → 200 with empty body.
11551153
// Body read IS truncated at the limit — OOM protection effective.
11561154
byte[] chunkedData = new String(new char[(int) testLimit * 2])
11571155
.replace('\0', 'x').getBytes("UTF-8");
@@ -1162,10 +1160,10 @@ public void testJsonRpcSizeLimitIntegration() {
11621160
Assert.assertEquals(200, resp.getStatusLine().getStatusCode());
11631161
body = EntityUtils.toString(resp.getEntity());
11641162
Assert.assertTrue("Chunked oversized should return empty body"
1165-
+ " (RateLimiterServlet absorbs BadMessageException)", body.isEmpty());
1163+
+ " (jsonrpc4j absorbs BadMessageException)", body.isEmpty());
11661164
resp.close();
11671165
} finally {
1168-
field.setLong(fullNodeJsonRpcHttpService, originalLimit);
1166+
fullNodeJsonRpcHttpService.setMaxRequestSize(originalLimit);
11691167
}
11701168
} catch (Exception e) {
11711169
Assert.fail(e.getMessage());

0 commit comments

Comments
 (0)