### Question:
ConnectTrial { void trialHeadMethodForInstanceLength() throws IOException { final DownloadConnection connection = OkDownload.with().connectionFactory() .create(task.getUrl()); final DownloadListener listener = OkDownload.with().callbackDispatcher().dispatch(); try { connection.setRequestMethod(METHOD_HEAD); final Map<String, List<String>> userHeader = task.getHeaderMapFields(); if (userHeader != null) Util.addUserRequestHeaderField(userHeader, connection); listener.connectTrialStart(task, connection.getRequestProperties()); final DownloadConnection.Connected connectedForContentLength = connection.execute(); listener.connectTrialEnd(task, connectedForContentLength.getResponseCode(), connectedForContentLength.getResponseHeaderFields()); this.instanceLength = Util.parseContentLength( connectedForContentLength.getResponseHeaderField(CONTENT_LENGTH)); } finally { connection.release(); } } ConnectTrial(@NonNull DownloadTask task, @NonNull BreakpointInfo info); void executeTrial(); long getInstanceLength(); boolean isAcceptRange(); boolean isChunked(); @Nullable String getResponseEtag(); @Nullable String getResponseFilename(); int getResponseCode(); boolean isEtagOverdue();  }### Answer:
@Test public void trialHeadMethodForInstanceLength() throws IOException { final DownloadConnection.Factory factory = OkDownload.with().connectionFactory(); final DownloadConnection connection = mock(DownloadConnection.class); when(factory.create(anyString())).thenReturn(connection); final DownloadConnection.Connected connected = mock(DownloadConnection.Connected.class); when(connection.execute()).thenReturn(connected); when(connected.getResponseHeaderField(CONTENT_LENGTH)).thenReturn("10"); final CallbackDispatcher callbackDispatcher = OkDownload.with().callbackDispatcher(); final DownloadListener listener = mock(DownloadListener.class); when(callbackDispatcher.dispatch()).thenReturn(listener); connectTrial.trialHeadMethodForInstanceLength(); verify(connection).setRequestMethod(eq(METHOD_HEAD)); verify(listener).connectTrialStart(eq(task), nullable(Map.class)); verify(listener).connectTrialEnd(eq(task), anyInt(), nullable(Map.class)); assertThat(connectTrial.getInstanceLength()).isEqualTo(10L); }
@Test public void trialHeadMethodForInstanceLength_userHeader() throws Exception { Map<String, List<String>> userHeader = new HashMap<>(); List<String> values = new ArrayList<>(); values.add("header1-value1"); values.add("header1-value2"); userHeader.put("header1", values); values = new ArrayList<>(); values.add("header2-value"); userHeader.put("header2", values); when(task.getHeaderMapFields()).thenReturn(userHeader); connectTrial.trialHeadMethodForInstanceLength(); verify(connection).addHeader(eq("header1"), eq("header1-value1")); verify(connection).addHeader(eq("header1"), eq("header1-value2")); verify(connection).addHeader(eq("header2"), eq("header2-value")); }