From a15db490f169412e50fc3140d95d8a70108465d5 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Mon, 4 Sep 2023 18:32:32 +0200 Subject: [PATCH] Defaults the WireMock server port to 8080; fixes gh-1920 --- .../wiremock/WireMockApplicationListener.java | 24 ++++++++--- ...utoConfigureWireMockDefaultSetupTests.java | 43 +++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockDefaultSetupTests.java diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java index 047e784333..7fa4aa6e2a 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/WireMockApplicationListener.java @@ -53,7 +53,7 @@ public void onApplicationEvent(ApplicationPreparedEvent event) { private void registerPort(ConfigurableEnvironment environment) { Integer httpPortProperty = environment.getProperty("wiremock.server.port", Integer.class); // If the httpPortProperty is not found it means the AutoConfigureWireMock hasn't - // been initialised. + // been initialised or the default was used if (httpPortProperty != null && isHttpDynamic(httpPortProperty)) { registerPropertySourceForDynamicEntries(environment, "wiremock.server.port", 10000, 12500, "wiremock.server.port-dynamic"); @@ -61,6 +61,13 @@ private void registerPort(ConfigurableEnvironment environment) { log.debug("Registered property source for dynamic http port"); } } + else { + Map source = getWireMockSource(environment); + source.put("wiremock.server.port", 8080); + if (log.isDebugEnabled()) { + log.debug("Registered WireMock server port property to the default <8080> value"); + } + } int httpsPortProperty = environment.getProperty("wiremock.server.https-port", Integer.class, 0); if (isHttpsDynamic(httpsPortProperty)) { registerPropertySourceForDynamicEntries(environment, "wiremock.server.https-port", 12500, 15000, @@ -70,9 +77,7 @@ private void registerPort(ConfigurableEnvironment environment) { } } else if (httpsPortProperty == -1) { - MutablePropertySources propertySources = environment.getPropertySources(); - addPropertySource(propertySources); - Map source = ((MapPropertySource) propertySources.get("wiremock")).getSource(); + Map source = getWireMockSource(environment); source.put("wiremock.server.https-port-dynamic", true); if (log.isDebugEnabled()) { log.debug("Registered property source for dynamic https with https port property set to -1"); @@ -81,6 +86,13 @@ else if (httpsPortProperty == -1) { } + private Map getWireMockSource(ConfigurableEnvironment environment) { + MutablePropertySources propertySources = environment.getPropertySources(); + addPropertySource(propertySources); + Map source = ((MapPropertySource) propertySources.get("wiremock")).getSource(); + return source; + } + private boolean isHttpsDynamic(int httpsPortProperty) { return httpsPortProperty == 0; } @@ -91,9 +103,7 @@ private boolean isHttpDynamic(Integer httpPortProperty) { private void registerPropertySourceForDynamicEntries(ConfigurableEnvironment environment, String portProperty, int minPort, int maxPort, String dynamicPortProperty) { - MutablePropertySources propertySources = environment.getPropertySources(); - addPropertySource(propertySources); - Map source = ((MapPropertySource) propertySources.get("wiremock")).getSource(); + Map source = getWireMockSource(environment); int port = SocketUtils.findAvailableTcpPort(minPort, maxPort); source.put(portProperty, port); if (log.isDebugEnabled()) { diff --git a/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockDefaultSetupTests.java b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockDefaultSetupTests.java new file mode 100644 index 0000000000..03c638ad54 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockDefaultSetupTests.java @@ -0,0 +1,43 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.wiremock; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.BDDAssertions.then; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = WiremockTestsApplication.class, + properties = "app.baseUrl=http://localhost:${wiremock.server.port}", webEnvironment = WebEnvironment.NONE) +@AutoConfigureWireMock +public class AutoConfigureWireMockDefaultSetupTests { + + @Value("${app.baseUrl}") + String url; + + @Test + public void contextLoads() throws Exception { + then(url).isEqualTo("http://localhost:8080"); + } + +}