From fab15142475f0f81c27fc6a1455497d76ba326fd Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 9 Nov 2023 11:31:15 -0500 Subject: [PATCH] refactor to not create anonymous instances on every call --- src/test/java/itest/util/Utils.java | 102 +++++++++++++++------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/src/test/java/itest/util/Utils.java b/src/test/java/itest/util/Utils.java index 8363a263d..2e120df1b 100644 --- a/src/test/java/itest/util/Utils.java +++ b/src/test/java/itest/util/Utils.java @@ -92,56 +92,38 @@ HttpResponse 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 post( - String url, boolean authentication, MultiMap form, int timeout) - throws InterruptedException, ExecutionException, TimeoutException { - CompletableFuture> future = new CompletableFuture<>(); - RequestOptions options = new RequestOptions().setURI(url); - HttpRequest 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 delete(String url, boolean authentication, int timeout) - throws InterruptedException, ExecutionException, TimeoutException { - CompletableFuture> future = new CompletableFuture<>(); - RequestOptions options = new RequestOptions().setURI(url); - HttpRequest req = - TestWebClient.this.request(HttpMethod.DELETE, options); - if (authentication) { - req.basicAuthentication("user", "pass"); - } + private class RedirectExtensionsImpl implements RedirectExtensions { + public HttpResponse post( + String url, boolean authentication, MultiMap form, int timeout) + throws InterruptedException, ExecutionException, TimeoutException { + CompletableFuture> future = new CompletableFuture<>(); + RequestOptions options = new RequestOptions().setURI(url); + HttpRequest 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()) { @@ -150,12 +132,34 @@ public HttpResponse 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 delete(String url, boolean authentication, int timeout) + throws InterruptedException, ExecutionException, TimeoutException { + CompletableFuture> future = new CompletableFuture<>(); + RequestOptions options = new RequestOptions().setURI(url); + HttpRequest 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); + } } } }