Skip to content

Commit 0c33228

Browse files
committed
Add MockMvc Kotlin DSL
This commit introduces a `MockMvc` Kotlin DSL via a set of extensions like `MockMvc.get` or `MockMvc.request` that provide a Kotlin idiomatic and easily discoverable API while staying close to the original Java API design. Closes gh-1951
1 parent b7d60b9 commit 0c33228

File tree

8 files changed

+911
-0
lines changed

8 files changed

+911
-0
lines changed
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet
18+
19+
import org.springframework.http.HttpHeaders
20+
import org.springframework.http.MediaType
21+
import org.springframework.mock.web.MockHttpSession
22+
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder
23+
import org.springframework.test.web.servlet.request.RequestPostProcessor
24+
import org.springframework.util.MultiValueMap
25+
import java.security.Principal
26+
import java.util.*
27+
import javax.servlet.http.Cookie
28+
29+
/**
30+
* Provide a [MockHttpServletRequestBuilder] Kotlin DSL in order to be able to write idiomatic Kotlin code.
31+
*
32+
* @see MockMvc.get
33+
* @see MockMvc.post
34+
* @see MockMvc.put
35+
* @see MockMvc.patch
36+
* @see MockMvc.delete
37+
* @see MockMvc.options
38+
* @see MockMvc.request
39+
* @author Sebastien Deleuze
40+
* @since 5.2
41+
*/
42+
open class MockHttpServletRequestDsl(private val builder: MockHttpServletRequestBuilder) {
43+
44+
/**
45+
* @see [MockHttpServletRequestBuilder.contextPath]
46+
*/
47+
var contextPath: String? = null
48+
49+
/**
50+
* @see [MockHttpServletRequestBuilder.servletPath]
51+
*/
52+
var servletPath: String? = null
53+
54+
/**
55+
* @see [MockHttpServletRequestBuilder.pathInfo]
56+
*/
57+
var pathInfo: String? = null
58+
59+
/**
60+
* @see [MockHttpServletRequestBuilder.secure]
61+
*/
62+
var secure: Boolean? = null
63+
64+
/**
65+
* @see [MockHttpServletRequestBuilder.characterEncoding]
66+
*/
67+
var characterEncoding: String? = null
68+
69+
/**
70+
* @see [MockHttpServletRequestBuilder.content]
71+
*/
72+
var content: Any? = null
73+
74+
/**
75+
* @see [MockHttpServletRequestBuilder.accept]
76+
*/
77+
var accept: MediaType? = null
78+
79+
/**
80+
* @see [MockHttpServletRequestBuilder.accept]
81+
*/
82+
fun accept(vararg mediaTypes: MediaType) {
83+
builder.accept(*mediaTypes)
84+
}
85+
86+
/**
87+
* @see [MockHttpServletRequestBuilder.contentType]
88+
*/
89+
var contentType: MediaType? = null
90+
91+
/**
92+
* @see [MockHttpServletRequestBuilder.headers]
93+
*/
94+
fun headers(headers: HttpHeaders.() -> Unit) {
95+
builder.headers(HttpHeaders().apply(headers))
96+
}
97+
98+
/**
99+
* @see [MockHttpServletRequestBuilder.header]
100+
*/
101+
fun header(name: String, vararg values: Any) {
102+
builder.header(name, *values)
103+
}
104+
105+
/**
106+
* @see [MockHttpServletRequestBuilder.param]
107+
*/
108+
fun param(name: String, vararg values: String) {
109+
builder.param(name, *values)
110+
}
111+
112+
/**
113+
* @see [MockHttpServletRequestBuilder.params]
114+
*/
115+
var params: MultiValueMap<String, String>? = null
116+
117+
/**
118+
* @see [MockHttpServletRequestBuilder.cookie]
119+
*/
120+
fun cookie(vararg cookies: Cookie) {
121+
builder.cookie(*cookies)
122+
}
123+
124+
/**
125+
* @see [MockHttpServletRequestBuilder.locale]
126+
*/
127+
fun locale(vararg locales: Locale) {
128+
builder.locale(*locales)
129+
}
130+
131+
/**
132+
* @see [MockHttpServletRequestBuilder.requestAttr]
133+
*/
134+
fun requestAttr(name: String, value: Any) {
135+
builder.requestAttr(name, value)
136+
}
137+
138+
/**
139+
* @see [MockHttpServletRequestBuilder.sessionAttr]
140+
*/
141+
fun sessionAttr(name: String, value: Any) {
142+
builder.sessionAttr(name, value)
143+
}
144+
145+
/**
146+
* @see [MockHttpServletRequestBuilder.sessionAttrs]
147+
*/
148+
var sessionAttrs: Map<String, Any>? = null
149+
150+
/**
151+
* @see [MockHttpServletRequestBuilder.flashAttr]
152+
*/
153+
fun flashAttr(name: String, value: Any) {
154+
builder.flashAttr(name, value)
155+
}
156+
157+
/**
158+
* @see [MockHttpServletRequestBuilder.flashAttrs]
159+
*/
160+
var flashAttrs: Map<String, Any>? = null
161+
162+
/**
163+
* @see [MockHttpServletRequestBuilder.session]
164+
*/
165+
var session: MockHttpSession? = null
166+
167+
/**
168+
* @see [MockHttpServletRequestBuilder.principal]
169+
*/
170+
var principal: Principal? = null
171+
172+
/**
173+
* @see [MockHttpServletRequestBuilder.with]
174+
*/
175+
fun with(processor: RequestPostProcessor) {
176+
builder.with(processor)
177+
}
178+
179+
/**
180+
* @see [MockHttpServletRequestBuilder.merge]
181+
*/
182+
fun merge(parent: MockHttpServletRequestBuilder?) {
183+
builder.merge(parent)
184+
}
185+
186+
internal fun perform(mockMvc: MockMvc): ResultActionsDsl {
187+
contextPath?.also { builder.contextPath(contextPath!!) }
188+
servletPath?.also { builder.servletPath(servletPath!!) }
189+
pathInfo?.also { builder.pathInfo(pathInfo) }
190+
secure?.also { builder.secure(secure!!) }
191+
characterEncoding?.also { builder.characterEncoding(characterEncoding!!) }
192+
content?.also {
193+
when (content) {
194+
is String -> builder.content(content as String)
195+
is ByteArray -> builder.content(content as ByteArray)
196+
else -> builder.content(content.toString())
197+
}
198+
}
199+
accept?.also { builder.accept(accept!!) }
200+
contentType?.also { builder.contentType(contentType!!) }
201+
params?.also { builder.params(params!!) }
202+
sessionAttrs?.also { builder.sessionAttrs(sessionAttrs!!) }
203+
flashAttrs?.also { builder.flashAttrs(flashAttrs!!) }
204+
session?.also { builder.session(session!!) }
205+
principal?.also { builder.principal(principal!!) }
206+
return ResultActionsDsl(mockMvc.perform(builder))
207+
}
208+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.web.servlet
18+
19+
import org.springframework.mock.web.MockMultipartFile
20+
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder
21+
import javax.servlet.http.Part
22+
23+
/**
24+
* Provide a [MockMultipartHttpServletRequestBuilder] Kotlin DSL in order to be able to write idiomatic Kotlin code.
25+
*
26+
* @see MockMvc.multipart
27+
* @author Sebastien Deleuze
28+
* @since 5.2
29+
*/
30+
class MockMultipartHttpServletRequestDsl(private val builder: MockMultipartHttpServletRequestBuilder) : MockHttpServletRequestDsl(builder) {
31+
32+
/**
33+
* @see [MockMultipartHttpServletRequestBuilder.file]
34+
*/
35+
fun file(name: String, content: ByteArray) = builder.file(name, content)
36+
37+
/**
38+
* @see [MockMultipartHttpServletRequestBuilder.file]
39+
*/
40+
fun file(file: MockMultipartFile) = builder.file(file)
41+
42+
/**
43+
* @see [MockMultipartHttpServletRequestBuilder.part]
44+
*/
45+
fun part(vararg parts: Part) = builder.part(*parts)
46+
}

0 commit comments

Comments
 (0)