private UrlCredentials getCredentials(Map<String, String> config) { String configValue = config.get(CONFIG_CONNECTION_AUTHTYPE); Types authType = Types.parseType(configValue); switch (authType) { case BASIC:
public static Types parseType(String name) { if (name == null) { return Types.NONE; } try { return Types.valueOf(name.toUpperCase().trim()); } catch (Exception e) { return Types.NONE; } } }
@Test public void testBasicAuthOK() throws Exception { Map<String, String> props = new HashMap<>(); props.put(AgentConstants.CONFIG_CONNECTION_AUTHTYPE, Types.BASIC.name()); props.put(AgentConstants.CONFIG_CONNECTION_USERNAME, USERNAME); props.put(AgentConstants.CONFIG_CONNECTION_PASSWORD, PASSWORD); ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); configurationHandler.putAll(props); ConnectionHandler connectionHandler = m_agentContext.getHandler(ConnectionHandler.class); HttpURLConnection connection = (HttpURLConnection) connectionHandler.getConnection(m_basicAuthURL); assertEquals(connection.getResponseCode(), HttpServletResponse.SC_OK); } }
@Test public void testBasicAuthFORBIDDEN() throws Exception { Map<String, String> props = new HashMap<>(); props.put(AgentConstants.CONFIG_CONNECTION_AUTHTYPE, Types.NONE.name()); ConfigurationHandler configurationHandler = m_agentContext.getHandler(ConfigurationHandler.class); configurationHandler.putAll(props); ConnectionHandler connectionHandler = m_agentContext.getHandler(ConnectionHandler.class); HttpURLConnection connection = (HttpURLConnection) connectionHandler.getConnection(m_basicAuthURL); assertEquals(connection.getResponseCode(), HttpServletResponse.SC_FORBIDDEN); }