|
3 | 3 | import static org.junit.Assert.assertArrayEquals; |
4 | 4 | import static org.junit.Assert.assertEquals; |
5 | 5 | import static org.junit.Assert.assertFalse; |
| 6 | +import static org.junit.Assert.assertNull; |
6 | 7 | import static org.junit.Assert.assertTrue; |
7 | 8 |
|
8 | 9 | import java.io.IOException; |
@@ -162,4 +163,102 @@ public void statusNotForwardedBeforeCommit() throws IOException { |
162 | 163 | w.commitToResponse(); |
163 | 164 | assertEquals(201, mockResp.getStatus()); |
164 | 165 | } |
| 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 | + } |
165 | 264 | } |
0 commit comments