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);
}
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;
}
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);
}
}
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());
}
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();
}
}
Aggregations