Include query string parameters for non-GET requests in Parameters

Previously, the query string was only parsed when converted a GET
request. This commit updates WebTestClientRequestConverter to
parse the query string for all requests irrespective of the method.

Closes gh-472
This commit is contained in:
Andy Wilkinson
2018-02-15 12:19:58 +00:00
parent 15ea8d1898
commit bb630c17b5
2 changed files with 43 additions and 5 deletions

View File

@@ -30,7 +30,6 @@ import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.core.io.buffer.DefaultDataBuffer;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.codec.FormHttpMessageReader;
@@ -83,10 +82,8 @@ class WebTestClientRequestConverter implements RequestConverter<ExchangeResult>
}
private Parameters extractParameters(ExchangeResult result) {
if (result.getMethod() == HttpMethod.GET) {
return this.queryStringParser.parse(result.getUrl());
}
Parameters parameters = new Parameters();
parameters.addAll(this.queryStringParser.parse(result.getUrl()));
if (MediaType.APPLICATION_FORM_URLENCODED
.equals(result.getRequestHeaders().getContentType())) {
parameters.addAll(this.formDataReader

View File

@@ -131,7 +131,7 @@ public class WebTestClientRequestConverterTests {
}
@Test
public void postRequestWithParameters() throws Exception {
public void postRequestWithFormDataParameters() throws Exception {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.addAll("a", Arrays.asList("alpha", "apple"));
parameters.addAll("b", Arrays.asList("br&vo"));
@@ -151,6 +151,47 @@ public class WebTestClientRequestConverterTests {
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
}
@Test
public void postRequestWithQueryStringParameters() throws Exception {
ExchangeResult result = WebTestClient
.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
req.body(BodyExtractors.toFormData()).block();
return null;
})).configureClient().baseUrl("http://localhost").build().post()
.uri(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo"))
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(),
is(URI.create("http://localhost/foo?a=alpha&a=apple&b=br%26vo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParameters().size(), is(2));
assertThat(request.getParameters(),
hasEntry("a", Arrays.asList("alpha", "apple")));
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
}
@Test
public void postRequestWithQueryStringAndFormDataParameters() throws Exception {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.addAll("a", Arrays.asList("apple"));
ExchangeResult result = WebTestClient
.bindToRouterFunction(RouterFunctions.route(POST("/foo"), (req) -> {
req.body(BodyExtractors.toFormData()).block();
return null;
})).configureClient().baseUrl("http://localhost").build().post()
.uri(URI.create("http://localhost/foo?a=alpha&b=br%26vo"))
.body(BodyInserters.fromFormData(parameters)).exchange().expectBody()
.returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(),
is(URI.create("http://localhost/foo?a=alpha&b=br%26vo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
assertThat(request.getParameters().size(), is(2));
assertThat(request.getParameters(),
hasEntry("a", Arrays.asList("alpha", "apple")));
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
}
@Test
public void postRequestWithNoContentType() throws Exception {
ExchangeResult result = WebTestClient