Search in sources :

Example 6 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldGetHttpClientContext.

/**
 * The REST_GET function should set the proper credentials in the HttpClientContext.
 * @throws Exception
 */
@Test
public void restGetShouldGetHttpClientContext() throws Exception {
    HttpHost target = new HttpHost("localhost", 8080);
    HttpHost proxy = new HttpHost("localhost", 3128);
    {
        RestConfig restConfig = new RestConfig();
        HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.empty());
        assertNull(actual.getCredentialsProvider());
    }
    {
        RestConfig restConfig = new RestConfig();
        restConfig.put(BASIC_AUTH_USER, "user");
        restConfig.put(BASIC_AUTH_PASSWORD_PATH, basicAuthPasswordFile.getAbsolutePath());
        HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.empty());
        HttpClientContext expected = HttpClientContext.create();
        CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider();
        expectedCredentialsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), basicAuthPassword));
        expected.setCredentialsProvider(expectedCredentialsProvider);
        assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target)));
        assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
    }
    {
        RestConfig restConfig = new RestConfig();
        restConfig.put(PROXY_BASIC_AUTH_USER, "proxyUser");
        restConfig.put(PROXY_BASIC_AUTH_PASSWORD_PATH, proxyBasicAuthPasswordFile.getAbsolutePath());
        HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.of(proxy));
        HttpClientContext expected = HttpClientContext.create();
        CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider();
        expectedCredentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), proxyAuthPassword));
        expected.setCredentialsProvider(expectedCredentialsProvider);
        assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target)));
        assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
    }
    {
        RestConfig restConfig = new RestConfig();
        restConfig.put(BASIC_AUTH_USER, "user");
        restConfig.put(BASIC_AUTH_PASSWORD_PATH, basicAuthPasswordFile.getAbsolutePath());
        restConfig.put(PROXY_BASIC_AUTH_USER, "proxyUser");
        restConfig.put(PROXY_BASIC_AUTH_PASSWORD_PATH, proxyBasicAuthPasswordFile.getAbsolutePath());
        HttpClientContext actual = RestFunctions.getHttpClientContext(restConfig, target, Optional.of(proxy));
        HttpClientContext expected = HttpClientContext.create();
        CredentialsProvider expectedCredentialsProvider = new BasicCredentialsProvider();
        expectedCredentialsProvider.setCredentials(new AuthScope(target), new UsernamePasswordCredentials(restConfig.getBasicAuthUser(), basicAuthPassword));
        expectedCredentialsProvider.setCredentials(new AuthScope(proxy), new UsernamePasswordCredentials(restConfig.getProxyBasicAuthUser(), proxyAuthPassword));
        expected.setCredentialsProvider(expectedCredentialsProvider);
        assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(target)), actual.getCredentialsProvider().getCredentials(new AuthScope(target)));
        assertEquals(expected.getCredentialsProvider().getCredentials(new AuthScope(proxy)), actual.getCredentialsProvider().getCredentials(new AuthScope(proxy)));
    }
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) AuthScope(org.apache.http.auth.AuthScope) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Test(org.junit.jupiter.api.Test)

Example 7 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldParseResponseOnNullHttpEntity.

@Test
public void restGetShouldParseResponseOnNullHttpEntity() throws Exception {
    RestConfig restConfig = new RestConfig();
    HttpGet httpGet = mock(HttpGet.class);
    // return empty on null httpEntity
    assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, null));
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) HttpGet(org.apache.http.client.methods.HttpGet) Test(org.junit.jupiter.api.Test)

Example 8 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldParseResponseOnEmptyInputStream.

@Test
public void restGetShouldParseResponseOnEmptyInputStream() throws Exception {
    RestConfig restConfig = new RestConfig();
    HttpGet httpGet = mock(HttpGet.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    // return empty on empty input stream
    when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)));
    assertEquals(Optional.empty(), RestFunctions.parseResponse(restConfig, httpGet, httpEntity));
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) Test(org.junit.jupiter.api.Test)

Example 9 with RestConfig

use of org.apache.metron.stellar.dsl.functions.RestConfig in project metron by apache.

the class RestFunctionsTest method restGetShouldParseResponse.

@Test
public void restGetShouldParseResponse() throws Exception {
    RestConfig restConfig = new RestConfig();
    HttpGet httpGet = mock(HttpGet.class);
    HttpEntity httpEntity = mock(HttpEntity.class);
    // return successfully parsed response
    when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("{\"get\":\"success\"}".getBytes(StandardCharsets.UTF_8)));
    Optional<Object> actual = RestFunctions.parseResponse(restConfig, httpGet, httpEntity);
    assertTrue(actual.isPresent());
    assertEquals("success", ((Map<String, Object>) actual.get()).get("get"));
}
Also used : RestConfig(org.apache.metron.stellar.dsl.functions.RestConfig) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) Test(org.junit.jupiter.api.Test)

Aggregations

RestConfig (org.apache.metron.stellar.dsl.functions.RestConfig)9 Test (org.junit.jupiter.api.Test)8 HttpGet (org.apache.http.client.methods.HttpGet)5 HttpEntity (org.apache.http.HttpEntity)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)2 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 HttpHost (org.apache.http.HttpHost)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1