Skip to content

Add form fields to MockMvc Kotlin DSL #34412

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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]
*/
Expand Down Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down