|
| 1 | +package datadog.trace.instrumentation.tomcat7; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | +import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.contains; |
| 7 | +import static org.mockito.ArgumentMatchers.eq; |
| 8 | +import static org.mockito.Mockito.doThrow; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.never; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | + |
| 14 | +import datadog.appsec.api.blocking.BlockingContentType; |
| 15 | +import datadog.trace.api.gateway.BlockResponseFunction; |
| 16 | +import datadog.trace.api.gateway.Flow; |
| 17 | +import datadog.trace.api.gateway.RequestContext; |
| 18 | +import datadog.trace.api.internal.TraceSegment; |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.IOException; |
| 21 | +import javax.servlet.ServletOutputStream; |
| 22 | +import javax.servlet.WriteListener; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +class GlassFishBlockingHelperTest { |
| 28 | + |
| 29 | + // ------- commitBlocking() ------- |
| 30 | + |
| 31 | + @Test |
| 32 | + void commitBlocking_nullResponse_returnsFalse() { |
| 33 | + assertFalse(GlassFishBlockingHelper.commitBlocking(null, null, rba(403))); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + void commitBlocking_committedResponse_returnsFalse() { |
| 38 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 39 | + when(resp.isCommitted()).thenReturn(true); |
| 40 | + assertFalse(GlassFishBlockingHelper.commitBlocking(null, resp, rba(403))); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void commitBlocking_blockingContentTypeNone_setsStatusWithoutBody() throws IOException { |
| 45 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 46 | + when(resp.isCommitted()).thenReturn(false); |
| 47 | + |
| 48 | + assertTrue( |
| 49 | + GlassFishBlockingHelper.commitBlocking( |
| 50 | + null, resp, new Flow.Action.RequestBlockingAction(403, BlockingContentType.NONE))); |
| 51 | + |
| 52 | + verify(resp).setStatus(403); |
| 53 | + verify(resp).flushBuffer(); |
| 54 | + verify(resp, never()).setHeader(eq("Content-Type"), any()); |
| 55 | + verify(resp, never()).getOutputStream(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void commitBlocking_withJsonAccept_writesJsonBody() throws IOException { |
| 60 | + HttpServletRequest req = mock(HttpServletRequest.class); |
| 61 | + when(req.getHeader("Accept")).thenReturn("application/json"); |
| 62 | + TestServletOutputStream out = new TestServletOutputStream(); |
| 63 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 64 | + when(resp.isCommitted()).thenReturn(false); |
| 65 | + when(resp.getOutputStream()).thenReturn(out); |
| 66 | + |
| 67 | + assertTrue(GlassFishBlockingHelper.commitBlocking(req, resp, rba(403))); |
| 68 | + |
| 69 | + verify(resp).setStatus(403); |
| 70 | + verify(resp).setHeader(eq("Content-Type"), contains("json")); |
| 71 | + verify(resp).setHeader(eq("Content-Length"), any()); |
| 72 | + assertTrue(out.getBytes().length > 0); |
| 73 | + verify(resp).flushBuffer(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void commitBlocking_withHtmlAccept_writesHtmlBody() throws IOException { |
| 78 | + HttpServletRequest req = mock(HttpServletRequest.class); |
| 79 | + when(req.getHeader("Accept")).thenReturn("text/html"); |
| 80 | + TestServletOutputStream out = new TestServletOutputStream(); |
| 81 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 82 | + when(resp.isCommitted()).thenReturn(false); |
| 83 | + when(resp.getOutputStream()).thenReturn(out); |
| 84 | + |
| 85 | + assertTrue(GlassFishBlockingHelper.commitBlocking(req, resp, rba(403))); |
| 86 | + |
| 87 | + verify(resp).setHeader(eq("Content-Type"), contains("html")); |
| 88 | + assertTrue(out.getBytes().length > 0); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void commitBlocking_nullRequest_defaultsToJsonBody() throws IOException { |
| 93 | + TestServletOutputStream out = new TestServletOutputStream(); |
| 94 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 95 | + when(resp.isCommitted()).thenReturn(false); |
| 96 | + when(resp.getOutputStream()).thenReturn(out); |
| 97 | + |
| 98 | + assertTrue(GlassFishBlockingHelper.commitBlocking(null, resp, rba(403))); |
| 99 | + |
| 100 | + verify(resp).setStatus(403); |
| 101 | + assertTrue(out.getBytes().length > 0); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void commitBlocking_ioException_returnsFalse() throws IOException { |
| 106 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 107 | + when(resp.isCommitted()).thenReturn(false); |
| 108 | + when(resp.getOutputStream()).thenThrow(new IOException("stream error")); |
| 109 | + |
| 110 | + assertFalse(GlassFishBlockingHelper.commitBlocking(null, resp, rba(403))); |
| 111 | + } |
| 112 | + |
| 113 | + // ------- tryBlock() ------- |
| 114 | + |
| 115 | + @Test |
| 116 | + void tryBlock_withBrf_commitsViaFunctionAndReturnsTrue() throws Exception { |
| 117 | + TraceSegment segment = mock(TraceSegment.class); |
| 118 | + BlockResponseFunction brf = mock(BlockResponseFunction.class); |
| 119 | + RequestContext reqCtx = mockReqCtx(brf, segment); |
| 120 | + |
| 121 | + Flow.Action.RequestBlockingAction action = rba(403); |
| 122 | + assertTrue(GlassFishBlockingHelper.tryBlock(reqCtx, null, null, action)); |
| 123 | + |
| 124 | + verify(brf).tryCommitBlockingResponse(segment, action); |
| 125 | + verify(segment).effectivelyBlocked(); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void tryBlock_noBrf_fallbackSucceeds_returnsTrue() throws IOException { |
| 130 | + TraceSegment segment = mock(TraceSegment.class); |
| 131 | + RequestContext reqCtx = mockReqCtx(null, segment); |
| 132 | + TestServletOutputStream out = new TestServletOutputStream(); |
| 133 | + HttpServletResponse resp = mock(HttpServletResponse.class); |
| 134 | + when(resp.isCommitted()).thenReturn(false); |
| 135 | + when(resp.getOutputStream()).thenReturn(out); |
| 136 | + |
| 137 | + assertTrue(GlassFishBlockingHelper.tryBlock(reqCtx, null, resp, rba(403))); |
| 138 | + verify(segment).effectivelyBlocked(); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void tryBlock_noBrf_nullFallbackResponse_returnsFalse() { |
| 143 | + RequestContext reqCtx = mock(RequestContext.class); |
| 144 | + when(reqCtx.getBlockResponseFunction()).thenReturn(null); |
| 145 | + |
| 146 | + assertFalse(GlassFishBlockingHelper.tryBlock(reqCtx, null, null, rba(403))); |
| 147 | + verify(reqCtx, never()).getTraceSegment(); |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + void tryBlock_brfThrows_returnsFalse() throws Exception { |
| 152 | + TraceSegment segment = mock(TraceSegment.class); |
| 153 | + BlockResponseFunction brf = mock(BlockResponseFunction.class); |
| 154 | + RequestContext reqCtx = mockReqCtx(brf, segment); |
| 155 | + doThrow(new RuntimeException("commit failed")) |
| 156 | + .when(brf) |
| 157 | + .tryCommitBlockingResponse(any(), any(Flow.Action.RequestBlockingAction.class)); |
| 158 | + |
| 159 | + assertFalse(GlassFishBlockingHelper.tryBlock(reqCtx, null, null, rba(403))); |
| 160 | + verify(segment, never()).effectivelyBlocked(); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void tryBlock_effectivelyBlockedThrows_stillReturnsTrue() throws Exception { |
| 165 | + TraceSegment segment = mock(TraceSegment.class); |
| 166 | + BlockResponseFunction brf = mock(BlockResponseFunction.class); |
| 167 | + RequestContext reqCtx = mockReqCtx(brf, segment); |
| 168 | + doThrow(new RuntimeException("span already finished")).when(segment).effectivelyBlocked(); |
| 169 | + |
| 170 | + assertTrue(GlassFishBlockingHelper.tryBlock(reqCtx, null, null, rba(403))); |
| 171 | + } |
| 172 | + |
| 173 | + // ------- Helpers ------- |
| 174 | + |
| 175 | + private static Flow.Action.RequestBlockingAction rba(int statusCode) { |
| 176 | + return new Flow.Action.RequestBlockingAction(statusCode, BlockingContentType.AUTO); |
| 177 | + } |
| 178 | + |
| 179 | + private static RequestContext mockReqCtx(BlockResponseFunction brf, TraceSegment segment) { |
| 180 | + RequestContext reqCtx = mock(RequestContext.class); |
| 181 | + when(reqCtx.getBlockResponseFunction()).thenReturn(brf); |
| 182 | + when(reqCtx.getTraceSegment()).thenReturn(segment); |
| 183 | + return reqCtx; |
| 184 | + } |
| 185 | + |
| 186 | + private static final class TestServletOutputStream extends ServletOutputStream { |
| 187 | + private final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
| 188 | + |
| 189 | + @Override |
| 190 | + public boolean isReady() { |
| 191 | + return true; |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void setWriteListener(WriteListener listener) {} |
| 196 | + |
| 197 | + @Override |
| 198 | + public void write(int b) throws IOException { |
| 199 | + buffer.write(b); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public void write(byte[] b, int off, int len) throws IOException { |
| 204 | + buffer.write(b, off, len); |
| 205 | + } |
| 206 | + |
| 207 | + public byte[] getBytes() { |
| 208 | + return buffer.toByteArray(); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments