Skip to content

Commit ffdbe28

Browse files
author
Mitali Jha
committed
Adding additional unit tests
1 parent 99e4818 commit ffdbe28

13 files changed

+1148
-9
lines changed

client/src/test/java/org/asynchttpclient/RequestBuilderTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,22 @@
1818
import static java.nio.charset.StandardCharsets.UTF_8;
1919
import static org.asynchttpclient.Dsl.get;
2020
import static org.testng.Assert.assertEquals;
21+
import static org.testng.Assert.assertTrue;
2122

2223
import java.io.IOException;
2324
import java.io.UnsupportedEncodingException;
25+
import java.util.Collection;
26+
import java.util.Collections;
27+
import java.util.HashMap;
2428
import java.util.List;
29+
import java.util.Map;
2530
import java.util.concurrent.ExecutionException;
2631

32+
import org.asynchttpclient.cookie.Cookie;
2733
import org.testng.annotations.Test;
2834

35+
import io.netty.handler.codec.http.HttpMethod;
36+
2937
public class RequestBuilderTest {
3038

3139
private final static String SAFE_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890-_~.";
@@ -107,4 +115,40 @@ public void testContentTypeCharsetToBodyEncoding() {
107115
final Request req2 = get("http://localhost").setHeader("Content-Type", "application/json; charset=\"utf-8\"").build();
108116
assertEquals(req2.getCharset(), UTF_8);
109117
}
118+
119+
@Test
120+
public void testDefaultMethod() {
121+
RequestBuilder requestBuilder = new RequestBuilder();
122+
String defaultMethodName = HttpMethod.GET.name();
123+
assertEquals(requestBuilder.method, defaultMethodName, "Default HTTP method should be " + defaultMethodName);
124+
}
125+
126+
@Test
127+
public void testSetHeaders() {
128+
RequestBuilder requestBuilder = new RequestBuilder();
129+
assertTrue(requestBuilder.headers.isEmpty(), "Headers should be empty by default.");
130+
131+
Map<String, Collection<String>> headers = new HashMap<>();
132+
headers.put("Content-Type", Collections.singleton("application/json"));
133+
requestBuilder.setHeaders(headers);
134+
assertTrue(requestBuilder.headers.contains("Content-Type"), "headers set by setHeaders have not been set");
135+
assertEquals(requestBuilder.headers.get("Content-Type"), "application/json", "header value incorrect");
136+
}
137+
138+
public void testAddOrReplaceCookies() {
139+
RequestBuilder requestBuilder = new RequestBuilder();
140+
Cookie cookie = new Cookie("name", "value", false, "google.com", "/", 1000, true, true);
141+
requestBuilder.addOrReplaceCookie(cookie);
142+
assertEquals(requestBuilder.cookies.size(), 1, "cookies size should be 1 after adding one cookie");
143+
assertEquals(requestBuilder.cookies.get(0), cookie, "cookie does not match");
144+
145+
Cookie cookie2 = new Cookie("name", "value2", true, "google2.com", "/path", 1001, false, false);
146+
requestBuilder.addOrReplaceCookie(cookie2);
147+
assertEquals(requestBuilder.cookies.size(), 1, "cookies size should remain 1 as we just replaced a cookie with same name");
148+
assertEquals(requestBuilder.cookies.get(0), cookie2, "cookie does not match");
149+
150+
Cookie cookie3 = new Cookie("name", "value", false, "google.com", "/", 1000, true, true);
151+
requestBuilder.addOrReplaceCookie(cookie3);
152+
assertEquals(requestBuilder.cookies.size(), 2, "cookie size must be 2 after adding 1 more cookie i.e. cookie3");
153+
}
110154
}

client/src/test/java/org/asynchttpclient/handler/resumable/PropertiesBasedResumableProcesserTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,17 @@ public void testSaveLoad() throws Exception {
3737
assertEquals(m.get("http://localhost/test.url"), Long.valueOf(15L));
3838
assertEquals(m.get("http://localhost/test2.url"), Long.valueOf(50L));
3939
}
40+
41+
@Test
42+
public void testRemove() {
43+
PropertiesBasedResumableProcessor propertiesProcessor = new PropertiesBasedResumableProcessor();
44+
propertiesProcessor.put("http://localhost/test.url", 15L);
45+
propertiesProcessor.put("http://localhost/test2.url", 50L);
46+
propertiesProcessor.remove("http://localhost/test.url");
47+
propertiesProcessor.save(null);
48+
propertiesProcessor = new PropertiesBasedResumableProcessor();
49+
Map<String, Long> propertiesMap = propertiesProcessor.load();
50+
assertEquals(propertiesMap.size(), 1);
51+
assertEquals(propertiesMap.get("http://localhost/test2.url"), Long.valueOf(50L));
52+
}
4053
}

client/src/test/java/org/asynchttpclient/handler/resumable/ResumableAsyncHandlerTest.java

Lines changed: 158 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,183 @@
1414
*/
1515

1616
import static org.asynchttpclient.Dsl.get;
17+
import static org.mockito.Mockito.*;
18+
import static org.powermock.api.mockito.PowerMockito.mock;
1719
import static org.testng.Assert.*;
18-
import io.netty.handler.codec.http.HttpHeaders;
1920

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;
2030
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;
2135
import org.testng.annotations.Test;
2236

37+
import io.netty.handler.codec.http.DefaultHttpHeaders;
38+
import io.netty.handler.codec.http.HttpHeaders;
39+
2340
/**
2441
* @author Benjamin Hanzelmann
2542
*/
26-
public class ResumableAsyncHandlerTest {
27-
28-
@Test(groups = "standalone")
43+
@PrepareForTest({ HttpResponseStatus.class, State.class })
44+
public class ResumableAsyncHandlerTest extends PowerMockTestCase {
45+
@Test
2946
public void testAdjustRange() {
3047
MapResumableProcessor proc = new MapResumableProcessor();
3148

32-
ResumableAsyncHandler h = new ResumableAsyncHandler(proc);
49+
ResumableAsyncHandler handler = new ResumableAsyncHandler(proc);
3350
Request request = get("http://test/url").build();
34-
Request newRequest = h.adjustRequestRange(request);
51+
Request newRequest = handler.adjustRequestRange(request);
3552
assertEquals(newRequest.getUri(), request.getUri());
3653
String rangeHeader = newRequest.getHeaders().get(HttpHeaders.Names.RANGE);
3754
assertNull(rangeHeader);
3855

3956
proc.put("http://test/url", 5000);
40-
newRequest = h.adjustRequestRange(request);
57+
newRequest = handler.adjustRequestRange(request);
4158
assertEquals(newRequest.getUri(), request.getUri());
4259
rangeHeader = newRequest.getHeaders().get(HttpHeaders.Names.RANGE);
4360
assertEquals(rangeHeader, "bytes=5000-");
4461
}
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+
}
45196
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.asynchttpclient.handler.resumable;
2+
3+
import static org.mockito.Mockito.*;
4+
5+
import java.io.IOException;
6+
import java.io.RandomAccessFile;
7+
import java.nio.ByteBuffer;
8+
9+
import org.powermock.api.mockito.PowerMockito;
10+
import org.testng.annotations.Test;
11+
12+
public class ResumableRandomAccessFileListenerTest {
13+
14+
@Test
15+
public void testOnBytesReceivedBufferHasArray() throws IOException {
16+
RandomAccessFile file = PowerMockito.mock(RandomAccessFile.class);
17+
ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file);
18+
byte[] array = new byte[] { 1, 2, 23, 33 };
19+
ByteBuffer buf = ByteBuffer.wrap(array);
20+
listener.onBytesReceived(buf);
21+
verify(file).write(array, 0, 4);
22+
}
23+
24+
@Test
25+
public void testOnBytesReceivedBufferHasNoArray() throws IOException {
26+
RandomAccessFile file = PowerMockito.mock(RandomAccessFile.class);
27+
ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file);
28+
29+
byte[] byteArray = new byte[] { 1, 2, 23, 33 };
30+
ByteBuffer buf = ByteBuffer.allocateDirect(4);
31+
buf.put(byteArray);
32+
buf.flip();
33+
listener.onBytesReceived(buf);
34+
verify(file).write(byteArray);
35+
}
36+
37+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.asynchttpclient.netty;
2+
3+
import static org.testng.Assert.*;
4+
5+
import java.util.concurrent.CancellationException;
6+
import java.util.concurrent.ExecutionException;
7+
8+
import static org.mockito.Mockito.*;
9+
10+
import org.asynchttpclient.AsyncHandler;
11+
import org.testng.annotations.Test;
12+
13+
public class NettyResponseFutureTest {
14+
15+
@Test
16+
public void testCancel() {
17+
AsyncHandler asyncHandler = mock(AsyncHandler.class);
18+
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null);
19+
boolean result = nettyResponseFuture.cancel(false);
20+
verify(asyncHandler).onThrowable(anyObject());
21+
assertTrue(result, "Cancel should return true if the Future was cancelled successfully");
22+
assertTrue(nettyResponseFuture.isCancelled(), "isCancelled should return true for a cancelled Future");
23+
}
24+
25+
@Test
26+
public void testCancelOnAlreadyCancelled() {
27+
AsyncHandler asyncHandler = mock(AsyncHandler.class);
28+
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null);
29+
nettyResponseFuture.cancel(false);
30+
boolean result = nettyResponseFuture.cancel(false);
31+
assertFalse(result, "cancel should return false for an already cancelled Future");
32+
assertTrue(nettyResponseFuture.isCancelled(), "isCancelled should return true for a cancelled Future");
33+
}
34+
35+
@Test(expectedExceptions = CancellationException.class)
36+
public void testGetContentThrowsCancellationExceptionIfCancelled() throws InterruptedException, ExecutionException {
37+
AsyncHandler asyncHandler = mock(AsyncHandler.class);
38+
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null);
39+
nettyResponseFuture.cancel(false);
40+
nettyResponseFuture.get();
41+
fail("A CancellationException must have occurred by now as 'cancel' was called before 'get'");
42+
}
43+
44+
@Test
45+
public void testGet() throws Exception {
46+
AsyncHandler asyncHandler = mock(AsyncHandler.class);
47+
Object value = new Object();
48+
when(asyncHandler.onCompleted()).thenReturn(value);
49+
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null);
50+
nettyResponseFuture.done();
51+
Object result = nettyResponseFuture.get();
52+
assertEquals(result, value, "The Future should return the value given by asyncHandler#onCompleted");
53+
}
54+
55+
@Test(expectedExceptions = ExecutionException.class)
56+
public void testGetThrowsExceptionThrownByAsyncHandler() throws Exception {
57+
AsyncHandler asyncHandler = mock(AsyncHandler.class);
58+
when(asyncHandler.onCompleted()).thenThrow(new RuntimeException());
59+
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null);
60+
nettyResponseFuture.done();
61+
nettyResponseFuture.get();
62+
fail("An ExecutionException must have occurred by now as asyncHandler threw an exception in 'onCompleted'");
63+
}
64+
65+
@Test(expectedExceptions = ExecutionException.class)
66+
public void testGetThrowsExceptionOnAbort() throws InterruptedException, ExecutionException {
67+
AsyncHandler asyncHandler = mock(AsyncHandler.class);
68+
NettyResponseFuture nettyResponseFuture = new NettyResponseFuture<>(null, asyncHandler, null, 3, null, null);
69+
nettyResponseFuture.abort(new RuntimeException());
70+
nettyResponseFuture.get();
71+
fail("An ExecutionException must have occurred by now as 'abort' was called before 'get'");
72+
}
73+
}

0 commit comments

Comments
 (0)