Fixing missing context-path usage
without this change when the user adds context-path it gets ignored and WireMock fails to register stubs with this change we update the WireMock instance to include the context path fixes #99
This commit is contained in:
@@ -196,7 +196,7 @@ class StubRunnerExecutor implements StubFinder {
|
||||
return condition ? this.stubServer.getStubUrl() : null;
|
||||
}
|
||||
|
||||
private void startStubServers(StubRunnerOptions stubRunnerOptions,
|
||||
private void startStubServers(final StubRunnerOptions stubRunnerOptions,
|
||||
final StubConfiguration stubConfiguration, StubRepository repository) {
|
||||
final List<WiremockMappingDescriptor> mappings = repository
|
||||
.getProjectDescriptors();
|
||||
@@ -206,7 +206,7 @@ class StubRunnerExecutor implements StubFinder {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("There are no HTTP related contracts. Won't start any servers");
|
||||
}
|
||||
this.stubServer = new StubServer(stubConfiguration, mappings, contracts, new NoOpHttpServerStub());
|
||||
this.stubServer = new StubServer(stubRunnerOptions, stubConfiguration, mappings, contracts, new NoOpHttpServerStub());
|
||||
return;
|
||||
}
|
||||
if (contracts.isEmpty()) {
|
||||
@@ -215,14 +215,14 @@ class StubRunnerExecutor implements StubFinder {
|
||||
+ "that's why will start the server - maybe you know what you're doing...");
|
||||
}
|
||||
if (port != null && port >= 0) {
|
||||
this.stubServer = new StubServer(stubConfiguration, mappings, contracts,
|
||||
this.stubServer = new StubServer(stubRunnerOptions, stubConfiguration, mappings, contracts,
|
||||
new WireMockHttpServerStub(port));
|
||||
} else {
|
||||
this.stubServer = this.portScanner
|
||||
.tryToExecuteWithFreePort(new PortCallback<StubServer>() {
|
||||
@Override
|
||||
public StubServer call(int availablePort) {
|
||||
return new StubServer(stubConfiguration,
|
||||
return new StubServer(stubRunnerOptions, stubConfiguration,
|
||||
mappings, contracts,
|
||||
new WireMockHttpServerStub(availablePort));
|
||||
}
|
||||
|
||||
@@ -61,6 +61,11 @@ public class StubRunnerOptions {
|
||||
*/
|
||||
final Map<StubConfiguration, Integer> stubIdsToPortMapping;
|
||||
|
||||
/**
|
||||
* Context Path of the server
|
||||
*/
|
||||
final String contextPath;
|
||||
|
||||
public StubRunnerOptions(Integer minPortValue, Integer maxPortValue,
|
||||
String stubRepositoryRoot, boolean workOffline, String stubsClassifier,
|
||||
Collection<StubConfiguration> dependencies,
|
||||
@@ -72,6 +77,21 @@ public class StubRunnerOptions {
|
||||
this.stubsClassifier = stubsClassifier;
|
||||
this.dependencies = dependencies;
|
||||
this.stubIdsToPortMapping = stubIdsToPortMapping;
|
||||
this.contextPath = "";
|
||||
}
|
||||
|
||||
public StubRunnerOptions(Integer minPortValue, Integer maxPortValue,
|
||||
String stubRepositoryRoot, boolean workOffline, String stubsClassifier,
|
||||
Collection<StubConfiguration> dependencies,
|
||||
Map<StubConfiguration, Integer> stubIdsToPortMapping, String contextPath) {
|
||||
this.minPortValue = minPortValue;
|
||||
this.maxPortValue = maxPortValue;
|
||||
this.stubRepositoryRoot = stubRepositoryRoot;
|
||||
this.workOffline = workOffline;
|
||||
this.stubsClassifier = stubsClassifier;
|
||||
this.dependencies = dependencies;
|
||||
this.stubIdsToPortMapping = stubIdsToPortMapping;
|
||||
this.contextPath = contextPath;
|
||||
}
|
||||
|
||||
public Integer port(StubConfiguration stubConfiguration) {
|
||||
@@ -90,18 +110,6 @@ public class StubRunnerOptions {
|
||||
return this.maxPortValue;
|
||||
}
|
||||
|
||||
public String getStubRepositoryRoot() {
|
||||
return this.stubRepositoryRoot;
|
||||
}
|
||||
|
||||
public boolean isWorkOffline() {
|
||||
return this.workOffline;
|
||||
}
|
||||
|
||||
public String getStubsClassifier() {
|
||||
return this.stubsClassifier;
|
||||
}
|
||||
|
||||
public Collection<StubConfiguration> getDependencies() {
|
||||
return this.dependencies;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ public class StubRunnerOptionsBuilder {
|
||||
private String stubRepositoryRoot;
|
||||
private boolean workOffline = false;
|
||||
private String stubsClassifier = "stubs";
|
||||
private String contextPath = "";
|
||||
|
||||
public StubRunnerOptionsBuilder() {
|
||||
}
|
||||
@@ -95,18 +96,25 @@ public class StubRunnerOptionsBuilder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public StubRunnerOptionsBuilder withContextPath(String contextPath) {
|
||||
this.contextPath = contextPath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StubRunnerOptionsBuilder withOptions(StubRunnerOptions options) {
|
||||
this.minPortValue = options.minPortValue;
|
||||
this.maxPortValue = options.maxPortValue;
|
||||
this.stubRepositoryRoot = options.stubRepositoryRoot;
|
||||
this.workOffline = options.workOffline;
|
||||
this.stubsClassifier = options.stubsClassifier;
|
||||
this.contextPath = options.contextPath;
|
||||
return this;
|
||||
}
|
||||
|
||||
public StubRunnerOptions build() {
|
||||
return new StubRunnerOptions(this.minPortValue, this.maxPortValue, this.stubRepositoryRoot,
|
||||
this.workOffline, this.stubsClassifier, buildDependencies(), this.stubIdsToPortMapping);
|
||||
this.workOffline, this.stubsClassifier, buildDependencies(), this.stubIdsToPortMapping,
|
||||
this.contextPath);
|
||||
}
|
||||
|
||||
private Collection<StubConfiguration> buildDependencies() {
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.util.Collection;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.contract.spec.Contract;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
|
||||
@@ -34,9 +35,11 @@ class StubServer {
|
||||
final StubConfiguration stubConfiguration;
|
||||
final Collection<WiremockMappingDescriptor> mappings;
|
||||
final Collection<Contract> contracts;
|
||||
private final StubRunnerOptions stubRunnerOptions;
|
||||
|
||||
StubServer(StubConfiguration stubConfiguration, Collection<WiremockMappingDescriptor> mappings,
|
||||
Collection<Contract> contracts, HttpServerStub httpServerStub) {
|
||||
StubServer(StubRunnerOptions stubRunnerOptions, StubConfiguration stubConfiguration,
|
||||
Collection<WiremockMappingDescriptor> mappings, Collection<Contract> contracts, HttpServerStub httpServerStub) {
|
||||
this.stubRunnerOptions = stubRunnerOptions;
|
||||
this.stubConfiguration = stubConfiguration;
|
||||
this.mappings = mappings;
|
||||
this.httpServerStub = httpServerStub;
|
||||
@@ -68,13 +71,23 @@ class StubServer {
|
||||
|
||||
public URL getStubUrl() {
|
||||
try {
|
||||
return new URL("http://localhost:" + getPort());
|
||||
return new URL("http://localhost:" + getPort() + prependSlashIfNecessary(this.stubRunnerOptions.contextPath));
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
throw new IllegalStateException("Cannot parse URL", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String prependSlashIfNecessary(String contextPath) {
|
||||
if (!StringUtils.hasText(contextPath)) {
|
||||
return "";
|
||||
}
|
||||
if (contextPath.startsWith("/")) {
|
||||
return contextPath;
|
||||
}
|
||||
return "/" + contextPath;
|
||||
}
|
||||
|
||||
public StubConfiguration getStubConfiguration() {
|
||||
return this.stubConfiguration;
|
||||
}
|
||||
@@ -84,7 +97,7 @@ class StubServer {
|
||||
}
|
||||
|
||||
private void registerStubMappings() {
|
||||
WireMock wireMock = new WireMock("localhost", this.httpServerStub.port());
|
||||
WireMock wireMock = new WireMock("localhost", this.httpServerStub.port(), prependSlashIfNecessary(this.stubRunnerOptions.contextPath));
|
||||
registerDefaultHealthChecks(wireMock);
|
||||
registerStubs(this.mappings, wireMock);
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.IOException;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.contract.stubrunner.AetherStubDownloader;
|
||||
import org.springframework.cloud.contract.stubrunner.BatchStubRunner;
|
||||
@@ -48,6 +49,8 @@ public class StubRunnerConfiguration {
|
||||
private StubDownloader stubDownloader;
|
||||
@Autowired
|
||||
private StubRunnerProperties props;
|
||||
@Autowired(required = false)
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
/**
|
||||
* Bean that initializes stub runners, runs them and on shutdown closes them. Upon its
|
||||
@@ -63,7 +66,9 @@ public class StubRunnerConfiguration {
|
||||
.withWorkOffline(this.props.getRepositoryRoot() == null
|
||||
|| this.props.isWorkOffline())
|
||||
.withStubsClassifier(this.props.getClassifier())
|
||||
.withStubs(this.props.getIds()).build();
|
||||
.withStubs(this.props.getIds())
|
||||
.withContextPath(contextPath())
|
||||
.build();
|
||||
BatchStubRunner batchStubRunner = new BatchStubRunnerFactory(stubRunnerOptions,
|
||||
this.stubDownloader != null ? this.stubDownloader
|
||||
: new AetherStubDownloader(stubRunnerOptions),
|
||||
@@ -78,4 +83,11 @@ public class StubRunnerConfiguration {
|
||||
return stubRepositoryRoot != null ? stubRepositoryRoot.getURI().toString() : "";
|
||||
}
|
||||
|
||||
private String contextPath() {
|
||||
if (this.serverProperties == null) {
|
||||
return "";
|
||||
}
|
||||
return this.serverProperties.getContextPath();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ class StubServerSpec extends Specification {
|
||||
def 'should register stub mappings upon server start'() {
|
||||
given:
|
||||
List<WiremockMappingDescriptor> mappingDescriptors = new StubRepository(repository).getProjectDescriptors()
|
||||
StubServer pingStubServer = new StubServer(stubConfiguration, mappingDescriptors, [],
|
||||
StubServer pingStubServer = new StubServer(new TestStubRunnerOptions(), stubConfiguration, mappingDescriptors, [],
|
||||
new WireMockHttpServerStub(STUB_SERVER_PORT))
|
||||
when:
|
||||
pingStubServer.start()
|
||||
@@ -40,7 +40,7 @@ class StubServerSpec extends Specification {
|
||||
def 'should provide stub server URL'() {
|
||||
given:
|
||||
List<WiremockMappingDescriptor> mappingDescriptors = new StubRepository(repository).getProjectDescriptors()
|
||||
StubServer pingStubServer = new StubServer(stubConfiguration, mappingDescriptors, [],
|
||||
StubServer pingStubServer = new StubServer(new TestStubRunnerOptions(), stubConfiguration, mappingDescriptors, [],
|
||||
new WireMockHttpServerStub(STUB_SERVER_PORT))
|
||||
when:
|
||||
pingStubServer.start()
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package org.springframework.cloud.contract.stubrunner
|
||||
|
||||
import groovy.transform.PackageScope
|
||||
|
||||
/**
|
||||
* @author Marcin Grzejszczak
|
||||
*/
|
||||
@PackageScope class TestStubRunnerOptions extends StubRunnerOptions {
|
||||
public TestStubRunnerOptions() {
|
||||
super(1, 2, "", false, "", new ArrayList<StubConfiguration>(),
|
||||
new HashMap<StubConfiguration, Integer>());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user