Search in sources :

Example 1 with MockCookie

use of org.springframework.web.testfixture.servlet.MockCookie in project spring-framework by spring-projects.

the class DefaultServerRequestBuilderTests method from.

@Test
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("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().getFirst("foo")).isEqualTo("bar");
    assertThat(result.headers().asHttpHeaders().getFirst("baz")).isEqualTo("qux");
    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.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");
}
Also used : MockCookie(org.springframework.web.testfixture.servlet.MockCookie) Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) InetSocketAddress(java.net.InetSocketAddress) MockCookie(org.springframework.web.testfixture.servlet.MockCookie) Test(org.junit.jupiter.api.Test)

Aggregations

Cookie (jakarta.servlet.http.Cookie)1 InetSocketAddress (java.net.InetSocketAddress)1 Test (org.junit.jupiter.api.Test)1 MockCookie (org.springframework.web.testfixture.servlet.MockCookie)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1