@Override public ContainerResponseWriter get( HttpServerRequest vertxRequest, ContainerRequest jerseyRequest) { return new VertxResponseWriter(vertxRequest, vertx, responseProcessors, postResponseProcessors); }
/** * {@inheritDoc} */ @Override public void setSuspendTimeout(long timeOut, TimeUnit timeUnit) throws IllegalStateException { if (timeoutHandler == null) { throw new IllegalStateException("The timeoutHandler is null"); } suspend(timeOut, timeUnit, timeoutHandler); }
/** * {@inheritDoc} */ @Override public void commit() { // End the vertx response if not a write stream if (!isWriteStream) { end(); } }
@Test public void testSetSuspendTimeout() throws Exception { ContainerResponseWriter.TimeoutHandler timeoutHandler = mock(ContainerResponseWriter.TimeoutHandler.class); try { writer.setSuspendTimeout(10, TimeUnit.SECONDS); fail(); } catch (IllegalStateException e) { // Expected } writer.suspend(0, TimeUnit.SECONDS, timeoutHandler); writer.setSuspendTimeout(5, TimeUnit.MINUTES); verify(vertx, times(1)).setTimer(anyLong(), any(Handler.class)); }
@Test public void testWriteResponseStatusAndHeaders() throws Exception { ContainerResponse cr = mock(ContainerResponse.class); MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); when(cr.getStatusInfo()).thenReturn(mock(Response.StatusType.class)); when(cr.getStringHeaders()).thenReturn(headers); VertxResponseProcessor processor1 = mock(VertxResponseProcessor.class); VertxResponseProcessor processor2 = mock(VertxResponseProcessor.class); responseProcessors.add(processor1); responseProcessors.add(processor2); headers.add("x-test", "custom header"); OutputStream outputStream = writer.writeResponseStatusAndHeaders(15, cr); assertNotNull(outputStream); verify(response, times(1)).setStatusCode(anyInt()); verify(response, times(1)).setStatusMessage(anyString()); verify(response, times(1)).putHeader(anyString(), anyListOf(String.class)); verify(processor1).process(eq(response), eq(cr)); verify(processor2).process(eq(response), eq(cr)); writer.writeResponseStatusAndHeaders(-1, cr); verify(response, times(2)).putHeader(anyString(), anyListOf(String.class)); }
@Test public void testCommit() throws Exception { VertxPostResponseProcessor processor1 = mock(VertxPostResponseProcessor.class); VertxPostResponseProcessor processor2 = mock(VertxPostResponseProcessor.class); postResponseProcessors.add(processor1); postResponseProcessors.add(processor2); writer.commit(); verify(response).end(); verify(processor1).process(eq(response), any(ContainerResponse.class)); verify(processor2).process(eq(response), any(ContainerResponse.class)); }
@Test public void testFailure() throws Exception { Throwable error = mock(Throwable.class); writer.failure(error); verify(response, times(1)).setStatusCode(eq(500)); verify(response, times(1)).setStatusMessage(eq("Internal Server Error")); verify(response, times(1)).end(); }
@Test public void testEnableResponseBuffering() throws Exception { boolean result; result = writer.enableResponseBuffering(); assertFalse(result); } }
@Test public void testWriteResponse_Throw() throws Exception { ContainerResponse cr = mock(ContainerResponse.class); MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); when(cr.getStatusInfo()).thenReturn(mock(Response.StatusType.class)); when(cr.getStringHeaders()).thenReturn(headers); VertxResponseProcessor processor1 = mock(VertxResponseProcessor.class); VertxResponseProcessor processor2 = mock(VertxResponseProcessor.class); responseProcessors.add(processor1); responseProcessors.add(processor2); headers.add("x-test", "custom header"); OutputStream outputStream = writer.writeResponseStatusAndHeaders(15, cr); assertNotNull(outputStream); verify(response, times(1)).setStatusCode(anyInt()); verify(response, times(1)).setStatusMessage(anyString()); verify(response, times(1)).putHeader(anyString(), anyListOf(String.class)); verify(processor1).process(eq(response), eq(cr)); verify(processor2).process(eq(response), eq(cr)); writer.writeResponseStatusAndHeaders(-1, cr); verify(response, times(2)).putHeader(anyString(), anyListOf(String.class)); }
/** * {@inheritDoc} */ @Override public void setSuspendTimeout(long timeOut, TimeUnit timeUnit) throws IllegalStateException { if (timeoutHandler == null) { throw new IllegalStateException("The timeoutHandler is null"); } suspend(timeOut, timeUnit, timeoutHandler); }
@Override public ContainerResponseWriter get( HttpServerRequest vertxRequest, ContainerRequest jerseyRequest) { return new VertxResponseWriter(vertxRequest, vertx, container, responseProcessors, postResponseProcessors); }
/** * {@inheritDoc} */ @Override public void commit() { // End the vertx response if not a write stream if (!isWriteStream) { end(); } }
@Test public void testWrite_Chunked() throws Exception { ContainerResponse cr = mock(ContainerResponse.class); MultivaluedMap<String, String> headers = new MultivaluedHashMap<>(); when(cr.getStatusInfo()).thenReturn(mock(Response.StatusType.class)); when(cr.getStringHeaders()).thenReturn(headers); when(cr.isChunked()).thenReturn(true); OutputStream outputStream = writer.writeResponseStatusAndHeaders(-1, cr); verify(response, times(1)).setChunked(eq(true)); outputStream.write("Chunked data".getBytes()); verify(response, times(1)).write(any(Buffer.class)); outputStream.flush(); verify(response, times(1)).write(any(Buffer.class)); outputStream.write("Chunked data".getBytes()); verify(response, times(2)).write(any(Buffer.class)); outputStream.write("Chunked data".getBytes()); verify(response, times(3)).write(any(Buffer.class)); outputStream.write("Final chunked data".getBytes()); outputStream.close(); verify(response, times(4)).write(any(Buffer.class)); try { outputStream.write("fail".getBytes()); fail(); } catch (RuntimeException e) { // Expected } }
/** * {@inheritDoc} */ @Override public void setSuspendTimeout(long timeOut, TimeUnit timeUnit) throws IllegalStateException { if (timeoutHandler == null) { throw new IllegalStateException("The timeoutHandler is null"); } suspend(timeOut, timeUnit, timeoutHandler); }
@Override public ContainerResponseWriter get( HttpServerRequest vertxRequest, ContainerRequest jerseyRequest) { return new VertxResponseWriter(vertxRequest, vertx, responseProcessors, postResponseProcessors); }
@Override public void handle(Void event) { end(); } });
when(response.headers()).thenReturn(vertxHeaders); OutputStream outputStream = writer.writeResponseStatusAndHeaders(12, cr);
@Test public void testSuspend() throws Exception { boolean result; ContainerResponseWriter.TimeoutHandler timeoutHandler = mock(ContainerResponseWriter.TimeoutHandler.class); result = writer.suspend(10, TimeUnit.SECONDS, timeoutHandler); assertTrue(result); verify(vertx, times(1)).setTimer(anyLong(), any(Handler.class)); result = writer.suspend(0, TimeUnit.SECONDS, timeoutHandler); assertTrue(result); verify(vertx, times(1)).setTimer(anyLong(), any(Handler.class)); result = writer.suspend(10, TimeUnit.SECONDS, timeoutHandler); assertTrue(result); verify(vertx, times(2)).setTimer(anyLong(), any(Handler.class)); }
@Before public void setUp() { when(request.response()).thenReturn(response); when(vertx.setTimer(anyLong(), any(Handler.class))).thenReturn(timerId); writer = new VertxResponseWriter(request, vertx, responseProcessors, postResponseProcessors); }
/** * {@inheritDoc} */ @Override public void commit() { // End the vertx response if not a write stream if (!isWriteStream) { end(); } }