Added support for query parameters in rest docs; fixes gh-445
This commit is contained in:
@@ -26,6 +26,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.MappingBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
import com.github.tomakehurst.wiremock.common.Json;
|
||||
import com.github.tomakehurst.wiremock.http.HttpHeader;
|
||||
import com.github.tomakehurst.wiremock.http.HttpHeaders;
|
||||
@@ -133,7 +134,21 @@ public class WireMockSnippet implements Snippet {
|
||||
}
|
||||
|
||||
private MappingBuilder request(Operation operation) {
|
||||
return requestHeaders(requestBuilder(operation), operation);
|
||||
return queryParams(
|
||||
requestHeaders(requestBuilder(operation), operation)
|
||||
, operation);
|
||||
}
|
||||
|
||||
private MappingBuilder queryParams(MappingBuilder request, Operation operation) {
|
||||
String rawQuery = operation.getRequest().getUri().getRawQuery();
|
||||
if (StringUtils.isEmpty(rawQuery)) {
|
||||
return request;
|
||||
}
|
||||
for (String queryPair : rawQuery.split("&")) {
|
||||
String[] splitQueryPair = queryPair.split("=");
|
||||
request = request.withQueryParam(splitQueryPair[0], WireMock.equalTo(splitQueryPair[1]));
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
private MappingBuilder requestHeaders(MappingBuilder request, Operation operation) {
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
package org.springframework.cloud.contract.wiremock;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Map;
|
||||
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
import com.github.tomakehurst.wiremock.matching.MultiValuePattern;
|
||||
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
|
||||
import org.assertj.core.api.BDDAssertions;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@@ -15,11 +23,15 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import wiremock.org.eclipse.jetty.http.HttpStatus;
|
||||
|
||||
@@ -35,18 +47,40 @@ public class WiremockServerRestDocsApplicationTests {
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/resource"))
|
||||
.andExpect(MockMvcResultMatchers.content().string("Hello World"))
|
||||
.andExpect(content().string("Hello World"))
|
||||
.andDo(document("resource"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusIsMaintained() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/status"))
|
||||
.andExpect(MockMvcResultMatchers.content().string("Hello World"))
|
||||
.andExpect(MockMvcResultMatchers.status().is(HttpStatus.ACCEPTED_202))
|
||||
.andExpect(content().string("Hello World"))
|
||||
.andExpect(status().is(HttpStatus.ACCEPTED_202))
|
||||
.andDo(document("status"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryParamsAreFetchedFromStubs() throws Exception {
|
||||
this.mockMvc.perform(
|
||||
MockMvcRequestBuilders
|
||||
.get("/project_metadata/spring-framework?callback=a_function_name&foo=foo&bar=bar"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string("spring-framework a_function_name foo bar"))
|
||||
.andDo(document("query"));
|
||||
|
||||
File file = new File("target/snippets/stubs", "query.json");
|
||||
BDDAssertions.then(file).exists();
|
||||
StubMapping stubMapping = StubMapping.buildFrom(new String(Files.readAllBytes(file.toPath())));
|
||||
Map<String, MultiValuePattern> queryParameters = stubMapping.getRequest()
|
||||
.getQueryParameters();
|
||||
BDDAssertions.then(queryParameters.get("callback").getValuePattern())
|
||||
.isEqualTo(WireMock.equalTo("a_function_name"));
|
||||
BDDAssertions.then(queryParameters.get("foo").getValuePattern())
|
||||
.isEqualTo(WireMock.equalTo("foo"));
|
||||
BDDAssertions.then(queryParameters.get("bar").getValuePattern())
|
||||
.isEqualTo(WireMock.equalTo("bar"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@RestController
|
||||
protected static class TestConfiguration {
|
||||
@@ -63,6 +97,15 @@ public class WiremockServerRestDocsApplicationTests {
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED_202).body("Hello World");
|
||||
}
|
||||
|
||||
@ResponseBody
|
||||
@RequestMapping("/project_metadata/{projectId}")
|
||||
public ResponseEntity<String> query(@PathVariable("projectId") String projectId,
|
||||
@RequestParam("callback") String callback,
|
||||
@RequestParam("foo") String foo,
|
||||
@RequestParam("bar") String bar) {
|
||||
return ResponseEntity.ok().body(projectId + " " + callback + " " + foo + " " + bar);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user