### Question:
RedirectUtil { @NonNull public static String getRedirectedUrl(DownloadConnection.Connected connected, int responseCode) throws IOException { String url = connected.getResponseHeaderField("Location"); if (url == null) { throw new ProtocolException( "Response code is " + responseCode + " but can't find Location field"); } return url; }  static boolean isRedirect(int code); @NonNull static String getRedirectedUrl(DownloadConnection.Connected connected, int responseCode); static final int MAX_REDIRECT_TIMES; }### Answer:
@Test public void getRedirectedUrl_thrownProtocolException() throws IOException { thrown.expect(ProtocolException.class); thrown.expectMessage("Response code is 302 but can't find Location field"); final DownloadConnection.Connected connected = mock(DownloadConnection.Connected.class); when(connected.getResponseHeaderField("Location")).thenReturn(null); RedirectUtil.getRedirectedUrl(connected, 302); }
@Test public void getRedirectUrl() throws IOException { final String redirectUrl = "http: final DownloadConnection.Connected connected = mock(DownloadConnection.Connected.class); when(connected.getResponseHeaderField("Location")).thenReturn(redirectUrl); assertEquals(RedirectUtil.getRedirectedUrl(connected, 302), redirectUrl); }