1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -23,13 +23,15 @@ import org.hamcrest.CoreMatchers
23
23
import org.junit.jupiter.api.Test
24
24
import org.springframework.http.HttpMethod
25
25
import org.springframework.http.HttpStatus
26
+ import org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE
26
27
import org.springframework.http.MediaType.APPLICATION_ATOM_XML
27
28
import org.springframework.http.MediaType.APPLICATION_JSON
28
29
import org.springframework.http.MediaType.APPLICATION_XML
29
30
import org.springframework.http.MediaType.TEXT_PLAIN
30
31
import org.springframework.test.json.JsonCompareMode
31
32
import org.springframework.test.web.Person
32
33
import org.springframework.test.web.servlet.setup.MockMvcBuilders
34
+ import org.springframework.util.LinkedMultiValueMap
33
35
import org.springframework.web.bind.annotation.GetMapping
34
36
import org.springframework.web.bind.annotation.PathVariable
35
37
import org.springframework.web.bind.annotation.PostMapping
@@ -221,15 +223,62 @@ class MockMvcExtensionsTests {
221
223
}
222
224
223
225
@Test
224
- fun queryParameter () {
226
+ fun queryParam () {
225
227
val result = mockMvc.get(" /" ) {
226
- queryParam(" foo" , " bar" )
227
- queryParam(" foo" , " baz" )
228
+ queryParam(" foo" , " bar" , " baz" )
228
229
}.andReturn()
229
230
assertThat(result.request.parameterMap[" foo" ]).containsExactly(" bar" , " baz" )
230
231
assertThat(result.request.queryString).isEqualTo(" foo=bar&foo=baz" )
231
232
}
232
233
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
+ }
233
282
234
283
@RestController
235
284
private class PersonController {
0 commit comments