Skip to content

Commit 3956a36

Browse files
committed
Merge branch '6.2.x'
2 parents 5c09435 + 056757b commit 3956a36

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -129,6 +129,18 @@ open class MockHttpServletRequestDsl(private val builder: AbstractMockHttpServle
129129
*/
130130
var queryParams: MultiValueMap<String, String>? = null
131131

132+
/**
133+
* @see [MockHttpServletRequestBuilder.formField]
134+
*/
135+
fun formField(name: String, vararg values: String) {
136+
builder.formField(name, *values)
137+
}
138+
139+
/**
140+
* @see [MockHttpServletRequestBuilder.formFields]
141+
*/
142+
var formFields: MultiValueMap<String, String>? = null
143+
132144
/**
133145
* @see [MockHttpServletRequestBuilder.cookie]
134146
*/
@@ -215,6 +227,7 @@ open class MockHttpServletRequestDsl(private val builder: AbstractMockHttpServle
215227
contentType?.also { builder.contentType(it) }
216228
params?.also { builder.params(it) }
217229
queryParams?.also { builder.queryParams(it) }
230+
formFields?.also { builder.formFields(it) }
218231
sessionAttrs?.also { builder.sessionAttrs(it) }
219232
flashAttrs?.also { builder.flashAttrs(it) }
220233
session?.also { builder.session(it) }

spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,13 +23,15 @@ import org.hamcrest.CoreMatchers
2323
import org.junit.jupiter.api.Test
2424
import org.springframework.http.HttpMethod
2525
import org.springframework.http.HttpStatus
26+
import org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE
2627
import org.springframework.http.MediaType.APPLICATION_ATOM_XML
2728
import org.springframework.http.MediaType.APPLICATION_JSON
2829
import org.springframework.http.MediaType.APPLICATION_XML
2930
import org.springframework.http.MediaType.TEXT_PLAIN
3031
import org.springframework.test.json.JsonCompareMode
3132
import org.springframework.test.web.Person
3233
import org.springframework.test.web.servlet.setup.MockMvcBuilders
34+
import org.springframework.util.LinkedMultiValueMap
3335
import org.springframework.web.bind.annotation.GetMapping
3436
import org.springframework.web.bind.annotation.PathVariable
3537
import org.springframework.web.bind.annotation.PostMapping
@@ -221,15 +223,62 @@ class MockMvcExtensionsTests {
221223
}
222224

223225
@Test
224-
fun queryParameter() {
226+
fun queryParam() {
225227
val result = mockMvc.get("/") {
226-
queryParam("foo", "bar")
227-
queryParam("foo", "baz")
228+
queryParam("foo", "bar", "baz")
228229
}.andReturn()
229230
assertThat(result.request.parameterMap["foo"]).containsExactly("bar", "baz")
230231
assertThat(result.request.queryString).isEqualTo("foo=bar&foo=baz")
231232
}
232233

234+
@Test
235+
fun queryParams() {
236+
val result = mockMvc.get("/") {
237+
queryParams = LinkedMultiValueMap(mapOf("foo" to listOf("bar", "baz")))
238+
}.andReturn()
239+
assertThat(result.request.parameterMap["foo"]).containsExactly("bar", "baz")
240+
assertThat(result.request.queryString).isEqualTo("foo=bar&foo=baz")
241+
}
242+
243+
@Test
244+
fun formField() {
245+
val result = mockMvc.post("/person") {
246+
formField("name", "foo", "bar")
247+
formField("someDouble", "1.23")
248+
}.andReturn()
249+
assertThat(result.request.contentType).startsWith(APPLICATION_FORM_URLENCODED_VALUE)
250+
assertThat(result.request.contentAsString).isEqualTo("name=foo&name=bar&someDouble=1.23")
251+
}
252+
253+
@Test
254+
fun formFields() {
255+
val result = mockMvc.post("/person") {
256+
formFields = LinkedMultiValueMap(mapOf("name" to listOf("foo", "bar"), "someDouble" to listOf("1.23")))
257+
}.andReturn()
258+
assertThat(result.request.contentType).startsWith(APPLICATION_FORM_URLENCODED_VALUE)
259+
assertThat(result.request.contentAsString).isEqualTo("name=foo&name=bar&someDouble=1.23")
260+
}
261+
262+
@Test
263+
fun sessionAttr() {
264+
val result = mockMvc.post("/person") {
265+
sessionAttr("name", "foo")
266+
sessionAttr("someDouble", 1.23)
267+
}.andReturn()
268+
val session = result.request.session!!
269+
assertThat(session.getAttribute("name")).isEqualTo("foo")
270+
assertThat(session.getAttribute("someDouble")).isEqualTo(1.23)
271+
}
272+
273+
@Test
274+
fun sessionAttrs() {
275+
val result = mockMvc.post("/person") {
276+
sessionAttrs = mapOf("name" to "foo", "someDouble" to 1.23)
277+
}.andReturn()
278+
val session = result.request.session!!
279+
assertThat(session.getAttribute("name")).isEqualTo("foo")
280+
assertThat(session.getAttribute("someDouble")).isEqualTo(1.23)
281+
}
233282

234283
@RestController
235284
private class PersonController {

0 commit comments

Comments
 (0)