Remove unnecessary host entry from Spring REST Docs contract generation

This commit is contained in:
Tomasz Kopczynski
2017-05-08 00:17:58 +02:00
committed by Tomasz Kopczynski
parent 4c4a18ed22
commit 3d23964dca
3 changed files with 46 additions and 9 deletions

View File

@@ -323,15 +323,13 @@ import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method 'POST'
url 'http://localhost:8080/foo'
url '/foo'
body('''
{"foo": 23 }
''')
headers {
header('''Accept''', '''application/json''')
header('''Content-Type''', '''application/json''')
header('''Host''', '''localhost:8080''')
header('''Content-Length''', '''12''')
}
}
response {

View File

@@ -5,6 +5,8 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -17,6 +19,7 @@ import org.springframework.restdocs.operation.OperationRequest;
import org.springframework.restdocs.operation.OperationResponse;
import org.springframework.restdocs.snippet.TemplatedSnippet;
import org.springframework.restdocs.templates.TemplateEngine;
import org.springframework.util.StringUtils;
/**
* A {@link org.springframework.restdocs.snippet.Snippet} that documents the Spring Cloud Contract Groovy DSL.
@@ -30,6 +33,7 @@ public class ContractDslSnippet extends TemplatedSnippet {
private static final String SNIPPET_NAME = "dsl-contract";
private Map<String, Object> model = new HashMap<>();
private Set<String> bannedHeaders = new HashSet<>(Arrays.asList(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH));
/**
* Creates a new {@code ContractDslSnippet} with no additional attributes.
@@ -53,7 +57,8 @@ public class ContractDslSnippet extends TemplatedSnippet {
return this.model;
}
@Override public void document(Operation operation) throws IOException {
@Override
public void document(Operation operation) throws IOException {
TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes().get(TemplateEngine.class.getName());
String renderedContract = templateEngine.compileTemplate("default-dsl-contract-only")
.render(createModelForContract(operation));
@@ -90,12 +95,30 @@ public class ContractDslSnippet extends TemplatedSnippet {
private void insertRequestModel(Operation operation, Map<String, Object> model) {
OperationRequest request = operation.getRequest();
model.put("request_method", request.getMethod());
model.put("request_url", request.getUri());
model.put("request_url", prepareRequestUrl(request.getUri()));
model.put("request_body_present", request.getContent().length > 0);
model.put("request_body", request.getContentAsString());
HttpHeaders headers = request.getHeaders();
Map<String, String> headers = request.getHeaders().toSingleValueMap();
filterHeaders(headers);
model.put("request_headers_present", !headers.isEmpty());
model.put("request_headers", headers.toSingleValueMap().entrySet());
model.put("request_headers", headers.entrySet());
}
private void filterHeaders(Map<String, String> headers) {
for (String header : this.bannedHeaders) {
if (headers.containsKey(header)) {
headers.remove(header);
}
}
}
private String prepareRequestUrl(URI uri) {
String path = uri.getRawPath();
String query = uri.getRawQuery();
if (StringUtils.hasText(query)) {
path = path + "?" + query;
}
return path;
}
private Map<String, Object> createModelForContract(Operation operation) {

View File

@@ -5,6 +5,8 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;
import org.junit.Before;
import org.junit.Rule;
@@ -14,8 +16,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.contract.spec.Contract;
import org.springframework.cloud.contract.spec.internal.Header;
import org.springframework.cloud.contract.verifier.util.ContractVerifierDslConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.restdocs.JUnitRestDocumentation;
import org.springframework.test.context.junit4.SpringRunner;
@@ -80,9 +84,12 @@ public class ContractDslSnippetTests {
String contract = readFromFile(file("/contracts/index.groovy"));
// try to parse the contract
Contract parsedContract = ContractVerifierDslConverter.convert(contract);
then(parsedContract.getRequest().getHeaders().getEntries()).isNotEmpty();
then(parsedContract.getRequest().getHeaders().getEntries()).isNotNull();
then(headerNames(parsedContract.getRequest().getHeaders().getEntries())).doesNotContain
(HttpHeaders.HOST, HttpHeaders.CONTENT_LENGTH);
then(parsedContract.getRequest().getMethod().getClientValue()).isNotNull();
then(parsedContract.getRequest().getUrl().getClientValue()).isNotNull();
then(parsedContract.getRequest().getUrl().getClientValue().toString()).startsWith("/");
then(parsedContract.getRequest().getBody().getClientValue()).isNotNull();
then(parsedContract.getResponse().getStatus().getClientValue()).isNotNull();
then(parsedContract.getResponse().getHeaders().getEntries()).isNotEmpty();
@@ -101,9 +108,10 @@ public class ContractDslSnippetTests {
String contract = readFromFile(file("/contracts/empty.groovy"));
// try to parse the contract
Contract parsedContract = ContractVerifierDslConverter.convert(contract);
then(parsedContract.getRequest().getHeaders().getEntries()).isNotEmpty();
then(parsedContract.getRequest().getHeaders()).isNull();
then(parsedContract.getRequest().getMethod().getClientValue()).isNotNull();
then(parsedContract.getRequest().getUrl().getClientValue()).isNotNull();
then(parsedContract.getRequest().getUrl().getClientValue().toString()).startsWith("/");
then(parsedContract.getRequest().getBody()).isNull();
then(parsedContract.getResponse().getStatus().getClientValue()).isNotNull();
then(parsedContract.getResponse().getHeaders()).isNull();
@@ -111,6 +119,14 @@ public class ContractDslSnippetTests {
then(parsedContract.getResponse().getMatchers()).isNull();
}
private Set<String> headerNames(Set<Header> headers) {
Set<String> names = new HashSet<>();
for (Header header : headers) {
names.add(header.getName());
}
return names;
}
private File file(String name) throws URISyntaxException {
return new File(OUTPUT, name);
}