Expose WireMockServer for @Autowired in test class
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.cloud.contract.wiremock;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -35,13 +36,13 @@ import com.jayway.jsonpath.JsonPath;
|
||||
|
||||
public class ContractRequestHandler implements ResultHandler {
|
||||
|
||||
static final String ATTRIBUTE_NAME_CONFIGURATION = "org.springframework.restdocs.configuration";
|
||||
|
||||
private Map<String, JsonPath> jsonPaths = new LinkedHashMap<>();
|
||||
private MediaType contentType;
|
||||
private WireMockSnippet snippet;
|
||||
private String name;
|
||||
|
||||
public ContractRequestHandler(WireMockSnippet snippet) {
|
||||
this.snippet = snippet;
|
||||
public ContractRequestHandler() {
|
||||
}
|
||||
|
||||
public ResultHandler contract(String name) {
|
||||
@@ -54,14 +55,15 @@ public class ContractRequestHandler implements ResultHandler {
|
||||
@Override
|
||||
public void handle(MvcResult result) throws Exception {
|
||||
MockHttpServletRequest request = result.getRequest();
|
||||
Map<String,Object> configuration = getConfiguration(result);
|
||||
String actual = StreamUtils.copyToString(request.getInputStream(),
|
||||
Charset.forName("UTF-8"));
|
||||
for (JsonPath jsonPath : jsonPaths.values()) {
|
||||
new JsonPathValue(jsonPath, actual).assertHasValue(Object.class, "an object");
|
||||
}
|
||||
snippet.setJsonPaths(jsonPaths.keySet());
|
||||
configuration.put("contract.jsonPaths", jsonPaths.keySet());
|
||||
if (contentType != null) {
|
||||
snippet.setContentType(contentType);
|
||||
configuration.put("contract.contentType", contentType);
|
||||
String resultType = request.getContentType();
|
||||
assertThat(resultType).isNotNull().as("no content type");
|
||||
assertThat(contentType.includes(MediaType.valueOf(resultType))).isTrue()
|
||||
@@ -70,6 +72,16 @@ public class ContractRequestHandler implements ResultHandler {
|
||||
MockMvcRestDocumentation.document(this.name).handle(result);
|
||||
}
|
||||
|
||||
private Map<String, Object> getConfiguration(MvcResult result) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = (Map<String, Object>) result.getRequest().getAttribute(ATTRIBUTE_NAME_CONFIGURATION);
|
||||
if (map==null) {
|
||||
map = new HashMap<>();
|
||||
result.getRequest().setAttribute(ATTRIBUTE_NAME_CONFIGURATION, map);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public ContractRequestHandler jsonPath(String expression, Object... args) {
|
||||
compile(expression, args);
|
||||
return this;
|
||||
|
||||
@@ -22,8 +22,8 @@ package org.springframework.cloud.contract.wiremock;
|
||||
*/
|
||||
public class RestDocsContracts {
|
||||
|
||||
public static ContractRequestHandler content(WireMockSnippet snippet) {
|
||||
return new ContractRequestHandler(snippet);
|
||||
public static ContractRequestHandler verify() {
|
||||
return new ContractRequestHandler();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportAware;
|
||||
@@ -49,7 +50,10 @@ public class WireMockConfiguration implements SmartLifecycle, ImportAware {
|
||||
|
||||
@Value("${wiremock.server.https-port:-1}")
|
||||
private int httpsPort = -1;
|
||||
|
||||
|
||||
@Autowired
|
||||
private DefaultListableBeanFactory beanFactory;
|
||||
|
||||
@Override
|
||||
public void setImportMetadata(AnnotationMetadata metadata) {
|
||||
AnnotationAttributes map = AnnotationAttributes.fromMap(
|
||||
@@ -77,6 +81,9 @@ public class WireMockConfiguration implements SmartLifecycle, ImportAware {
|
||||
this.options = factory;
|
||||
}
|
||||
server = new WireMockServer(options);
|
||||
if (!beanFactory.containsBean("wireMockServer")) {
|
||||
beanFactory.registerSingleton("wireMockServer", server);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer;
|
||||
|
||||
@@ -31,12 +30,7 @@ public class WireMockRestDocsConfiguration
|
||||
|
||||
@Override
|
||||
public void customize(MockMvcRestDocumentationConfigurer configurer) {
|
||||
configurer.snippets().withAdditionalDefaults(wireMockSnippet());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WireMockSnippet wireMockSnippet() {
|
||||
return new WireMockSnippet();
|
||||
configurer.snippets().withAdditionalDefaults(new WireMockSnippet());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ import java.io.IOException;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
@@ -63,6 +62,7 @@ public class WireMockSnippet implements Snippet {
|
||||
|
||||
@Override
|
||||
public void document(Operation operation) throws IOException {
|
||||
extractMatchers(operation);
|
||||
String json = Json
|
||||
.write(request(operation).willReturn(response(operation)).build());
|
||||
RestDocumentationContext context = (RestDocumentationContext) operation
|
||||
@@ -75,6 +75,14 @@ public class WireMockSnippet implements Snippet {
|
||||
}
|
||||
}
|
||||
|
||||
private void extractMatchers(Operation operation) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Set<String> jsonPaths = (Set<String>) operation.getAttributes()
|
||||
.get("contract.jsonPaths");
|
||||
this.jsonPaths = jsonPaths;
|
||||
contentType = (MediaType) operation.getAttributes().get("contract.contentType");
|
||||
}
|
||||
|
||||
private ResponseDefinitionBuilder response(Operation operation) {
|
||||
return aResponse().withHeaders(responseHeaders(operation))
|
||||
.withBody(operation.getResponse().getContentAsString());
|
||||
@@ -141,7 +149,6 @@ public class WireMockSnippet implements Snippet {
|
||||
.getHeaders();
|
||||
HttpHeaders result = new HttpHeaders();
|
||||
for (String name : headers.keySet()) {
|
||||
// TODO: whitelist the headers
|
||||
if (!headerBlackList.contains(name.toLowerCase())) {
|
||||
result = result.plus(new HttpHeader(name, headers.get(name)));
|
||||
}
|
||||
@@ -149,12 +156,4 @@ public class WireMockSnippet implements Snippet {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setJsonPaths(Collection<String> jsonPaths) {
|
||||
this.jsonPaths = new LinkedHashSet<>(jsonPaths);
|
||||
}
|
||||
|
||||
public void setContentType(MediaType contentType) {
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user