|
14 | 14 | */
|
15 | 15 |
|
16 | 16 | import static org.asynchttpclient.Dsl.get;
|
| 17 | +import static org.mockito.Mockito.*; |
| 18 | +import static org.powermock.api.mockito.PowerMockito.mock; |
17 | 19 | import static org.testng.Assert.*;
|
18 |
| -import io.netty.handler.codec.http.HttpHeaders; |
19 | 20 |
|
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.ByteBuffer; |
| 23 | + |
| 24 | +import org.asynchttpclient.AsyncHandler; |
| 25 | +import org.asynchttpclient.AsyncHandler.State; |
| 26 | +import org.asynchttpclient.netty.NettyResponseHeaders; |
| 27 | +import org.asynchttpclient.HttpResponseBodyPart; |
| 28 | +import org.asynchttpclient.HttpResponseHeaders; |
| 29 | +import org.asynchttpclient.HttpResponseStatus; |
20 | 30 | import org.asynchttpclient.Request;
|
| 31 | +import org.asynchttpclient.uri.Uri; |
| 32 | +import org.powermock.api.mockito.PowerMockito; |
| 33 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 34 | +import org.powermock.modules.testng.PowerMockTestCase; |
21 | 35 | import org.testng.annotations.Test;
|
22 | 36 |
|
| 37 | +import io.netty.handler.codec.http.DefaultHttpHeaders; |
| 38 | +import io.netty.handler.codec.http.HttpHeaders; |
| 39 | + |
23 | 40 | /**
|
24 | 41 | * @author Benjamin Hanzelmann
|
25 | 42 | */
|
26 |
| -public class ResumableAsyncHandlerTest { |
27 |
| - |
28 |
| - @Test(groups = "standalone") |
| 43 | +@PrepareForTest({ HttpResponseStatus.class, State.class }) |
| 44 | +public class ResumableAsyncHandlerTest extends PowerMockTestCase { |
| 45 | + @Test |
29 | 46 | public void testAdjustRange() {
|
30 | 47 | MapResumableProcessor proc = new MapResumableProcessor();
|
31 | 48 |
|
32 |
| - ResumableAsyncHandler h = new ResumableAsyncHandler(proc); |
| 49 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(proc); |
33 | 50 | Request request = get("http://test/url").build();
|
34 |
| - Request newRequest = h.adjustRequestRange(request); |
| 51 | + Request newRequest = handler.adjustRequestRange(request); |
35 | 52 | assertEquals(newRequest.getUri(), request.getUri());
|
36 | 53 | String rangeHeader = newRequest.getHeaders().get(HttpHeaders.Names.RANGE);
|
37 | 54 | assertNull(rangeHeader);
|
38 | 55 |
|
39 | 56 | proc.put("http://test/url", 5000);
|
40 |
| - newRequest = h.adjustRequestRange(request); |
| 57 | + newRequest = handler.adjustRequestRange(request); |
41 | 58 | assertEquals(newRequest.getUri(), request.getUri());
|
42 | 59 | rangeHeader = newRequest.getHeaders().get(HttpHeaders.Names.RANGE);
|
43 | 60 | assertEquals(rangeHeader, "bytes=5000-");
|
44 | 61 | }
|
| 62 | + |
| 63 | + @Test |
| 64 | + public void testOnStatusReceivedOkStatus() throws Exception { |
| 65 | + MapResumableProcessor processor = new MapResumableProcessor(); |
| 66 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(processor); |
| 67 | + HttpResponseStatus responseStatus200 = mock(HttpResponseStatus.class); |
| 68 | + when(responseStatus200.getStatusCode()).thenReturn(200); |
| 69 | + when(responseStatus200.getUri()).thenReturn(mock(Uri.class)); |
| 70 | + State state = handler.onStatusReceived(responseStatus200); |
| 71 | + assertEquals(state, AsyncHandler.State.CONTINUE, "Status should be CONTINUE for a OK response"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testOnStatusReceived206Status() throws Exception { |
| 76 | + MapResumableProcessor processor = new MapResumableProcessor(); |
| 77 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(processor); |
| 78 | + HttpResponseStatus responseStatus206 = mock(HttpResponseStatus.class); |
| 79 | + when(responseStatus206.getStatusCode()).thenReturn(206); |
| 80 | + when(responseStatus206.getUri()).thenReturn(mock(Uri.class)); |
| 81 | + State state = handler.onStatusReceived(responseStatus206); |
| 82 | + assertEquals(state, AsyncHandler.State.CONTINUE, "Status should be CONTINUE for a 'Partial Content' response"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testOnStatusReceivedOkStatusWithDecoratedAsyncHandler() throws Exception { |
| 87 | + HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); |
| 88 | + when(mockResponseStatus.getStatusCode()).thenReturn(200); |
| 89 | + when(mockResponseStatus.getUri()).thenReturn(mock(Uri.class)); |
| 90 | + |
| 91 | + AsyncHandler decoratedAsyncHandler = mock(AsyncHandler.class); |
| 92 | + State mockState = mock(State.class); |
| 93 | + when(decoratedAsyncHandler.onStatusReceived(mockResponseStatus)).thenReturn(mockState); |
| 94 | + |
| 95 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); |
| 96 | + |
| 97 | + State state = handler.onStatusReceived(mockResponseStatus); |
| 98 | + verify(decoratedAsyncHandler).onStatusReceived(mockResponseStatus); |
| 99 | + assertEquals(state, mockState, "State returned should be equal to the one returned from decoratedAsyncHandler"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testOnStatusReceived500Status() throws Exception{ |
| 104 | + MapResumableProcessor processor = new MapResumableProcessor(); |
| 105 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(processor); |
| 106 | + HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); |
| 107 | + when(mockResponseStatus.getStatusCode()).thenReturn(500); |
| 108 | + when(mockResponseStatus.getUri()).thenReturn(mock(Uri.class)); |
| 109 | + State state = handler.onStatusReceived(mockResponseStatus); |
| 110 | + assertEquals(state, AsyncHandler.State.ABORT, "State should be ABORT for Internal Server Error status"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testOnBodyPartReceived() throws Exception { |
| 115 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(); |
| 116 | + HttpResponseBodyPart bodyPart = PowerMockito.mock(HttpResponseBodyPart.class); |
| 117 | + when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); |
| 118 | + ByteBuffer buffer = ByteBuffer.allocate(0); |
| 119 | + when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); |
| 120 | + State state = handler.onBodyPartReceived(bodyPart); |
| 121 | + assertEquals(state, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onBodyPartReceived"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testOnBodyPartReceivedWithResumableListenerThrowsException() throws Exception { |
| 126 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(); |
| 127 | + |
| 128 | + ResumableListener resumableListener = PowerMockito.mock(ResumableListener.class); |
| 129 | + doThrow(new IOException()).when(resumableListener).onBytesReceived(anyObject()); |
| 130 | + handler.setResumableListener(resumableListener); |
| 131 | + |
| 132 | + HttpResponseBodyPart bodyPart = PowerMockito.mock(HttpResponseBodyPart.class); |
| 133 | + State state = handler.onBodyPartReceived(bodyPart); |
| 134 | + assertEquals(state, AsyncHandler.State.ABORT, |
| 135 | + "State should be ABORT if the resumableListener threw an exception in onBodyPartReceived"); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testOnBodyPartReceivedWithDecoratedAsyncHandler() throws Exception { |
| 140 | + HttpResponseBodyPart bodyPart = PowerMockito.mock(HttpResponseBodyPart.class); |
| 141 | + when(bodyPart.getBodyPartBytes()).thenReturn(new byte[0]); |
| 142 | + ByteBuffer buffer = ByteBuffer.allocate(0); |
| 143 | + when(bodyPart.getBodyByteBuffer()).thenReturn(buffer); |
| 144 | + |
| 145 | + AsyncHandler decoratedAsyncHandler = mock(AsyncHandler.class); |
| 146 | + State mockState = mock(State.class); |
| 147 | + when(decoratedAsyncHandler.onBodyPartReceived(bodyPart)).thenReturn(mockState); |
| 148 | + |
| 149 | + // following is needed to set the url variable |
| 150 | + HttpResponseStatus mockResponseStatus = mock(HttpResponseStatus.class); |
| 151 | + when(mockResponseStatus.getStatusCode()).thenReturn(200); |
| 152 | + Uri mockUri = mock(Uri.class); |
| 153 | + when(mockUri.toUrl()).thenReturn("http://non.null"); |
| 154 | + when(mockResponseStatus.getUri()).thenReturn(mockUri); |
| 155 | + |
| 156 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); |
| 157 | + handler.onStatusReceived(mockResponseStatus); |
| 158 | + |
| 159 | + State state = handler.onBodyPartReceived(bodyPart); |
| 160 | + assertEquals(state, mockState, "State should be equal to the state returned from decoratedAsyncHandler"); |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testOnHeadersReceived() throws Exception { |
| 166 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(); |
| 167 | + HttpHeaders responseHeaders = new DefaultHttpHeaders(); |
| 168 | + HttpResponseHeaders headers = new NettyResponseHeaders(responseHeaders); |
| 169 | + State status = handler.onHeadersReceived(headers); |
| 170 | + assertEquals(status, AsyncHandler.State.CONTINUE, "State should be CONTINUE for a successful onHeadersReceived"); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testOnHeadersReceivedWithDecoratedAsyncHandler() throws Exception { |
| 175 | + HttpHeaders responseHeaders = new DefaultHttpHeaders(); |
| 176 | + HttpResponseHeaders headers = new NettyResponseHeaders(responseHeaders); |
| 177 | + |
| 178 | + AsyncHandler decoratedAsyncHandler = mock(AsyncHandler.class); |
| 179 | + State mockState = mock(State.class); |
| 180 | + when(decoratedAsyncHandler.onHeadersReceived(headers)).thenReturn(mockState); |
| 181 | + |
| 182 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(decoratedAsyncHandler); |
| 183 | + State status = handler.onHeadersReceived(headers); |
| 184 | + assertEquals(status, mockState, "State should be equal to the state returned from decoratedAsyncHandler"); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testOnHeadersReceivedContentLengthMinus() throws Exception { |
| 189 | + ResumableAsyncHandler handler = new ResumableAsyncHandler(); |
| 190 | + HttpHeaders responseHeaders = new DefaultHttpHeaders(); |
| 191 | + responseHeaders.add(HttpHeaders.Names.CONTENT_LENGTH, -1); |
| 192 | + HttpResponseHeaders headers = new NettyResponseHeaders(responseHeaders); |
| 193 | + State status = handler.onHeadersReceived(headers); |
| 194 | + assertEquals(status, AsyncHandler.State.ABORT, "State should be ABORT for content length -1"); |
| 195 | + } |
45 | 196 | }
|
0 commit comments