1 package com.android.volley.toolbox; 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertNull; 5 import static org.junit.Assert.assertSame; 6 import static org.mockito.Mockito.when; 7 8 import com.android.volley.Header; 9 import com.android.volley.Request; 10 import com.android.volley.mock.TestRequest; 11 import java.io.IOException; 12 import java.io.InputStream; 13 import java.net.SocketTimeoutException; 14 import java.util.ArrayList; 15 import java.util.Collections; 16 import java.util.List; 17 import java.util.Map; 18 import org.apache.http.HttpEntity; 19 import org.apache.http.HttpResponse; 20 import org.apache.http.StatusLine; 21 import org.apache.http.conn.ConnectTimeoutException; 22 import org.apache.http.message.BasicHeader; 23 import org.junit.Before; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.mockito.Mock; 27 import org.mockito.MockitoAnnotations; 28 import org.robolectric.RobolectricTestRunner; 29 30 @RunWith(RobolectricTestRunner.class) 31 public class AdaptedHttpStackTest { 32 private static final Request<?> REQUEST = new TestRequest.Get(); 33 private static final Map<String, String> ADDITIONAL_HEADERS = Collections.emptyMap(); 34 35 @Mock private HttpStack mHttpStack; 36 @Mock private HttpResponse mHttpResponse; 37 @Mock private StatusLine mStatusLine; 38 @Mock private HttpEntity mHttpEntity; 39 @Mock private InputStream mContent; 40 41 private AdaptedHttpStack mAdaptedHttpStack; 42 43 @Before 44 public void setUp() { 45 MockitoAnnotations.initMocks(this); 46 mAdaptedHttpStack = new AdaptedHttpStack(mHttpStack); 47 when(mHttpResponse.getStatusLine()).thenReturn(mStatusLine); 48 } 49 50 @Test(expected = SocketTimeoutException.class) 51 public void requestTimeout() throws Exception { 52 when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)) 53 .thenThrow(new ConnectTimeoutException()); 54 55 mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); 56 } 57 58 @Test 59 public void emptyResponse() throws Exception { 60 when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); 61 when(mStatusLine.getStatusCode()).thenReturn(12345); 62 when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]); 63 64 com.android.volley.toolbox.HttpResponse response = 65 mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); 66 67 assertEquals(12345, response.getStatusCode()); 68 assertEquals(Collections.emptyList(), response.getHeaders()); 69 assertNull(response.getContent()); 70 } 71 72 @Test 73 public void nonEmptyResponse() throws Exception { 74 when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); 75 when(mStatusLine.getStatusCode()).thenReturn(12345); 76 when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]); 77 when(mHttpResponse.getEntity()).thenReturn(mHttpEntity); 78 when(mHttpEntity.getContentLength()).thenReturn((long) Integer.MAX_VALUE); 79 when(mHttpEntity.getContent()).thenReturn(mContent); 80 81 com.android.volley.toolbox.HttpResponse response = 82 mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); 83 84 assertEquals(12345, response.getStatusCode()); 85 assertEquals(Collections.emptyList(), response.getHeaders()); 86 assertEquals(Integer.MAX_VALUE, response.getContentLength()); 87 assertSame(mContent, response.getContent()); 88 } 89 90 @Test(expected = IOException.class) 91 public void responseTooBig() throws Exception { 92 when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); 93 when(mStatusLine.getStatusCode()).thenReturn(12345); 94 when(mHttpResponse.getAllHeaders()).thenReturn(new org.apache.http.Header[0]); 95 when(mHttpResponse.getEntity()).thenReturn(mHttpEntity); 96 when(mHttpEntity.getContentLength()).thenReturn(Integer.MAX_VALUE + 1L); 97 when(mHttpEntity.getContent()).thenReturn(mContent); 98 99 mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); 100 } 101 102 @Test 103 public void responseWithHeaders() throws Exception { 104 when(mHttpStack.performRequest(REQUEST, ADDITIONAL_HEADERS)).thenReturn(mHttpResponse); 105 when(mStatusLine.getStatusCode()).thenReturn(12345); 106 when(mHttpResponse.getAllHeaders()) 107 .thenReturn( 108 new org.apache.http.Header[] { 109 new BasicHeader("header1", "value1_B"), 110 new BasicHeader("header3", "value3"), 111 new BasicHeader("HEADER2", "value2"), 112 new BasicHeader("header1", "value1_A") 113 }); 114 115 com.android.volley.toolbox.HttpResponse response = 116 mAdaptedHttpStack.executeRequest(REQUEST, ADDITIONAL_HEADERS); 117 118 assertEquals(12345, response.getStatusCode()); 119 assertNull(response.getContent()); 120 121 List<Header> expectedHeaders = new ArrayList<>(); 122 expectedHeaders.add(new Header("header1", "value1_B")); 123 expectedHeaders.add(new Header("header3", "value3")); 124 expectedHeaders.add(new Header("HEADER2", "value2")); 125 expectedHeaders.add(new Header("header1", "value1_A")); 126 assertEquals(expectedHeaders, response.getHeaders()); 127 } 128 } 129