@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 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)); }
@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 } }
when(response.headers()).thenReturn(vertxHeaders); OutputStream outputStream = writer.writeResponseStatusAndHeaders(12, cr);