|
| 1 | +package org.springframework.restdocs.mockmvc |
| 2 | + |
| 3 | +import org.springframework.http.HttpMethod |
| 4 | +import org.springframework.test.web.servlet.MockHttpServletRequestDsl |
| 5 | +import org.springframework.test.web.servlet.MockMultipartHttpServletRequestDsl |
| 6 | +import org.springframework.test.web.servlet.MockMvc |
| 7 | +import org.springframework.test.web.servlet.ResultActionsDsl |
| 8 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders |
| 9 | +import java.net.URI |
| 10 | + |
| 11 | +private const val ATTRIBUTE_NAME_URL_TEMPLATE = "org.springframework.restdocs.urlTemplate" |
| 12 | + |
| 13 | +/** |
| 14 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 15 | + * The url template will be captured and made available for documentation. |
| 16 | + * |
| 17 | + * @see MockMvcRequestBuilders.get |
| 18 | + * @see org.springframework.test.web.servlet.get |
| 19 | + * @author He Jow Moon |
| 20 | + * @since 6.1.14 |
| 21 | + */ |
| 22 | +fun MockMvc.get(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 23 | + val requestBuilder = MockMvcRequestBuilders.get(urlTemplate, *vars) |
| 24 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 25 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 26 | + .perform(this) |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL. |
| 31 | + * |
| 32 | + * @see MockMvcRequestBuilders.get |
| 33 | + * @see org.springframework.test.web.servlet.get |
| 34 | + * @author He Jow Moon |
| 35 | + * @since 6.1.14 |
| 36 | + */ |
| 37 | +fun MockMvc.get(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 38 | + val requestBuilder = MockMvcRequestBuilders.get(uri) |
| 39 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 44 | + * The url template will be captured and made available for documentation. |
| 45 | + * |
| 46 | + * @see MockMvcRequestBuilders.post |
| 47 | + * @see org.springframework.test.web.servlet.post |
| 48 | + * @author He Jow Moon |
| 49 | + * @since 6.1.14 |
| 50 | + */ |
| 51 | +fun MockMvc.post(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 52 | + val requestBuilder = MockMvcRequestBuilders.post(urlTemplate, *vars) |
| 53 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 54 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 55 | + .perform(this) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 60 | + * |
| 61 | + * @see MockMvcRequestBuilders.post |
| 62 | + * @see org.springframework.test.web.servlet.post |
| 63 | + * @author He Jow Moon |
| 64 | + * @since 6.1.14 |
| 65 | + */ |
| 66 | +fun MockMvc.post(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 67 | + val requestBuilder = MockMvcRequestBuilders.post(uri) |
| 68 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 73 | + * The url template will be captured and made available for documentation. |
| 74 | + * |
| 75 | + * @see MockMvcRequestBuilders.put |
| 76 | + * @see org.springframework.test.web.servlet.put |
| 77 | + * @author He Jow Moon |
| 78 | + * @since 6.1.14 |
| 79 | + */ |
| 80 | +fun MockMvc.put(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 81 | + val requestBuilder = MockMvcRequestBuilders.put(urlTemplate, *vars) |
| 82 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 83 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 84 | + .perform(this) |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 89 | + * |
| 90 | + * @see MockMvcRequestBuilders.put |
| 91 | + * @see org.springframework.test.web.servlet.put |
| 92 | + * @author He Jow Moon |
| 93 | + * @since 6.1.14 |
| 94 | + */ |
| 95 | +fun MockMvc.put(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 96 | + val requestBuilder = MockMvcRequestBuilders.put(uri) |
| 97 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 102 | + * The url template will be captured and made available for documentation. |
| 103 | + * |
| 104 | + * @see MockMvcRequestBuilders.patch |
| 105 | + * @see org.springframework.test.web.servlet.patch |
| 106 | + * @author He Jow Moon |
| 107 | + * @since 6.1.14 |
| 108 | + */ |
| 109 | +fun MockMvc.patch(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 110 | + val requestBuilder = MockMvcRequestBuilders.patch(urlTemplate, *vars) |
| 111 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 112 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 113 | + .perform(this) |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 118 | + * |
| 119 | + * @see MockMvcRequestBuilders.patch |
| 120 | + * @see org.springframework.test.web.servlet.patch |
| 121 | + * @author He Jow Moon |
| 122 | + * @since 6.1.14 |
| 123 | + */ |
| 124 | +fun MockMvc.patch(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 125 | + val requestBuilder = MockMvcRequestBuilders.patch(uri) |
| 126 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 131 | + * The url template will be captured and made available for documentation. |
| 132 | + * |
| 133 | + * @see MockMvcRequestBuilders.delete |
| 134 | + * @see org.springframework.test.web.servlet.delete |
| 135 | + * @author He Jow Moon |
| 136 | + * @since 6.1.14 |
| 137 | + */ |
| 138 | +fun MockMvc.delete(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 139 | + val requestBuilder = MockMvcRequestBuilders.delete(urlTemplate, *vars) |
| 140 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 141 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 142 | + .perform(this) |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 147 | + * |
| 148 | + * @see MockMvcRequestBuilders.delete |
| 149 | + * @see org.springframework.test.web.servlet.delete |
| 150 | + * @author He Jow Moon |
| 151 | + * @since 6.1.14 |
| 152 | + */ |
| 153 | +fun MockMvc.delete(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 154 | + val requestBuilder = MockMvcRequestBuilders.delete(uri) |
| 155 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 160 | + * The url template will be captured and made available for documentation. |
| 161 | + * |
| 162 | + * @see MockMvcRequestBuilders.options |
| 163 | + * @see org.springframework.test.web.servlet.options |
| 164 | + * @author He Jow Moon |
| 165 | + * @since 6.1.14 |
| 166 | + */ |
| 167 | +fun MockMvc.options(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 168 | + val requestBuilder = MockMvcRequestBuilders.options(urlTemplate, *vars) |
| 169 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 170 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 171 | + .perform(this) |
| 172 | +} |
| 173 | + |
| 174 | +/** |
| 175 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 176 | + * |
| 177 | + * @see MockMvcRequestBuilders.options |
| 178 | + * @see org.springframework.test.web.servlet.options |
| 179 | + * @author He Jow Moon |
| 180 | + * @since 6.1.14 |
| 181 | + */ |
| 182 | +fun MockMvc.options(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 183 | + val requestBuilder = MockMvcRequestBuilders.options(uri) |
| 184 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 189 | + * The url template will be captured and made available for documentation. |
| 190 | + * |
| 191 | + * @see MockMvcRequestBuilders.head |
| 192 | + * @see org.springframework.test.web.servlet.head |
| 193 | + * @author He Jow Moon |
| 194 | + * @since 6.1.14 |
| 195 | + */ |
| 196 | +fun MockMvc.head(urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 197 | + val requestBuilder = MockMvcRequestBuilders.head(urlTemplate, *vars) |
| 198 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 199 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 200 | + .perform(this) |
| 201 | +} |
| 202 | + |
| 203 | +/** |
| 204 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 205 | + * |
| 206 | + * @see MockMvcRequestBuilders.head |
| 207 | + * @see org.springframework.test.web.servlet.head |
| 208 | + * @author He Jow Moon |
| 209 | + * @since 6.1.14 |
| 210 | + */ |
| 211 | +fun MockMvc.head(uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 212 | + val requestBuilder = MockMvcRequestBuilders.head(uri) |
| 213 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 214 | +} |
| 215 | + |
| 216 | +/** |
| 217 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 218 | + * The url template will be captured and made available for documentation. |
| 219 | + * |
| 220 | + * @see MockMvcRequestBuilders.request |
| 221 | + * @see org.springframework.test.web.servlet.request |
| 222 | + * @author He Jow Moon |
| 223 | + * @since 6.1.14 |
| 224 | + */ |
| 225 | +fun MockMvc.request(method: HttpMethod, urlTemplate: String, vararg vars: Any?, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 226 | + val requestBuilder = MockMvcRequestBuilders.request(method, urlTemplate, *vars) |
| 227 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 228 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 229 | + .perform(this) |
| 230 | +} |
| 231 | + |
| 232 | +/** |
| 233 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 234 | + * |
| 235 | + * @see MockMvcRequestBuilders.request |
| 236 | + * @see org.springframework.test.web.servlet.request |
| 237 | + * @author He Jow Moon |
| 238 | + * @since 6.1.14 |
| 239 | + */ |
| 240 | +fun MockMvc.request(method: HttpMethod, uri: URI, dsl: MockHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 241 | + val requestBuilder = MockMvcRequestBuilders.request(method, uri) |
| 242 | + return MockHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 243 | +} |
| 244 | + |
| 245 | +/** |
| 246 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 247 | + * The url template will be captured and made available for documentation. |
| 248 | + * |
| 249 | + * @see MockMvcRequestBuilders.multipart |
| 250 | + * @see org.springframework.test.web.servlet.multipart |
| 251 | + * @author He Jow Moon |
| 252 | + * @since 6.1.14 |
| 253 | + */ |
| 254 | +fun MockMvc.multipart(urlTemplate: String, vararg vars: Any?, dsl: MockMultipartHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 255 | + val requestBuilder = MockMvcRequestBuilders.multipart(urlTemplate, *vars) |
| 256 | + return MockMultipartHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 257 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 258 | + .perform(this) |
| 259 | +} |
| 260 | + |
| 261 | +/** |
| 262 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 263 | + * The url template will be captured and made available for documentation. |
| 264 | + * |
| 265 | + * @see MockMvcRequestBuilders.multipart |
| 266 | + * @see org.springframework.test.web.servlet.multipart |
| 267 | + * @author He Jow Moon |
| 268 | + * @since 6.1.14 |
| 269 | + */ |
| 270 | +fun MockMvc.multipart(httpMethod: HttpMethod, urlTemplate: String, vararg vars: Any?, dsl: MockMultipartHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 271 | + val requestBuilder = MockMvcRequestBuilders.multipart(httpMethod, urlTemplate, *vars) |
| 272 | + return MockMultipartHttpServletRequestDsl(requestBuilder).apply(dsl) |
| 273 | + .apply { requestAttr(ATTRIBUTE_NAME_URL_TEMPLATE, urlTemplate) } |
| 274 | + .perform(this) |
| 275 | +} |
| 276 | + |
| 277 | +/** |
| 278 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 279 | + * |
| 280 | + * @see MockMvcRequestBuilders.multipart |
| 281 | + * @see org.springframework.test.web.servlet.multipart |
| 282 | + * @author He Jow Moon |
| 283 | + * @since 6.1.14 |
| 284 | + */ |
| 285 | +fun MockMvc.multipart(uri: URI, dsl: MockMultipartHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 286 | + val requestBuilder = MockMvcRequestBuilders.multipart(uri) |
| 287 | + return MockMultipartHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 288 | +} |
| 289 | + |
| 290 | +/** |
| 291 | + * [MockMvc] extension providing access to [MockHttpServletRequestDsl] Kotlin DSL and support RestDocs. |
| 292 | + * |
| 293 | + * @see MockMvcRequestBuilders.multipart |
| 294 | + * @see org.springframework.test.web.servlet.multipart |
| 295 | + * @author He Jow Moon |
| 296 | + * @since 6.1.14 |
| 297 | + */ |
| 298 | +fun MockMvc.multipart(httpMethod: HttpMethod, uri: URI, dsl: MockMultipartHttpServletRequestDsl.() -> Unit = {}): ResultActionsDsl { |
| 299 | + val requestBuilder = MockMvcRequestBuilders.multipart(httpMethod, uri) |
| 300 | + return MockMultipartHttpServletRequestDsl(requestBuilder).apply(dsl).perform(this) |
| 301 | +} |
0 commit comments