Skip to content

Commit

Permalink
refactor to not create anonymous instances on every call
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Nov 10, 2023
1 parent b4ecd95 commit fab1514
Showing 1 changed file with 53 additions and 49 deletions.
102 changes: 53 additions & 49 deletions src/test/java/itest/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,56 +92,38 @@ HttpResponse<Buffer> delete(String url, boolean authentication, int timeout)

public static class TestWebClient extends WebClientBase {

private final RedirectExtensions extensions;

public TestWebClient(HttpClient client, WebClientOptions options) {
super(client, options);
this.extensions = new RedirectExtensionsImpl();
}

public RedirectExtensions extensions() {
return new RedirectExtensions() {
public HttpResponse<Buffer> post(
String url, boolean authentication, MultiMap form, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req = TestWebClient.this.request(HttpMethod.POST, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
if (form != null) {
req.sendForm(
form,
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
} else {
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
}
if (future.get().statusCode() == 308) {
return post(future.get().getHeader("Location"), true, form, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}
return extensions;
}

public HttpResponse<Buffer> delete(String url, boolean authentication, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req =
TestWebClient.this.request(HttpMethod.DELETE, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
private class RedirectExtensionsImpl implements RedirectExtensions {
public HttpResponse<Buffer> post(
String url, boolean authentication, MultiMap form, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req = TestWebClient.this.request(HttpMethod.POST, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
if (form != null) {
req.sendForm(
form,
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
} else {
req.send(
ar -> {
if (ar.succeeded()) {
Expand All @@ -150,12 +132,34 @@ public HttpResponse<Buffer> delete(String url, boolean authentication, int timeo
future.completeExceptionally(ar.cause());
}
});
if (future.get().statusCode() == 308) {
return delete(future.get().getHeader("Location"), true, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}
};
if (future.get().statusCode() == 308) {
return post(future.get().getHeader("Location"), true, form, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}

public HttpResponse<Buffer> delete(String url, boolean authentication, int timeout)
throws InterruptedException, ExecutionException, TimeoutException {
CompletableFuture<HttpResponse<Buffer>> future = new CompletableFuture<>();
RequestOptions options = new RequestOptions().setURI(url);
HttpRequest<Buffer> req = TestWebClient.this.request(HttpMethod.DELETE, options);
if (authentication) {
req.basicAuthentication("user", "pass");
}
req.send(
ar -> {
if (ar.succeeded()) {
future.complete(ar.result());
} else {
future.completeExceptionally(ar.cause());
}
});
if (future.get().statusCode() == 308) {
return delete(future.get().getHeader("Location"), true, timeout);
}
return future.get(timeout, TimeUnit.SECONDS);
}
}
}
}

0 comments on commit fab1514

Please sign in to comment.