/** * Applies basic authentication to the given connection, if it is a {@link HttpURLConnection}. * * @param conn * the connection to apply basic authentication to; * @param values * the credentials to apply. */ private void applyBasicAuthentication(URLConnection conn, Object[] values) { if (conn instanceof HttpURLConnection) { conn.setRequestProperty(HTTP_HEADER_AUTHORIZATION, getBasicAuthCredentials(values)); } }
/** * 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!"; }