DefaultConnection implements Connection { @Override @Nullable public URL getLocation() { assertConnectionIsOpen(); String location = conn.getHeaderField(LOCATION_HEADER); if (location == null) { return null; } LOG.debug("Location: {}", location); return resolveRelative(location); } DefaultConnection(HttpConnector httpConnector); @Override void resetNonce(Session session); @Override int sendRequest(URL url, Session session, @Nullable ZonedDateTime ifModifiedSince); @Override int sendCertificateRequest(URL url, Login login); @Override int sendSignedPostAsGetRequest(URL url, Login login); @Override int sendSignedRequest(URL url, JSONBuilder claims, Login login); @Override int sendSignedRequest(URL url, JSONBuilder claims, Session session, KeyPair keypair); @Override JSON readJsonResponse(); @Override List<X509Certificate> readCertificates(); @Override void handleRetryAfter(String message); @Override @Nullable String getNonce(); @Override @Nullable URL getLocation(); @Override Optional<ZonedDateTime> getLastModified(); @Override Optional<ZonedDateTime> getExpiration(); @Override Collection<URL> getLinks(String relation); @Override void close();  }
@Test public void testGetAbsoluteLocation() throws Exception { when(mockUrlConnection.getHeaderField("Location")).thenReturn("https: when(mockUrlConnection.getURL()).thenReturn(new URL("https: try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; URL location = conn.getLocation(); assertThat(location, is(new URL("https: } verify(mockUrlConnection).getHeaderField("Location"); verify(mockUrlConnection).getURL(); verifyNoMoreInteractions(mockUrlConnection); }
@Test public void testGetRelativeLocation() throws Exception { when(mockUrlConnection.getHeaderField("Location")).thenReturn("/otherlocation"); when(mockUrlConnection.getURL()).thenReturn(new URL("https: try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; URL location = conn.getLocation(); assertThat(location, is(new URL("https: } verify(mockUrlConnection).getHeaderField("Location"); verify(mockUrlConnection).getURL(); verifyNoMoreInteractions(mockUrlConnection); }
@Test public void testNoLocation() { try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; URL location = conn.getLocation(); assertThat(location, is(nullValue())); } verify(mockUrlConnection).getHeaderField("Location"); verifyNoMoreInteractions(mockUrlConnection); }