/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#matches(java.net.URL)}. */ @Test public void testMatchesNullURLOk() throws Exception { UrlCredentials creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/")); assert creds.matches(null) == false : "Null URL should never match any credentials!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#getCredentials()}. */ @Test public void testGetCredentialsOk() throws Exception { UrlCredentials creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/")); assertArrayEquals(new Object[0], creds.getCredentials()); creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"), "foo"); assertArrayEquals(new Object[] { "foo" }, creds.getCredentials()); creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"), (Object[]) null ); assertArrayEquals(new Object[0], creds.getCredentials()); creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/"), (Object) null); assertArrayEquals(new Object[] { null }, creds.getCredentials()); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(java.net.URL)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testUrlCredentialsNullURLFail() throws Exception { new UrlCredentials(null); }
/** * Supplies the actual credentials to the given {@link URLConnection}. * * @param conn * the connection to supply the credentials to, cannot be <code>null</code>; * @param urlCreds * the URL credentials to supply, cannot be <code>null</code>. * @throws IOException * in case of I/O problems. */ private void supplyCredentials(URLConnection conn, UrlCredentials urlCreds) throws IOException { final AuthType type = urlCreds.getType(); final Object[] creds = urlCreds.getCredentials(); if (AuthType.BASIC.equals(type)) { applyBasicAuthentication(conn, creds); } else if (AuthType.CLIENT_CERT.equals(type)) { applyClientCertificate(conn, creds); } else if (!AuthType.NONE.equals(type)) { throw new IllegalArgumentException("Unknown authentication type: " + type); } } }
/** * Returns the credentials to access the given URL. * * @param url * the URL to find the credentials for, cannot be <code>null</code>. * @return a {@link UrlCredentials} instance for the given URL, or <code>null</code> if none were found, or if none * were necessary. */ final UrlCredentials getCredentials(URL url) { Collection<UrlCredentials> creds; synchronized (m_credentialMapping) { creds = new ArrayList<>(m_credentialMapping.values()); } for (UrlCredentials c : creds) { if (c.matches(url)) { return c; } } return null; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#getBasicAuthCredentials(UrlCredentials)}. */ @Test() public void testGetBasicAuthCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; String header = new ConnectionFactoryImpl().getBasicAuthCredentials(credentials.getCredentials()); assert header != null : "Expected valid HTTP header to be returned!"; assert header.equals(header.trim()) : "Expected HTTP header not to contain any leading/trailing whitespace!"; assert "Basic Zm9vOmJhcg==".equals(header) : "Expected HTTP header to be constant!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String, java.util.Dictionary)}. */ @Test() public void testUpdatedUpdatesCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, Object> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials1 = connFactory.getCredentials(TEST_URL); assert credentials1 != null : "Expected valid credentials to be found!"; URL newURL = new URL("http://localhost:8181/test/"); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, newURL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials2 = connFactory.getCredentials(TEST_URL); assert credentials2 == null : "Expected no credentials to be found!"; credentials2 = connFactory.getCredentials(newURL); assert credentials2 != null : "Expected valid credentials to be found!"; assert credentials1 != credentials2 && !credentials1.equals(credentials2) : "Expected not the same credentials to be returned!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(org.apache.ace.connectionfactory.impl.UrlCredentials.AuthType, java.net.URL, java.lang.Object[])}. */ @Test public void testUrlCredentialsTypeAndURLOk() throws Exception { new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/")); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#matches(java.net.URL)}. */ @Test public void testMatchesValidURLOk() throws Exception { UrlCredentials creds = new UrlCredentials(AuthType.NONE, new URL("http://localhost:8080/")); assert creds.matches(new URL("http://localhost:8080/obr")) : "Base URL should match given URL!"; assert creds.matches(new URL("http://localhost:8080")) == false : "Base URL shouldn't match given URL!"; assert creds.matches(new URL("http://localhost:8081/")) == false : "Base URL shouldn't match given URL!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(java.net.URL)}. */ @Test public void testUrlCredentialsURLOk() throws Exception { new UrlCredentials(new URL("http://localhost:8080/")); }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.UrlCredentials#UrlCredentials(org.apache.ace.connectionfactory.impl.UrlCredentials.AuthType, java.net.URL, java.lang.Object[])}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testUrlCredentialsNullTypeFail() throws Exception { new UrlCredentials(null, new URL("http://localhost:8080/")); }
return new UrlCredentials(type, baseURL, creds);