Add params and remoteAddress to request builder

This commit adds methods for adding/changing parameters and remote
address in the WebMvc.fn request builder.

Closes gh-25776
This commit is contained in:
Arjen Poutsma
2020-10-07 10:29:36 +02:00
parent 9990bd2ea8
commit b2d8180f82
3 changed files with 99 additions and 19 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.web.servlet.function;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;
@@ -29,6 +30,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
import org.springframework.web.testfixture.servlet.MockCookie;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
@@ -46,30 +48,45 @@ class DefaultServerRequestBuilderTests {
void from() throws ServletException, IOException {
MockHttpServletRequest request = PathPatternsTestUtils.initRequest("POST", "https://example.com", true);
request.addHeader("foo", "bar");
request.setCookies(new MockCookie("foo", "bar"));
request.setAttribute("foo", "bar");
request.addParameter("foo", "bar");
request.setRemoteHost("127.0.0.1");
request.setRemotePort(80);
ServerRequest other = ServerRequest.create(request, messageConverters);
ServerRequest result = ServerRequest.from(other)
.method(HttpMethod.HEAD)
.header("foo", "bar")
.headers(httpHeaders -> httpHeaders.set("baz", "qux"))
.cookie("foo", "bar")
.cookies(cookies -> cookies.set("baz", new Cookie("baz", "qux")))
.attribute("foo", "bar")
.attributes(attributes -> attributes.put("baz", "qux"))
.header("baz", "qux")
.headers(httpHeaders -> httpHeaders.set("quux", "quuz"))
.cookie("baz", "qux")
.cookies(cookies -> cookies.set("quux", new Cookie("quux", "quuz")))
.attribute("baz", "qux")
.attributes(attributes -> attributes.put("quux", "quuz"))
.param("baz", "qux")
.params(params -> params.set("quux", "quuz"))
.body("baz")
.build();
assertThat(result.method()).isEqualTo(HttpMethod.HEAD);
assertThat(result.headers().asHttpHeaders().size()).isEqualTo(2);
assertThat(result.headers().asHttpHeaders().getFirst("foo")).isEqualTo("bar");
assertThat(result.headers().asHttpHeaders().getFirst("baz")).isEqualTo("qux");
assertThat(result.cookies().size()).isEqualTo(2);
assertThat(result.headers().asHttpHeaders().getFirst("quux")).isEqualTo("quuz");
assertThat(result.cookies().getFirst("foo").getValue()).isEqualTo("bar");
assertThat(result.cookies().getFirst("baz").getValue()).isEqualTo("qux");
assertThat(result.attributes().size()).isEqualTo(other.attributes().size() + 2);
assertThat(result.cookies().getFirst("quux").getValue()).isEqualTo("quuz");
assertThat(result.attributes().get("foo")).isEqualTo("bar");
assertThat(result.attributes().get("baz")).isEqualTo("qux");
assertThat(result.attributes().get("quux")).isEqualTo("quuz");
assertThat(result.params().getFirst("foo")).isEqualTo("bar");
assertThat(result.params().getFirst("baz")).isEqualTo("qux");
assertThat(result.params().getFirst("quux")).isEqualTo("quuz");
assertThat(result.remoteAddress()).contains(new InetSocketAddress("127.0.0.1", 80));
String body = result.body(String.class);
assertThat(body).isEqualTo("baz");