Search in sources :

Example 6 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class ServiceSocksProxyTest method testTimeout.

@Test(expected = IOException.class)
public void testTimeout() throws Exception {
    // Connect to a service that is not discoverable
    URL url = new URL("http://not-exist/ping");
    HttpRequests.execute(io.cdap.common.http.HttpRequest.get(url).build(), new HttpRequestConfig(500, 10000));
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) URL(java.net.URL) Test(org.junit.Test)

Example 7 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class ServiceSocksProxyTest method testDelayRegister.

@Test
public void testDelayRegister() throws Exception {
    URL url = new URL("http://test-service-2/ping");
    // Delay the service registration by 2 seconds.
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    ScheduledFuture<Cancellable> future = scheduler.schedule(() -> discoveryService.register(ResolvingDiscoverable.of(new Discoverable("test-service-2", httpService.getBindAddress()))), 2, TimeUnit.SECONDS);
    try {
        HttpResponse response = HttpRequests.execute(io.cdap.common.http.HttpRequest.get(url).build(), new HttpRequestConfig(5000, 5000));
        Assert.assertEquals(200, response.getResponseCode());
    } finally {
        future.get(5, TimeUnit.SECONDS).cancel();
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ResolvingDiscoverable(io.cdap.cdap.common.discovery.ResolvingDiscoverable) Discoverable(org.apache.twill.discovery.Discoverable) Cancellable(org.apache.twill.common.Cancellable) HttpResponse(io.cdap.common.http.HttpResponse) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) URL(java.net.URL) Test(org.junit.Test)

Example 8 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class RemoteClientAuthenticatorTest method testRemoteClientWithInternalAuthInjectsAuthenticationContext.

@Test
public void testRemoteClientWithInternalAuthInjectsAuthenticationContext() throws Exception {
    CConfiguration cConf = injector.getInstance(CConfiguration.class);
    cConf.setBoolean(Constants.Security.INTERNAL_AUTH_ENABLED, true);
    RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
    RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
    // Set authentication context principal.
    String expectedName = "somebody";
    String expectedCredValue = "credential";
    Credential.CredentialType expectedCredType = Credential.CredentialType.EXTERNAL;
    System.setProperty("user.name", expectedName);
    System.setProperty("user.credential.value", expectedCredValue);
    System.setProperty("user.credential.type", expectedCredType.name());
    HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
    int responseCode = conn.getResponseCode();
    // Verify that the request received the expected headers.
    HttpHeaders headers = testHttpHandler.getRequest().headers();
    Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
    Assert.assertEquals(expectedName, headers.get(Constants.Security.Headers.USER_ID));
    Assert.assertEquals(String.format("%s %s", expectedCredType.getQualifiedName(), expectedCredValue), headers.get(Constants.Security.Headers.RUNTIME_TOKEN));
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) Credential(io.cdap.cdap.proto.security.Credential) HttpURLConnection(java.net.HttpURLConnection) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Test(org.junit.Test)

Example 9 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by caskdata.

the class RemoteClientAuthenticatorTest method testRemoteClientWithoutInternalAuthInjectsNoAuthenticationContext.

@Test
public void testRemoteClientWithoutInternalAuthInjectsNoAuthenticationContext() throws Exception {
    CConfiguration cConf = injector.getInstance(CConfiguration.class);
    cConf.setBoolean(Constants.Security.INTERNAL_AUTH_ENABLED, false);
    RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
    RemoteClient remoteClient = remoteClientFactory.createRemoteClient(TEST_SERVICE, new HttpRequestConfig(15000, 15000, false), "/");
    HttpURLConnection conn = remoteClient.openConnection(HttpMethod.GET, "");
    int responseCode = conn.getResponseCode();
    // Verify that the request received the expected headers.
    HttpHeaders headers = testHttpHandler.getRequest().headers();
    Assert.assertEquals(HttpResponseStatus.OK.code(), responseCode);
    Assert.assertFalse(headers.contains(Constants.Security.Headers.USER_ID));
    Assert.assertFalse(headers.contains(Constants.Security.Headers.RUNTIME_TOKEN));
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) HttpURLConnection(java.net.HttpURLConnection) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Test(org.junit.Test)

Example 10 with HttpRequestConfig

use of io.cdap.common.http.HttpRequestConfig in project cdap by cdapio.

the class AuthenticationServiceMainTest method testBasicAuthenticationEnabled.

@Test
public void testBasicAuthenticationEnabled() throws IOException {
    HttpResponse response = HttpRequests.execute(HttpRequest.get(getAuthenticationBaseURI().toURL()).build(), new HttpRequestConfig(0, 0, false));
    Assert.assertEquals("basic realm=\"null\"", response.getHeaders().get("WWW-Authenticate").stream().findFirst().orElse(null));
    Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, response.getResponseCode());
    Injector injector = getServiceMainInstance(AuthenticationServiceMain.class).getInjector();
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    Discoverable authenticationEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Service.EXTERNAL_AUTHENTICATION)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull(authenticationEndpoint);
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) Injector(com.google.inject.Injector) HttpResponse(io.cdap.common.http.HttpResponse) HttpRequestConfig(io.cdap.common.http.HttpRequestConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Aggregations

HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)32 Test (org.junit.Test)16 RemoteClientFactory (io.cdap.cdap.common.internal.remote.RemoteClientFactory)14 HttpResponse (io.cdap.common.http.HttpResponse)14 URL (java.net.URL)14 DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)10 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)10 HttpURLConnection (java.net.HttpURLConnection)8 LocationFactory (org.apache.twill.filesystem.LocationFactory)7 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)6 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)6 NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)6 IOException (java.io.IOException)6 URI (java.net.URI)6 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)6 Id (io.cdap.cdap.common.id.Id)4 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)4 Discoverable (org.apache.twill.discovery.Discoverable)4 Location (org.apache.twill.filesystem.Location)4 Service (com.google.common.util.concurrent.Service)3