Added UrlResource support for WireMock config; fixes gh-591
This commit is contained in:
@@ -16,21 +16,21 @@
|
||||
|
||||
package org.springframework.cloud.contract.wiremock.file;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
|
||||
import com.github.tomakehurst.wiremock.common.BinaryFile;
|
||||
import com.github.tomakehurst.wiremock.common.ClasspathFileSource;
|
||||
import com.github.tomakehurst.wiremock.common.FileSource;
|
||||
import com.github.tomakehurst.wiremock.common.SingleRootFileSource;
|
||||
import com.github.tomakehurst.wiremock.common.TextFile;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.UrlResource;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -60,6 +60,10 @@ public class ResourcesFileSource implements FileSource {
|
||||
FileSystemResource files = (FileSystemResource) resource;
|
||||
sources[i] = new SingleRootFileSource(files.getFile());
|
||||
}
|
||||
else if (resource instanceof UrlResource) {
|
||||
UrlResource files = (UrlResource) resource;
|
||||
sources[i] = new SingleRootFileSource(getFile(files));
|
||||
}
|
||||
else {
|
||||
throw new IllegalArgumentException("Unsupported resource type for file source: " + resource.getClass());
|
||||
}
|
||||
@@ -67,6 +71,15 @@ public class ResourcesFileSource implements FileSource {
|
||||
return sources;
|
||||
}
|
||||
|
||||
private static File getFile(UrlResource files) {
|
||||
try {
|
||||
return files.getFile();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public BinaryFile getBinaryFileNamed(String name) {
|
||||
for (FileSource resource : this.sources) {
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes=WiremockTestsApplication.class, properties="app.baseUrl=http://localhost:${wiremock.server.port}", webEnvironment=WebEnvironment.NONE)
|
||||
@DirtiesContext
|
||||
@AutoConfigureWireMock(port=0, files="classpath*:root")
|
||||
public class AutoConfigureWireMockFilesApplicationWithUrlResourceTests {
|
||||
|
||||
@Autowired
|
||||
private Service service;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
assertThat(this.service.go()).isEqualTo("{\"message\":\"Hello Root\"}");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user