Merge branch '1.1.x'
This commit is contained in:
@@ -16,14 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.contract.wiremock.restdocs;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.MappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
||||
import com.github.tomakehurst.wiremock.common.Json;
|
||||
@@ -31,7 +23,6 @@ import com.github.tomakehurst.wiremock.http.HttpHeader;
|
||||
import com.github.tomakehurst.wiremock.http.HttpHeaders;
|
||||
import com.github.tomakehurst.wiremock.matching.UrlPattern;
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.restdocs.RestDocumentationContext;
|
||||
import org.springframework.restdocs.operation.Operation;
|
||||
@@ -41,6 +32,15 @@ import org.springframework.restdocs.snippet.StandardWriterResolver;
|
||||
import org.springframework.restdocs.snippet.WriterResolver;
|
||||
import org.springframework.restdocs.templates.TemplateFormat;
|
||||
import org.springframework.util.PropertyPlaceholderHelper;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
|
||||
@@ -192,11 +192,13 @@ public class WireMockSnippet implements Snippet {
|
||||
builder.withRequestBody(matchingJsonPath(jsonPath));
|
||||
}
|
||||
}
|
||||
else if (this.hasJsonBodyRequestToMatch) {
|
||||
builder.withRequestBody(equalToJson(content));
|
||||
}
|
||||
else {
|
||||
builder.withRequestBody(equalTo(content));
|
||||
else if (!StringUtils.isEmpty(content)) {
|
||||
if (this.hasJsonBodyRequestToMatch) {
|
||||
builder.withRequestBody(equalToJson(content));
|
||||
}
|
||||
else {
|
||||
builder.withRequestBody(equalTo(content));
|
||||
}
|
||||
}
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -112,6 +112,24 @@ public class WireMockSnippetTests {
|
||||
.isEqualTo("{\"name\": \"12\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_handle_empty_request_body() throws IOException {
|
||||
given(this.operation.getName()).willReturn("foo");
|
||||
WireMockSnippet snippet = new WireMockSnippet();
|
||||
given(this.operation.getRequest()).willReturn(requestPostWithEmptyBody());
|
||||
|
||||
snippet.document(this.operation);
|
||||
|
||||
File stub = new File(this.outputFolder, "stubs/foo.json");
|
||||
assertThat(stub).exists();
|
||||
StubMapping stubMapping = WireMockStubMapping
|
||||
.buildFrom(new String(Files.readAllBytes(stub.toPath())));
|
||||
assertThat(stubMapping.getRequest().getBodyPatterns()).isNullOrEmpty();
|
||||
assertThat(stubMapping.getResponse().getStatus())
|
||||
.isEqualTo(HttpStatus.ACCEPTED.value());
|
||||
|
||||
}
|
||||
|
||||
private OperationResponse response() {
|
||||
return new OperationResponse() {
|
||||
|
||||
@@ -219,4 +237,45 @@ public class WireMockSnippetTests {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private OperationRequest requestPostWithEmptyBody() {
|
||||
return new OperationRequest() {
|
||||
@Override
|
||||
public byte[] getContent() {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentAsString() {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpHeaders getHeaders() {
|
||||
HttpHeaders httpHeaders = new HttpHeaders();
|
||||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
|
||||
return httpHeaders;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HttpMethod getMethod() {
|
||||
return HttpMethod.POST;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameters getParameters() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<OperationRequestPart> getParts() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return URI.create("http://foo/bar");
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user