Search in sources :

Example 16 with DefaultHttpRequestConfig

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

the class ArtifactHttpHandlerTestBase method getResults.

/**
 * @return the contents from doing a get on the given url as the given type, or null if a 404 is returned
 */
@Nullable
<T> T getResults(URL endpoint, Type type) throws IOException {
    HttpResponse response = HttpRequests.execute(HttpRequest.get(endpoint).build(), new DefaultHttpRequestConfig(false));
    int responseCode = response.getResponseCode();
    if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
        return null;
    }
    Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    return GSON.fromJson(response.getResponseBodyAsString(), type);
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) Nullable(javax.annotation.Nullable)

Example 17 with DefaultHttpRequestConfig

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

the class PreferencesClientTestRun method getServiceResponse.

private HttpResponse getServiceResponse(URL serviceURL) throws IOException, InterruptedException {
    int iterations = 0;
    HttpResponse response;
    do {
        response = HttpRequests.execute(HttpRequest.builder(HttpMethod.GET, serviceURL).build(), new DefaultHttpRequestConfig(false));
        if (response.getResponseCode() == HttpURLConnection.HTTP_OK) {
            return response;
        }
        TimeUnit.MILLISECONDS.sleep(50);
        iterations++;
    } while (iterations <= 100);
    return response;
}
Also used : DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse)

Example 18 with DefaultHttpRequestConfig

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

the class MetricsClientTestRun method testAll.

@Test
public void testAll() throws Exception {
    appClient.deploy(NamespaceId.DEFAULT, createAppJarFile(FakeApp.class));
    ApplicationId app = NamespaceId.DEFAULT.app(FakeApp.NAME);
    ServiceId service = app.service(PingService.NAME);
    try {
        programClient.start(service);
        programClient.waitForStatus(service, ProgramStatus.RUNNING, 15, TimeUnit.SECONDS);
        URL serviceURL = serviceClient.getServiceURL(service);
        URL pingURL = new URL(serviceURL, "ping");
        HttpResponse response = HttpRequests.execute(HttpRequest.get(pingURL).build(), new DefaultHttpRequestConfig(false));
        Assert.assertEquals(200, response.getResponseCode());
        Tasks.waitFor(true, () -> metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), ImmutableMap.of("start", "now-20s", "end", "now")).getSeries().length > 0, 10, TimeUnit.SECONDS);
        MetricQueryResult result = metricsClient.query(MetricsTags.service(service), Constants.Metrics.Name.Service.SERVICE_INPUT);
        Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
        result = metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), Collections.singletonMap("aggregate", "true"));
        Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
        result = metricsClient.query(MetricsTags.service(service), Collections.singletonList(Constants.Metrics.Name.Service.SERVICE_INPUT), Collections.emptyList(), ImmutableMap.of("start", "now-20s", "end", "now"));
        Assert.assertEquals(1, result.getSeries()[0].getData()[0].getValue());
        List<MetricTagValue> tags = metricsClient.searchTags(MetricsTags.service(service));
        Assert.assertEquals(1, tags.size());
        Assert.assertEquals("run", tags.get(0).getName());
        List<String> metrics = metricsClient.searchMetrics(MetricsTags.service(service));
        Assert.assertTrue(metrics.contains(Constants.Metrics.Name.Service.SERVICE_INPUT));
    } finally {
        programClient.stop(service);
        assertProgramRuns(programClient, service, ProgramRunStatus.KILLED, 1, 10);
        appClient.delete(app);
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) MetricTagValue(io.cdap.cdap.proto.MetricTagValue) HttpResponse(io.cdap.common.http.HttpResponse) MetricQueryResult(io.cdap.cdap.proto.MetricQueryResult) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) URL(java.net.URL) ServiceId(io.cdap.cdap.proto.id.ServiceId) Test(org.junit.Test)

Example 19 with DefaultHttpRequestConfig

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

the class ServiceClientTestRun method testGetServiceURL.

@Test
public void testGetServiceURL() throws Exception {
    URL url = new URL(serviceClient.getServiceURL(service), "ping");
    HttpRequest request = HttpRequest.builder(HttpMethod.GET, url).build();
    HttpResponse response = HttpRequests.execute(request, new DefaultHttpRequestConfig(false));
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 20 with DefaultHttpRequestConfig

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

the class TrafficRelayServerTest method testRelay.

@Test
public void testRelay() throws Exception {
    NettyHttpService httpServer = NettyHttpService.builder("test").setHttpHandlers(new TestHandler()).build();
    httpServer.start();
    try {
        TrafficRelayServer relayServer = new TrafficRelayServer(InetAddress.getLoopbackAddress(), httpServer::getBindAddress);
        relayServer.startAndWait();
        try {
            InetSocketAddress relayAddr = relayServer.getBindAddress();
            // GET
            URL url = new URL(String.format("http://%s:%d/ping", relayAddr.getHostName(), relayAddr.getPort()));
            HttpResponse response = HttpRequests.execute(HttpRequest.get(url).build(), new DefaultHttpRequestConfig(false));
            Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            // POST
            url = new URL(String.format("http://%s:%d/echo", relayAddr.getHostName(), relayAddr.getPort()));
            response = HttpRequests.execute(HttpRequest.post(url).withBody("Testing").build(), new DefaultHttpRequestConfig(false));
            Assert.assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
            Assert.assertEquals("Testing", response.getResponseBodyAsString());
        } finally {
            relayServer.stopAndWait();
        }
    } finally {
        httpServer.stop();
    }
}
Also used : TestHandler(io.cdap.cdap.internal.app.runtime.monitor.proxy.TestHandler) InetSocketAddress(java.net.InetSocketAddress) DefaultHttpRequestConfig(io.cdap.cdap.common.http.DefaultHttpRequestConfig) NettyHttpService(io.cdap.http.NettyHttpService) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) TrafficRelayServer(io.cdap.cdap.internal.app.runtime.monitor.TrafficRelayServer) Test(org.junit.Test)

Aggregations

DefaultHttpRequestConfig (io.cdap.cdap.common.http.DefaultHttpRequestConfig)82 HttpResponse (io.cdap.common.http.HttpResponse)62 URL (java.net.URL)56 Test (org.junit.Test)50 HttpRequest (io.cdap.common.http.HttpRequest)24 RunnableTaskRequest (io.cdap.cdap.api.service.worker.RunnableTaskRequest)14 URI (java.net.URI)14 InetSocketAddress (java.net.InetSocketAddress)12 Discoverable (org.apache.twill.discovery.Discoverable)12 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)8 Gson (com.google.gson.Gson)7 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)6 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)6 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)6 HttpRequestConfig (io.cdap.common.http.HttpRequestConfig)6 File (java.io.File)6 SConfiguration (io.cdap.cdap.common.conf.SConfiguration)4 ResolvingDiscoverable (io.cdap.cdap.common.discovery.ResolvingDiscoverable)4 NoOpInternalAuthenticator (io.cdap.cdap.common.internal.remote.NoOpInternalAuthenticator)4 RemoteClient (io.cdap.cdap.common.internal.remote.RemoteClient)4