Access WebTestClient request Content-Type defensively

Closes gh-479
This commit is contained in:
Andy Wilkinson
2018-02-15 11:47:23 +00:00
parent 712b5fd9f7
commit 15ea8d1898
2 changed files with 17 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -87,8 +87,8 @@ class WebTestClientRequestConverter implements RequestConverter<ExchangeResult>
return this.queryStringParser.parse(result.getUrl());
}
Parameters parameters = new Parameters();
if (result.getRequestHeaders().getContentType()
.equals(MediaType.APPLICATION_FORM_URLENCODED)) {
if (MediaType.APPLICATION_FORM_URLENCODED
.equals(result.getRequestHeaders().getContentType())) {
parameters.addAll(this.formDataReader
.readMono(FORM_DATA_TYPE,
new ExchangeResultReactiveHttpInputMessage(result), null)

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 the original author or authors.
* Copyright 2014-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -150,6 +151,18 @@ public class WebTestClientRequestConverterTests {
assertThat(request.getParameters(), hasEntry("b", Arrays.asList("br&vo")));
}
@Test
public void postRequestWithNoContentType() throws Exception {
ExchangeResult result = WebTestClient
.bindToRouterFunction(RouterFunctions.route(POST("/foo"),
(req) -> ServerResponse.ok().build()))
.configureClient().baseUrl("http://localhost").build().post().uri("/foo")
.exchange().expectBody().returnResult();
OperationRequest request = this.converter.convert(result);
assertThat(request.getUri(), is(URI.create("http://localhost/foo")));
assertThat(request.getMethod(), is(HttpMethod.POST));
}
@Test
public void multipartUpload() throws Exception {
MultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();