From dc0ebefc56cf507bfc3c13bc7383156b78366aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Fri, 22 Nov 2019 15:03:17 +0100 Subject: [PATCH] Add support for asyncDispatch to MockMvc Kotlin DSL Closes gh-23758 --- .../web/servlet/MockHttpServletRequestDsl.kt | 2 +- .../test/web/servlet/ResultActionsDsl.kt | 17 ++++++++++++++++- .../test/web/servlet/MockMvcExtensionsTests.kt | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt index 643a32882cbf..7226d319b750 100644 --- a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt +++ b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/MockHttpServletRequestDsl.kt @@ -204,6 +204,6 @@ open class MockHttpServletRequestDsl(private val builder: MockHttpServletRequest flashAttrs?.also { builder.flashAttrs(flashAttrs!!) } session?.also { builder.session(session!!) } principal?.also { builder.principal(principal!!) } - return ResultActionsDsl(mockMvc.perform(builder)) + return ResultActionsDsl(mockMvc.perform(builder), mockMvc) } } diff --git a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt index 43c639742a20..5c54df36d327 100644 --- a/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt +++ b/spring-test/src/main/kotlin/org/springframework/test/web/servlet/ResultActionsDsl.kt @@ -1,12 +1,14 @@ package org.springframework.test.web.servlet +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders + /** * Provide a [ResultActions] Kotlin DSL in order to be able to write idiomatic Kotlin code. * * @author Sebastien Deleuze * @since 5.2 */ -class ResultActionsDsl(private val actions: ResultActions) { +class ResultActionsDsl(private val actions: ResultActions, private val mockMvc: MockMvc) { /** * Provide access to [MockMvcResultMatchersDsl] Kotlin DSL. @@ -26,6 +28,19 @@ class ResultActionsDsl(private val actions: ResultActions) { return this } + /** + * Enable asynchronous dispatching. + * @see MockMvcRequestBuilders.asyncDispatch + * @since 5.2.2 + */ + fun asyncDispatch(): ResultActionsDsl { + return andExpect { + request { asyncStarted() } + }.andReturn().let { + ResultActionsDsl(mockMvc.perform(MockMvcRequestBuilders.asyncDispatch(it)), mockMvc) + } + } + /** * @see ResultActions.andReturn */ diff --git a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt index 2ac5accb3af7..9729c10ec967 100644 --- a/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt +++ b/spring-test/src/test/kotlin/org/springframework/test/web/servlet/MockMvcExtensionsTests.kt @@ -25,6 +25,7 @@ import org.springframework.http.MediaType.* import org.springframework.test.web.Person import org.springframework.test.web.servlet.setup.MockMvcBuilders import org.springframework.web.bind.annotation.* +import reactor.core.publisher.Mono import java.security.Principal import java.util.* @@ -153,6 +154,13 @@ class MockMvcExtensionsTests { } } + @Test + fun asyncDispatch() { + mockMvc.get("/async").asyncDispatch().andExpect { + status { isOk } + } + } + @RestController private class PersonController { @@ -166,5 +174,10 @@ class MockMvcExtensionsTests { @PostMapping("/person") @ResponseStatus(HttpStatus.CREATED) fun post(@RequestBody person: Person) {} + + @GetMapping("/async") + fun getAsync(): Mono { + return Mono.just(Person("foo")) + } } }