Search in sources :

Example 21 with Callback

use of org.apache.hc.core5.function.Callback in project wiremock by wiremock.

the class WebhooksAcceptanceTest method addsFixedDelayViaDSL.

@Test
public void addsFixedDelayViaDSL() throws Exception {
    final int DELAY_MILLISECONDS = 1_000;
    rule.stubFor(post(urlPathEqualTo("/delayed")).willReturn(ok()).withPostServeAction("webhook", webhook().withFixedDelay(DELAY_MILLISECONDS).withMethod(RequestMethod.GET).withUrl(targetServer.url("/callback"))));
    verify(0, postRequestedFor(anyUrl()));
    client.post("/delayed", new StringEntity("", TEXT_PLAIN));
    Stopwatch stopwatch = Stopwatch.createStarted();
    waitForRequestToTargetServer();
    stopwatch.stop();
    double elapsedMilliseconds = stopwatch.elapsed(MILLISECONDS);
    assertThat(elapsedMilliseconds, closeTo(DELAY_MILLISECONDS, 500.0));
    verify(1, getRequestedFor(urlEqualTo("/callback")));
}
Also used : StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Stopwatch(com.google.common.base.Stopwatch) Test(org.junit.jupiter.api.Test)

Example 22 with Callback

use of org.apache.hc.core5.function.Callback in project wiremock by wiremock.

the class WebhooksAcceptanceTest method firesASingleWebhookWhenRequested.

@Test
public void firesASingleWebhookWhenRequested() throws Exception {
    rule.stubFor(post(urlPathEqualTo("/something-async")).willReturn(aResponse().withStatus(200)).withPostServeAction("webhook", webhook().withMethod(POST).withUrl(targetServer.url("/callback")).withHeader("Content-Type", "application/json").withHeader("X-Multi", "one", "two").withBody("{ \"result\": \"SUCCESS\" }")));
    verify(0, postRequestedFor(anyUrl()));
    client.post("/something-async", new StringEntity("", TEXT_PLAIN));
    waitForRequestToTargetServer();
    targetServer.verify(1, postRequestedFor(urlEqualTo("/callback")).withHeader("Content-Type", equalTo("application/json")).withRequestBody(equalToJson("{ \"result\": \"SUCCESS\" }")));
    List<String> multiHeaderValues = targetServer.findAll(postRequestedFor(urlEqualTo("/callback"))).get(0).header("X-Multi").values();
    assertThat(multiHeaderValues, hasItems("one", "two"));
    System.out.println("All info notifications:\n" + testNotifier.getInfoMessages().stream().map(message -> message.replace("\n", "\n>>> ")).collect(Collectors.joining("\n>>> ")));
    assertThat(testNotifier.getInfoMessages(), hasItem(allOf(containsString("Webhook POST request to"), containsString("/callback returned status"), containsString("200"))));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Stopwatch(com.google.common.base.Stopwatch) WireMock.any(com.github.tomakehurst.wiremock.client.WireMock.any) ConsoleNotifier(com.github.tomakehurst.wiremock.common.ConsoleNotifier) WireMock(com.github.tomakehurst.wiremock.client.WireMock) WireMockExtension(com.github.tomakehurst.wiremock.junit5.WireMockExtension) CompositeNotifier(testsupport.CompositeNotifier) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) WireMockTestClient(testsupport.WireMockTestClient) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TEXT_PLAIN(org.apache.hc.core5.http.ContentType.TEXT_PLAIN) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) LoggedRequest(com.github.tomakehurst.wiremock.verification.LoggedRequest) Webhooks.webhook(org.wiremock.webhooks.Webhooks.webhook) ServeEvent(com.github.tomakehurst.wiremock.stubbing.ServeEvent) WireMockConfiguration.options(com.github.tomakehurst.wiremock.core.WireMockConfiguration.options) Matchers(org.hamcrest.Matchers) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Webhooks(org.wiremock.webhooks.Webhooks) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TestNotifier(testsupport.TestNotifier) POST(com.github.tomakehurst.wiremock.http.RequestMethod.POST) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Admin(com.github.tomakehurst.wiremock.core.Admin) RequestMethod(com.github.tomakehurst.wiremock.http.RequestMethod) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) SECONDS(java.util.concurrent.TimeUnit.SECONDS) PostServeAction(com.github.tomakehurst.wiremock.extension.PostServeAction) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) Test(org.junit.jupiter.api.Test)

Example 23 with Callback

use of org.apache.hc.core5.function.Callback in project invesdwin-util by invesdwin.

the class URIsConnectApacheAsync method openConnection.

public <T> Future<T> openConnection(final String method, final AsyncResponseConsumer<T> responseConsumer, final FutureCallback<T> callback) {
    final SimpleHttpRequest request = SimpleHttpRequests.create(method, uri);
    request.setConfig(getRequestConfig());
    if (headers != null) {
        for (final Entry<String, String> header : headers.entrySet()) {
            request.addHeader(header.getKey(), header.getValue());
        }
    }
    if (body != null) {
        final ContentType commonsContentType;
        if (contentType != null) {
            commonsContentType = ContentType.create(contentType);
        } else {
            commonsContentType = null;
        }
        request.setBody(body, commonsContentType);
    }
    final SimpleRequestProducer requestProducer = SimpleRequestProducer.create(request);
    final Future<T> response = getHttpClient().execute(requestProducer, responseConsumer, callback);
    return response;
}
Also used : SimpleRequestProducer(org.apache.hc.client5.http.async.methods.SimpleRequestProducer) ContentType(org.apache.hc.core5.http.ContentType) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest)

Example 24 with Callback

use of org.apache.hc.core5.function.Callback in project easeagent by megaease.

the class HttpClient5AsyncTracingInterceptor method doBefore.

@Override
public void doBefore(MethodInfo methodInfo, Context context) {
    AsyncRequestProducer requestProducer = (AsyncRequestProducer) methodInfo.getArgs()[0];
    HttpRequest request = AgentFieldReflectAccessor.getFieldValue(requestProducer, "request");
    if (request == null) {
        return;
    }
    InternalRequest internalRequest = new InternalRequest(request);
    RequestContext requestContext = context.clientRequest(internalRequest);
    HttpUtils.handleReceive(requestContext.span().start(), internalRequest);
    context.put(HttpClient5AsyncTracingInterceptor.class, requestContext);
    @SuppressWarnings("unchecked") FutureCallback<HttpResponse> callback = (FutureCallback<HttpResponse>) methodInfo.getArgs()[4];
    InternalFutureCallback internalFutureCallback = new InternalFutureCallback(callback, request, requestContext);
    methodInfo.changeArg(4, internalFutureCallback);
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpResponse(org.apache.hc.core5.http.HttpResponse) RequestContext(com.megaease.easeagent.plugin.api.context.RequestContext) AsyncRequestProducer(org.apache.hc.core5.http.nio.AsyncRequestProducer) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 25 with Callback

use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.

the class HttpAsyncServer method listen.

/**
 * @since 5.1
 */
public Future<ListenerEndpoint> listen(final SocketAddress address, final URIScheme scheme, final Object attachment, final FutureCallback<ListenerEndpoint> callback) {
    final InetSocketAddress inetSocketAddress = (InetSocketAddress) address;
    final EndpointParameters parameters = new EndpointParameters(scheme.id, canonicalName != null ? canonicalName : "localhost", inetSocketAddress.getPort(), attachment);
    return super.listen(address, parameters, callback);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) EndpointParameters(org.apache.hc.core5.reactor.EndpointParameters)

Aggregations

Test (org.junit.jupiter.api.Test)10 IOException (java.io.IOException)6 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)6 CancellationException (java.util.concurrent.CancellationException)5 TimeoutException (java.util.concurrent.TimeoutException)5 HttpResponse (org.apache.hc.core5.http.HttpResponse)5 ExecutionException (java.util.concurrent.ExecutionException)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)4 TimeoutValueException (org.apache.hc.core5.util.TimeoutValueException)4 Stopwatch (com.google.common.base.Stopwatch)3 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)3 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)3 ContentType (org.apache.hc.core5.http.ContentType)3 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)3 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2