Search in sources :

Example 96 with Response

use of org.asynchttpclient.Response in project druid by druid-io.

the class EmitterTest method testSanity.

@Test
public void testSanity() throws Exception {
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
    emitter = sizeBasedEmitter(2);
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    waitForEmission(emitter, 1);
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 97 with Response

use of org.asynchttpclient.Response in project druid by druid-io.

the class EmitterTest method testGzipContentEncoding.

@Test
public void testGzipContentEncoding() throws Exception {
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("plain-text", 1), new UnitEvent("plain-text", 2));
    emitter = sizeBasedEmitterWithContentEncoding(2, ContentEncoding.GZIP);
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws IOException {
            Assert.assertEquals(TARGET_URL, request.getUrl());
            Assert.assertEquals("application/json", request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE));
            Assert.assertEquals(HttpHeaders.Values.GZIP, request.getHeaders().get(HttpHeaders.Names.CONTENT_ENCODING));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ByteBuffer data = request.getByteBufferData().slice();
            byte[] dataArray = new byte[data.remaining()];
            data.get(dataArray);
            CompressionUtils.gunzip(new ByteArrayInputStream(dataArray), baos);
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), baos.toString(StandardCharsets.UTF_8.name()));
            return GoHandlers.immediateFuture(okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    waitForEmission(emitter, 1);
    closeNoFlush(emitter);
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 98 with Response

use of org.asynchttpclient.Response in project druid by druid-io.

the class ParametrizedUriEmitterTest method testEmitterWithFeedUriExtractor.

@Test
public void testEmitterWithFeedUriExtractor() throws Exception {
    Emitter emitter = parametrizedEmmiter("http://example.com/{feed}");
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1), new UnitEvent("test", 2));
    httpClient.setGoHandler(new GoHandler() {

        @Override
        public ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals("http://example.com/test", request.getUrl());
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(EmitterTest.okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    emitter.flush();
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 99 with Response

use of org.asynchttpclient.Response in project druid by druid-io.

the class ParametrizedUriEmitterTest method testEmitterWithParametrizedUriExtractor.

@Test
public void testEmitterWithParametrizedUriExtractor() throws Exception {
    Emitter emitter = parametrizedEmmiter("http://example.com/{key1}/{key2}");
    final List<UnitEvent> events = Arrays.asList(new UnitEvent("test", 1, ImmutableMap.of("key1", "val1", "key2", "val2")), new UnitEvent("test", 2, ImmutableMap.of("key1", "val1", "key2", "val2")));
    httpClient.setGoHandler(new GoHandler() {

        @Override
        protected ListenableFuture<Response> go(Request request) throws JsonProcessingException {
            Assert.assertEquals("http://example.com/val1/val2", request.getUrl());
            Assert.assertEquals(StringUtils.format("[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1))), StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString());
            return GoHandlers.immediateFuture(EmitterTest.okResponse());
        }
    }.times(1));
    for (UnitEvent event : events) {
        emitter.emit(event);
    }
    emitter.flush();
    Assert.assertTrue(httpClient.succeeded());
}
Also used : Response(org.asynchttpclient.Response) UnitEvent(org.apache.druid.java.util.emitter.service.UnitEvent) Request(org.asynchttpclient.Request) Test(org.junit.Test)

Example 100 with Response

use of org.asynchttpclient.Response in project killbill by killbill.

the class KillBillAuth0Realm method findAuth0UserPermissions.

private Set<String> findAuth0UserPermissions(final String userId, final String token) {
    final String path;
    try {
        path = "/api/v2/users/" + URLEncoder.encode(userId, "UTF-8") + "/permissions";
    } catch (final UnsupportedEncodingException e) {
        // Should never happen
        throw new IllegalStateException(e);
    }
    final Response auth0RawResponse = doGetRequest(path, token);
    try {
        final List<Map<String, Object>> auth0Response = mapper.readValue(auth0RawResponse.getResponseBodyAsStream(), new TypeReference<List<Map<String, Object>>>() {
        });
        final Set<String> permissions = new HashSet<String>();
        for (final Map<String, Object> group : auth0Response) {
            final Object permission = group.get("permission_name");
            if (permission != null) {
                permissions.add((String) permission);
            }
        }
        return permissions;
    } catch (final IOException e) {
        log.warn("Unable to read response from Auth0", e);
        throw new AuthorizationException(e);
    }
}
Also used : AuthorizationException(org.apache.shiro.authz.AuthorizationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Response(org.asynchttpclient.Response) List(java.util.List) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Response (org.asynchttpclient.Response)142 Test (org.testng.annotations.Test)85 AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)79 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)55 HttpServletResponse (javax.servlet.http.HttpServletResponse)42 BoundRequestBuilder (org.asynchttpclient.BoundRequestBuilder)32 Request (org.asynchttpclient.Request)29 IOException (java.io.IOException)21 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)16 PubSubMessage (com.yahoo.bullet.pubsub.PubSubMessage)14 RESTPubSubTest.getOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getOkResponse)11 TimeoutException (java.util.concurrent.TimeoutException)11 RequestBuilder (org.asynchttpclient.RequestBuilder)11 UnitEvent (org.apache.druid.java.util.emitter.service.UnitEvent)10 RESTPubSubTest.getNotOkResponse (com.yahoo.bullet.pubsub.rest.RESTPubSubTest.getNotOkResponse)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 Map (java.util.Map)9 AuthorizationException (org.apache.shiro.authz.AuthorizationException)9 TestUtils.createTempFile (org.asynchttpclient.test.TestUtils.createTempFile)9