Skip to content

Commit 801e7ae

Browse files
committed
add testcase of JsonRpcServlet and CachedBodyRequestWrapper
1 parent d65f064 commit 801e7ae

4 files changed

Lines changed: 423 additions & 0 deletions

File tree

framework/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies {
5858
}
5959

6060
testImplementation group: 'org.springframework', name: 'spring-test', version: "${springVersion}"
61+
testImplementation group: 'javax.portlet', name: 'portlet-api', version: '3.0.1'
6162
implementation group: 'org.zeromq', name: 'jeromq', version: '0.5.3'
6263
api project(":chainbase")
6364
api project(":protocol")

framework/src/test/java/org/tron/core/services/filter/BufferedResponseWrapperTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
55
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNull;
67
import static org.junit.Assert.assertTrue;
78

89
import java.io.IOException;
@@ -162,4 +163,102 @@ public void statusNotForwardedBeforeCommit() throws IOException {
162163
w.commitToResponse();
163164
assertEquals(201, mockResp.getStatus());
164165
}
166+
167+
// --- getStatus() ---
168+
169+
@Test
170+
public void getStatus_returnsBufferedValue() {
171+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
172+
w.setStatus(404);
173+
assertEquals(404, w.getStatus());
174+
// actual response must still be untouched
175+
assertEquals(200, mockResp.getStatus());
176+
}
177+
178+
@Test
179+
public void getStatus_defaultIs200() {
180+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
181+
assertEquals(200, w.getStatus());
182+
}
183+
184+
// --- setHeader / addHeader for Content-Length ---
185+
186+
@Test
187+
public void setHeader_contentLength_exceedsLimit_overflow() {
188+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 100);
189+
w.setHeader("Content-Length", "101");
190+
assertTrue(w.isOverflow());
191+
// Content-Length must NOT have been forwarded to the actual response
192+
assertNull(mockResp.getHeader("Content-Length"));
193+
}
194+
195+
@Test
196+
public void setHeader_contentLength_withinLimit_noOverflow() {
197+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 100);
198+
w.setHeader("Content-Length", "100");
199+
assertFalse(w.isOverflow());
200+
}
201+
202+
@Test
203+
public void setHeader_contentLength_caseInsensitive_overflow() {
204+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 50);
205+
w.setHeader("content-length", "51");
206+
assertTrue(w.isOverflow());
207+
}
208+
209+
@Test
210+
public void setHeader_contentLength_malformed_ignored() {
211+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 100);
212+
w.setHeader("Content-Length", "not-a-number");
213+
assertFalse(w.isOverflow());
214+
}
215+
216+
@Test
217+
public void setHeader_nonContentLength_passesThroughToActual() {
218+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
219+
w.setHeader("X-Custom-Header", "hello");
220+
assertEquals("hello", mockResp.getHeader("X-Custom-Header"));
221+
}
222+
223+
@Test
224+
public void addHeader_contentLength_exceedsLimit_overflow() {
225+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 100);
226+
w.addHeader("Content-Length", "200");
227+
assertTrue(w.isOverflow());
228+
assertNull(mockResp.getHeader("Content-Length"));
229+
}
230+
231+
@Test
232+
public void addHeader_contentLength_malformed_ignored() {
233+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 100);
234+
w.addHeader("Content-Length", "bad");
235+
assertFalse(w.isOverflow());
236+
}
237+
238+
@Test
239+
public void addHeader_nonContentLength_passesThroughToActual() {
240+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
241+
w.addHeader("X-Trace-Id", "abc123");
242+
assertEquals("abc123", mockResp.getHeader("X-Trace-Id"));
243+
}
244+
245+
// --- commitToResponse idempotency ---
246+
247+
@Test(expected = IllegalStateException.class)
248+
public void commitToResponse_secondCall_throwsIllegalState() throws IOException {
249+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
250+
w.commitToResponse();
251+
w.commitToResponse();
252+
}
253+
254+
// --- getWriter path ---
255+
256+
@Test
257+
public void writeViaWriter_commitToResponse_flushesBody() throws IOException {
258+
BufferedResponseWrapper w = new BufferedResponseWrapper(mockResp, 0);
259+
w.getWriter().print("hello");
260+
w.getWriter().flush();
261+
w.commitToResponse();
262+
assertEquals("hello", mockResp.getContentAsString());
263+
}
165264
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.tron.core.services.filter;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
9+
import org.junit.Test;
10+
import org.springframework.mock.web.MockHttpServletRequest;
11+
12+
public class CachedBodyRequestWrapperTest {
13+
14+
private static final byte[] BODY = "hello world".getBytes(StandardCharsets.UTF_8);
15+
16+
private static byte[] readFully(javax.servlet.ServletInputStream in) throws IOException {
17+
ByteArrayOutputStream out = new ByteArrayOutputStream();
18+
byte[] buf = new byte[128];
19+
int n;
20+
while ((n = in.read(buf)) != -1) {
21+
out.write(buf, 0, n);
22+
}
23+
return out.toByteArray();
24+
}
25+
26+
// --- getInputStream ---
27+
28+
@Test
29+
public void getInputStream_returnsBodyContent() throws IOException {
30+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
31+
byte[] read = readFully(w.getInputStream());
32+
assertEquals(new String(BODY, StandardCharsets.UTF_8), new String(read, StandardCharsets.UTF_8));
33+
}
34+
35+
@Test
36+
public void getInputStream_calledTwice_bothSucceed() throws IOException {
37+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
38+
w.getInputStream();
39+
// second call of the same accessor is allowed by the servlet spec
40+
w.getInputStream();
41+
}
42+
43+
// --- getReader ---
44+
45+
@Test
46+
public void getReader_returnsBodyContent() throws IOException {
47+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
48+
String line = w.getReader().readLine();
49+
assertEquals("hello world", line);
50+
}
51+
52+
@Test
53+
public void getReader_calledTwice_bothSucceed() throws IOException {
54+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
55+
w.getReader();
56+
w.getReader();
57+
}
58+
59+
// --- mutual exclusion ---
60+
61+
@Test(expected = IllegalStateException.class)
62+
public void getReader_afterGetInputStream_throws() throws IOException {
63+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
64+
w.getInputStream();
65+
w.getReader();
66+
}
67+
68+
@Test(expected = IllegalStateException.class)
69+
public void getInputStream_afterGetReader_throws() throws IOException {
70+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
71+
w.getReader();
72+
w.getInputStream();
73+
}
74+
75+
// --- stream contract ---
76+
77+
@Test
78+
public void getInputStream_isFinished_afterFullRead() throws IOException {
79+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
80+
javax.servlet.ServletInputStream in = w.getInputStream();
81+
while (in.read() != -1) {
82+
// drain
83+
}
84+
assertTrue(in.isFinished());
85+
}
86+
87+
@Test
88+
public void getInputStream_isReady_returnsTrue() {
89+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(), BODY);
90+
assertTrue(w.getInputStream().isReady());
91+
}
92+
93+
@Test
94+
public void getInputStream_emptyBody_isFinishedImmediately() {
95+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(new MockHttpServletRequest(),
96+
new byte[0]);
97+
assertTrue(w.getInputStream().isFinished());
98+
}
99+
100+
@Test
101+
public void getReader_usesRequestCharacterEncoding() throws IOException {
102+
MockHttpServletRequest req = new MockHttpServletRequest();
103+
req.setCharacterEncoding("UTF-8");
104+
byte[] utf8Body = "tron".getBytes(StandardCharsets.UTF_8);
105+
CachedBodyRequestWrapper w = new CachedBodyRequestWrapper(req, utf8Body);
106+
assertEquals("tron", w.getReader().readLine());
107+
}
108+
}

0 commit comments

Comments
 (0)