Skip to content

Commit

Permalink
Merge branch '4.0.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
marcingrzejszczak committed Sep 4, 2023
2 parents 150bdd7 + b1eba33 commit 5bfd99c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ 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");
if (log.isDebugEnabled()) {
log.debug("Registered property source for dynamic http port");
}
}
else {
Map<String, Object> 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,
Expand All @@ -70,9 +77,7 @@ private void registerPort(ConfigurableEnvironment environment) {
}
}
else if (httpsPortProperty == -1) {
MutablePropertySources propertySources = environment.getPropertySources();
addPropertySource(propertySources);
Map<String, Object> source = ((MapPropertySource) propertySources.get("wiremock")).getSource();
Map<String, Object> 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");
Expand All @@ -81,6 +86,13 @@ else if (httpsPortProperty == -1) {

}

private Map<String, Object> getWireMockSource(ConfigurableEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();
addPropertySource(propertySources);
Map<String, Object> source = ((MapPropertySource) propertySources.get("wiremock")).getSource();
return source;
}

private boolean isHttpsDynamic(int httpsPortProperty) {
return httpsPortProperty == 0;
}
Expand All @@ -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<String, Object> source = ((MapPropertySource) propertySources.get("wiremock")).getSource();
Map<String, Object> source = getWireMockSource(environment);
int port = TestSocketUtils.findAvailableTcpPort(minPort, maxPort);
source.put(portProperty, port);
if (log.isDebugEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}

}

0 comments on commit 5bfd99c

Please sign in to comment.