DefaultHttpClient implements HttpClient, DefaultHttpClientCallTask.Tracker { @Override public ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback) { final DefaultHttpClientCallTask task = new DefaultHttpClientCallTask(url, method, headers, callTemplate, serviceCallback, this, mCompressionEnabled); try { task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } catch (final RejectedExecutionException e) { HandlerUtils.runOnUiThread(new Runnable() { @Override public void run() { serviceCallback.onCallFailed(e); } }); } return new ServiceCall() { @Override public void cancel() { task.cancel(true); } }; } DefaultHttpClient();  DefaultHttpClient(boolean compressionEnabled); @Override ServiceCall callAsync(String url, String method, Map<String, String> headers, CallTemplate callTemplate, final ServiceCallback serviceCallback); @Override synchronized void onStart(DefaultHttpClientCallTask task); @Override synchronized void onFinish(DefaultHttpClientCallTask task); @Override synchronized void close(); @Override void reopen(); static final String METHOD_GET; static final String METHOD_POST; static final String METHOD_DELETE; static final String CONTENT_TYPE_KEY; }
@Test public void get100() throws Exception { String urlString = "https: HttpsURLConnection urlConnection = mockConnection(urlString); when(urlConnection.getResponseCode()).thenReturn(100); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); when(urlConnection.getOutputStream()).thenReturn(buffer); when(urlConnection.getErrorStream()).thenReturn(new ByteArrayInputStream("Continue".getBytes())); DefaultHttpClient httpClient = new DefaultHttpClient(); Map<String, String> headers = new HashMap<>(); ServiceCallback serviceCallback = mock(ServiceCallback.class); mockCall(); httpClient.callAsync(urlString, METHOD_POST, headers, null, serviceCallback); verify(serviceCallback).onCallFailed(new HttpException(new HttpResponse(100, "Continue"))); verifyNoMoreInteractions(serviceCallback); verify(urlConnection).disconnect(); }
@Test public void error503() throws Exception { String urlString = "https: HttpsURLConnection urlConnection = mockConnection(urlString); when(urlConnection.getResponseCode()).thenReturn(503); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); when(urlConnection.getOutputStream()).thenReturn(buffer); when(urlConnection.getErrorStream()).thenReturn(new ByteArrayInputStream("Busy".getBytes())); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); when(callTemplate.buildRequestBody()).thenReturn("mockPayload"); DefaultHttpClient httpClient = new DefaultHttpClient(); String appSecret = UUID.randomUUID().toString(); UUID installId = UUID.randomUUID(); Map<String, String> headers = new HashMap<>(); headers.put("App-Secret", appSecret); headers.put("Install-ID", installId.toString()); ServiceCallback serviceCallback = mock(ServiceCallback.class); mockCall(); httpClient.callAsync(urlString, METHOD_POST, headers, callTemplate, serviceCallback); verify(serviceCallback).onCallFailed(new HttpException(new HttpResponse(503, "Busy"))); verifyNoMoreInteractions(serviceCallback); verify(urlConnection).disconnect(); verifyStatic(); TrafficStats.setThreadStatsTag(anyInt()); verifyStatic(); TrafficStats.clearThreadStatsTag(); }
@Test public void cancel() throws Exception { String urlString = "https: DefaultHttpClientCallTask mockCall = mock(DefaultHttpClientCallTask.class); whenNew(DefaultHttpClientCallTask.class).withAnyArguments().thenReturn(mockCall); DefaultHttpClient httpClient = new DefaultHttpClient(); ServiceCallback serviceCallback = mock(ServiceCallback.class); ServiceCall call = httpClient.callAsync(urlString, "", new HashMap<String, String>(), mock(HttpClient.CallTemplate.class), serviceCallback); call.cancel(); verify(mockCall).cancel(true); }
@Test public void failedConnection() throws Exception { String urlString = "https: URL url = mock(URL.class); whenNew(URL.class).withAnyArguments().thenReturn(url); when(url.getProtocol()).thenReturn("https"); IOException exception = new IOException("mock"); when(url.openConnection()).thenThrow(exception); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); ServiceCallback serviceCallback = mock(ServiceCallback.class); DefaultHttpClient httpClient = new DefaultHttpClient(); mockCall(); httpClient.callAsync(urlString, "", new HashMap<String, String>(), callTemplate, serviceCallback); verify(serviceCallback).onCallFailed(exception); verifyZeroInteractions(callTemplate); verifyZeroInteractions(serviceCallback); }
@Test public void failedWithError() throws Exception { String urlString = "https: URL url = mock(URL.class); whenNew(URL.class).withAnyArguments().thenReturn(url); when(url.getProtocol()).thenReturn("https"); when(url.openConnection()).thenThrow(new Error()); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); ServiceCallback serviceCallback = mock(ServiceCallback.class); DefaultHttpClient httpClient = new DefaultHttpClient(); mockCall(); try { httpClient.callAsync(urlString, "", new HashMap<String, String>(), callTemplate, serviceCallback); fail(); } catch (Error ignored) { } verifyZeroInteractions(serviceCallback); verifyStatic(); TrafficStats.setThreadStatsTag(anyInt()); verifyStatic(); TrafficStats.clearThreadStatsTag(); }
@Test public void failedSerialization() throws Exception { String urlString = "https: HttpsURLConnection urlConnection = mockConnection(urlString); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); JSONException exception = new JSONException("mock"); when(callTemplate.buildRequestBody()).thenThrow(exception); DefaultHttpClient httpClient = new DefaultHttpClient(); ServiceCallback serviceCallback = mock(ServiceCallback.class); mockCall(); httpClient.callAsync(urlString, METHOD_POST, new HashMap<String, String>(), callTemplate, serviceCallback); verify(serviceCallback).onCallFailed(exception); verifyNoMoreInteractions(serviceCallback); verify(urlConnection).disconnect(); verifyStatic(); TrafficStats.setThreadStatsTag(anyInt()); verifyStatic(); TrafficStats.clearThreadStatsTag(); }
@Test @PrepareForTest(HandlerUtils.class) public void rejectedAsyncTask() throws Exception { final Semaphore semaphore = new Semaphore(0); mockStatic(HandlerUtils.class); doAnswer(new Answer<Object>() { @Override public Object answer(final InvocationOnMock invocation) { new Thread("rejectedAsyncTask.handler") { @Override public void run() { ((Runnable) invocation.getArguments()[0]).run(); semaphore.release(); } }.start(); return null; } }).when(HandlerUtils.class); HandlerUtils.runOnUiThread(any(Runnable.class)); DefaultHttpClientCallTask call = mock(DefaultHttpClientCallTask.class); whenNew(DefaultHttpClientCallTask.class).withAnyArguments().thenReturn(call); RejectedExecutionException exception = new RejectedExecutionException(); when(call.executeOnExecutor(any(Executor.class))).thenThrow(exception); DefaultHttpClient httpClient = new DefaultHttpClient(); ServiceCallback serviceCallback = mock(ServiceCallback.class); assertNotNull(httpClient.callAsync("", "", new HashMap<String, String>(), mock(HttpClient.CallTemplate.class), serviceCallback)); semaphore.acquireUninterruptibly(); verify(serviceCallback).onCallFailed(exception); verify(serviceCallback, never()).onCallSucceeded(any(HttpResponse.class)); }
@Test public void failedToConnectWithHttpUrl() throws Exception { String urlString = "http: URL url = mock(URL.class); whenNew(URL.class).withAnyArguments().thenReturn(url); when(url.getProtocol()).thenReturn("http"); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); ServiceCallback serviceCallback = mock(ServiceCallback.class); DefaultHttpClient httpClient = new DefaultHttpClient(); mockCall(); httpClient.callAsync(urlString, "", new HashMap<String, String>(), callTemplate, serviceCallback); verify(serviceCallback).onCallFailed(any(IOException.class)); verifyZeroInteractions(callTemplate); verifyZeroInteractions(serviceCallback); }
@Test public void failedToConnectWithInvalidUrl() throws Exception { String urlString = "bad url"; URL url = mock(URL.class); whenNew(URL.class).withAnyArguments().thenReturn(url); when(url.getProtocol()).thenReturn(null); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); ServiceCallback serviceCallback = mock(ServiceCallback.class); DefaultHttpClient httpClient = new DefaultHttpClient(); mockCall(); httpClient.callAsync(urlString, "", new HashMap<String, String>(), callTemplate, serviceCallback); verify(serviceCallback).onCallFailed(any(IOException.class)); verifyZeroInteractions(callTemplate); verifyZeroInteractions(serviceCallback); }
@Test public void failedToConnectWithHttpConnect() throws Exception { String urlString = "https: URL url = mock(URL.class); whenNew(URL.class).withAnyArguments().thenReturn(url); when(url.getProtocol()).thenReturn("https"); HttpURLConnection urlConnection = mock(HttpURLConnection.class); when(url.openConnection()).thenReturn(urlConnection); HttpClient.CallTemplate callTemplate = mock(HttpClient.CallTemplate.class); ServiceCallback serviceCallback = mock(ServiceCallback.class); DefaultHttpClient httpClient = new DefaultHttpClient(); mockCall(); httpClient.callAsync(urlString, "", new HashMap<String, String>(), callTemplate, serviceCallback); verify(serviceCallback).onCallFailed(any(IOException.class)); verifyZeroInteractions(callTemplate); verifyZeroInteractions(serviceCallback); }