Added support of query parameters for RestDocs for WireMock stubs (#1237)
* Added support of query parameters for RestDocs for Stubs fixes #112
This commit is contained in:
committed by
Marcin Grzejszczak
parent
9450bb3a73
commit
a2845713a1
@@ -57,7 +57,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.patch;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.post;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.put;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.trace;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
|
||||
|
||||
/**
|
||||
* Represents a snippet for a WireMock stub.
|
||||
@@ -236,7 +236,7 @@ public class WireMockSnippet implements Snippet {
|
||||
}
|
||||
|
||||
private UrlPattern requestPattern(Operation operation) {
|
||||
return urlEqualTo(operation.getRequest().getUri().getPath());
|
||||
return urlPathEqualTo(operation.getRequest().getUri().getPath());
|
||||
}
|
||||
|
||||
private HttpHeaders responseHeaders(Operation operation) {
|
||||
|
||||
@@ -28,7 +28,9 @@ import java.util.Map;
|
||||
|
||||
import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern;
|
||||
import com.github.tomakehurst.wiremock.matching.EqualToXmlPattern;
|
||||
import com.github.tomakehurst.wiremock.matching.MultiValuePattern;
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -48,6 +50,7 @@ import org.springframework.restdocs.operation.OperationResponse;
|
||||
import org.springframework.restdocs.operation.Parameters;
|
||||
import org.springframework.restdocs.operation.RequestCookie;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
@@ -172,6 +175,24 @@ public class WireMockSnippetTests {
|
||||
WireMockStubMapping.buildFrom(new String(Files.readAllBytes(stub.toPath())));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_accept_query_params() throws IOException {
|
||||
this.operation = operation(requestGetWithQueryParam(), response(),
|
||||
this.context);
|
||||
WireMockSnippet snippet = new WireMockSnippet();
|
||||
|
||||
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().getUrlPath()).isEqualTo("/bar");
|
||||
assertThat(stubMapping.getRequest()
|
||||
.getQueryParameters())
|
||||
.containsOnly(Assertions.entry("myParam", MultiValuePattern.of(equalTo(("myValue")))));
|
||||
}
|
||||
|
||||
private Operation operation(OperationRequest request, OperationResponse response,
|
||||
RestDocumentationContext context) {
|
||||
return operation("foo", request, response, context);
|
||||
@@ -468,4 +489,51 @@ public class WireMockSnippetTests {
|
||||
};
|
||||
}
|
||||
|
||||
private OperationRequest requestGetWithQueryParam() {
|
||||
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.GET;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parameters getParameters() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<OperationRequestPart> getParts() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return URI.create("https://foo/bar?myParam=myValue");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<RequestCookie> getCookies() {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user