Skip to content

Commit 4cc8683

Browse files
author
khoutz182
committed
Add form fields to MockMvc Kotlin DSL
1 parent 7271358 commit 4cc8683

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -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

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ 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
@@ -230,6 +231,15 @@ class MockMvcExtensionsTests {
230231
assertThat(result.request.queryString).isEqualTo("foo=bar&foo=baz")
231232
}
232233

234+
@Test
235+
fun formField() {
236+
val result = mockMvc.post("/person") {
237+
formField("name", "foo")
238+
formField("someDouble", "1.23")
239+
}.andReturn()
240+
assertThat(result.request.contentType).startsWith(APPLICATION_FORM_URLENCODED_VALUE)
241+
assertThat(result.request.contentAsString).isEqualTo("name=foo&someDouble=1.23")
242+
}
233243

234244
@RestController
235245
private class PersonController {

0 commit comments

Comments
 (0)