Browse Source

Add form fields support to MockMvc Kotlin DSL

See gh-34412

Signed-off-by: Kevin Houtz <kevin@khoutz.com>
pull/34656/head
Kevin Houtz 10 months ago committed by Sébastien Deleuze
parent
commit
79c5fec1be
  1. 15
      spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt
  2. 12
      spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt

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

@ -1,5 +1,5 @@ @@ -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 @@ -129,6 +129,18 @@ open class MockHttpServletRequestDsl(private val builder: AbstractMockHttpServle
*/
var queryParams: MultiValueMap<String, String>? = null
/**
* @see [MockHttpServletRequestBuilder.formField]
*/
fun formField(name: String, vararg values: String) {
builder.formField(name, *values)
}
/**
* @see [MockHttpServletRequestBuilder.formFields]
*/
var formFields: MultiValueMap<String, String>? = null
/**
* @see [MockHttpServletRequestBuilder.cookie]
*/
@ -215,6 +227,7 @@ open class MockHttpServletRequestDsl(private val builder: AbstractMockHttpServle @@ -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) }

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

@ -1,5 +1,5 @@ @@ -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 @@ -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 { @@ -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 {

Loading…
Cancel
Save