From 79c5fec1be80cea17117aae7df51075b50233515 Mon Sep 17 00:00:00 2001 From: Kevin Houtz Date: Tue, 11 Feb 2025 17:58:38 -0600 Subject: [PATCH 1/2] Add form fields support to MockMvc Kotlin DSL See gh-34412 Signed-off-by: Kevin Houtz --- .../test/web/servlet/MockHttpServletRequestDsl.kt | 15 ++++++++++++++- .../test/web/servlet/MockMvcExtensionsTests.kt | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt index 0e42b641887..a85a3f3d774 100644 --- a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt +++ b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -129,6 +129,18 @@ open class MockHttpServletRequestDsl(private val builder: AbstractMockHttpServle */ var queryParams: MultiValueMap? = null + /** + * @see [MockHttpServletRequestBuilder.formField] + */ + fun formField(name: String, vararg values: String) { + builder.formField(name, *values) + } + + /** + * @see [MockHttpServletRequestBuilder.formFields] + */ + var formFields: MultiValueMap? = null + /** * @see [MockHttpServletRequestBuilder.cookie] */ @@ -215,6 +227,7 @@ open class MockHttpServletRequestDsl(private val builder: AbstractMockHttpServle contentType?.also { builder.contentType(it) } params?.also { builder.params(it) } queryParams?.also { builder.queryParams(it) } + formFields?.also { builder.formFields(it) } sessionAttrs?.also { builder.sessionAttrs(it) } flashAttrs?.also { builder.flashAttrs(it) } session?.also { builder.session(it) } diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index ff03bee71c8..2781d37b0a1 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -23,6 +23,7 @@ import org.hamcrest.CoreMatchers import org.junit.jupiter.api.Test import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus +import org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE import org.springframework.http.MediaType.APPLICATION_ATOM_XML import org.springframework.http.MediaType.APPLICATION_JSON import org.springframework.http.MediaType.APPLICATION_XML @@ -230,6 +231,15 @@ class MockMvcExtensionsTests { assertThat(result.request.queryString).isEqualTo("foo=bar&foo=baz") } + @Test + fun formField() { + val result = mockMvc.post("/person") { + formField("name", "foo") + formField("someDouble", "1.23") + }.andReturn() + assertThat(result.request.contentType).startsWith(APPLICATION_FORM_URLENCODED_VALUE) + assertThat(result.request.contentAsString).isEqualTo("name=foo&someDouble=1.23") + } @RestController private class PersonController { From 056757b493d36ee9bb7fbbabc439e39d99b647b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= <141109+sdeleuze@users.noreply.github.com> Date: Wed, 12 Feb 2025 11:17:09 +0100 Subject: [PATCH 2/2] Refine tests in MockMvcExtensionsTests Closes gh-34412 --- .../web/servlet/MockMvcExtensionsTests.kt | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index 2781d37b0a1..e9fb0a8c2c3 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -31,6 +31,7 @@ import org.springframework.http.MediaType.TEXT_PLAIN import org.springframework.test.json.JsonCompareMode import org.springframework.test.web.Person import org.springframework.test.web.servlet.setup.MockMvcBuilders +import org.springframework.util.LinkedMultiValueMap import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping @@ -222,10 +223,18 @@ class MockMvcExtensionsTests { } @Test - fun queryParameter() { + fun queryParam() { val result = mockMvc.get("/") { - queryParam("foo", "bar") - queryParam("foo", "baz") + queryParam("foo", "bar", "baz") + }.andReturn() + assertThat(result.request.parameterMap["foo"]).containsExactly("bar", "baz") + assertThat(result.request.queryString).isEqualTo("foo=bar&foo=baz") + } + + @Test + fun queryParams() { + val result = mockMvc.get("/") { + queryParams = LinkedMultiValueMap(mapOf("foo" to listOf("bar", "baz"))) }.andReturn() assertThat(result.request.parameterMap["foo"]).containsExactly("bar", "baz") assertThat(result.request.queryString).isEqualTo("foo=bar&foo=baz") @@ -234,11 +243,41 @@ class MockMvcExtensionsTests { @Test fun formField() { val result = mockMvc.post("/person") { - formField("name", "foo") + formField("name", "foo", "bar") formField("someDouble", "1.23") }.andReturn() assertThat(result.request.contentType).startsWith(APPLICATION_FORM_URLENCODED_VALUE) - assertThat(result.request.contentAsString).isEqualTo("name=foo&someDouble=1.23") + assertThat(result.request.contentAsString).isEqualTo("name=foo&name=bar&someDouble=1.23") + } + + @Test + fun formFields() { + val result = mockMvc.post("/person") { + formFields = LinkedMultiValueMap(mapOf("name" to listOf("foo", "bar"), "someDouble" to listOf("1.23"))) + }.andReturn() + assertThat(result.request.contentType).startsWith(APPLICATION_FORM_URLENCODED_VALUE) + assertThat(result.request.contentAsString).isEqualTo("name=foo&name=bar&someDouble=1.23") + } + + @Test + fun sessionAttr() { + val result = mockMvc.post("/person") { + sessionAttr("name", "foo") + sessionAttr("someDouble", 1.23) + }.andReturn() + val session = result.request.session!! + assertThat(session.getAttribute("name")).isEqualTo("foo") + assertThat(session.getAttribute("someDouble")).isEqualTo(1.23) + } + + @Test + fun sessionAttrs() { + val result = mockMvc.post("/person") { + sessionAttrs = mapOf("name" to "foo", "someDouble" to 1.23) + }.andReturn() + val session = result.request.session!! + assertThat(session.getAttribute("name")).isEqualTo("foo") + assertThat(session.getAttribute("someDouble")).isEqualTo(1.23) } @RestController