From d0f4433eac4e7fc940366694d2165e410af5ff16 Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Thu, 12 Dec 2024 21:23:25 -0500 Subject: [PATCH] Make MockWebServerExtension public and allow for server customization (#532) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Make MockWebServerExtension public. This is in preparation for   most likely moving it to part of kiwi-test, i.e., in src/main * Add ability to customize the MockWebServer by using a   Consumer * Add a builder. It currently only provides the ability to   specify your own MockWebServer and the customization Consumer,   but by using a builder instead of multiple constructors, it   provides a nicer interface. The no-args constructor still   exists for situations where no customization is needed. --- .../mockwebserver/MockWebServerExtension.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/kiwiproject/test/okhttp3/mockwebserver/MockWebServerExtension.java b/src/test/java/org/kiwiproject/test/okhttp3/mockwebserver/MockWebServerExtension.java index 7b4da39..fcfa6db 100644 --- a/src/test/java/org/kiwiproject/test/okhttp3/mockwebserver/MockWebServerExtension.java +++ b/src/test/java/org/kiwiproject/test/okhttp3/mockwebserver/MockWebServerExtension.java @@ -1,5 +1,9 @@ package org.kiwiproject.test.okhttp3.mockwebserver; +import static java.util.Objects.isNull; +import static org.kiwiproject.base.KiwiPreconditions.requireNotNull; + +import lombok.Builder; import lombok.Getter; import lombok.experimental.Accessors; import okhttp3.mockwebserver.MockWebServer; @@ -7,8 +11,10 @@ import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.kiwiproject.io.KiwiIO; +import org.kiwiproject.util.function.KiwiConsumers; import java.io.IOException; +import java.util.function.Consumer; /** * A simple JUnit Jupiter extension that creates and starts a {@link MockWebServer} @@ -18,11 +24,38 @@ public class MockWebServerExtension implements BeforeEachCallback, AfterEachCall @Getter @Accessors(fluent = true) - private MockWebServer server; + private final MockWebServer server; + + private final Consumer serverCustomizer; + + /** + * Create a new instance. + *

+ * The extension will use a default {@link MockWebServer} instance. + *

+ * If you want to customize the server or provide your own {@link MockWebServer}, + * use the builder instead of this constructor. + */ + public MockWebServerExtension() { + this(new MockWebServer(), KiwiConsumers.noOp()); + } + + /** + * Create a new instance that will use the given {@link MockWebServer} and customizer. + * + * @param server the server + * @param serverCustomizer allows a test to configure the server, e.g., to customize the protocols + * it supports or to serve requests via HTTPS over TLS. + */ + @Builder + MockWebServerExtension(MockWebServer server, Consumer serverCustomizer) { + this.server = requireNotNull(server, "server must not be null"); + this.serverCustomizer = isNull(serverCustomizer) ? KiwiConsumers.noOp() : serverCustomizer; + } @Override public void beforeEach(ExtensionContext context) throws IOException { - server = new MockWebServer(); + serverCustomizer.accept(server); server.start(); }