@Test public void testOnStatusReceived206Status() throws Exception { MapResumableProcessor processor = new MapResumableProcessor(); ResumableAsyncHandler handler = new ResumableAsyncHandler(processor); HttpResponseStatus responseStatus206 = mock(HttpResponseStatus.class); when(responseStatus206.getStatusCode()).thenReturn(206); when(responseStatus206.getUri()).thenReturn(mock(Uri.class)); State state = handler.onStatusReceived(responseStatus206); assertEquals(state, AsyncHandler.State.CONTINUE, "Status should be CONTINUE for a 'Partial Content' response"); }
@Test public void testOnStatusReceived500Status() throws Exception { MapResumableProcessor processor = new MapResumableProcessor(); ResumableAsyncHandler handler = new ResumableAsyncHandler(processor); HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); when(mockResponseStatus.getStatusCode()).thenReturn(500); when(mockResponseStatus.getUri()).thenReturn(mock(Uri.class)); State state = handler.onStatusReceived(mockResponseStatus); assertEquals(state, AsyncHandler.State.ABORT, "State should be ABORT for Internal Server Error status"); }
@Test public void testOnStatusReceivedOkStatus() throws Exception { MapResumableProcessor processor = new MapResumableProcessor(); ResumableAsyncHandler handler = new ResumableAsyncHandler(processor); HttpResponseStatus responseStatus200 = mock(HttpResponseStatus.class); when(responseStatus200.getStatusCode()).thenReturn(200); when(responseStatus200.getUri()).thenReturn(mock(Uri.class)); State state = handler.onStatusReceived(responseStatus200); assertEquals(state, AsyncHandler.State.CONTINUE, "Status should be CONTINUE for a OK response"); }
@Test public void testOnStatusReceivedOkStatusWithDecoratedAsyncHandler() throws Exception { HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); when(mockResponseStatus.getStatusCode()).thenReturn(200); when(mockResponseStatus.getUri()).thenReturn(mock(Uri.class)); @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class); when(decoratedAsyncHandler.onStatusReceived(mockResponseStatus)).thenReturn(State.CONTINUE); ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); State state = handler.onStatusReceived(mockResponseStatus); verify(decoratedAsyncHandler).onStatusReceived(mockResponseStatus); assertEquals(state, State.CONTINUE, "State returned should be equal to the one returned from decoratedAsyncHandler"); }
@Test public void testOnBodyPartReceivedWithDecoratedAsyncHandler() throws Exception { HttpResponseBodyPart bodyPart = mock(HttpResponseBodyPart.class); when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); ByteBuffer buffer = ByteBuffer.allocate(0); when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); @SuppressWarnings("unchecked") AsyncHandler<Response> decoratedAsyncHandler = mock(AsyncHandler.class); when(decoratedAsyncHandler.onBodyPartReceived(bodyPart)).thenReturn(State.CONTINUE); // following is needed to set the url variable HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); when(mockResponseStatus.getStatusCode()).thenReturn(200); Uri uri = Uri.create("http://non.null"); when(mockResponseStatus.getUri()).thenReturn(uri); ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); handler.onStatusReceived(mockResponseStatus); State state = handler.onBodyPartReceived(bodyPart); assertEquals(state, State.CONTINUE, "State should be equal to the state returned from decoratedAsyncHandler"); }