/** * 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()); }
/** * 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); } } }
/** * 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!"; }