|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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 | + * https://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.result |
| 18 | + |
| 19 | +import io.mockk.mockk |
| 20 | +import org.hamcrest.text.CharSequenceLength.* |
| 21 | +import org.junit.jupiter.api.Test |
| 22 | +import org.springframework.mock.web.MockHttpServletRequest |
| 23 | +import org.springframework.mock.web.MockHttpServletResponse |
| 24 | +import org.springframework.test.web.servlet.* |
| 25 | +import org.springframework.web.servlet.FlashMap |
| 26 | +import org.springframework.web.servlet.ModelAndView |
| 27 | +import java.nio.charset.StandardCharsets |
| 28 | + |
| 29 | +/** |
| 30 | + * Tests for [ContentResultMatchersDsl]. |
| 31 | + * |
| 32 | + * @author Dmitry Sulman |
| 33 | + */ |
| 34 | +class ContentResultMatchersDslTests { |
| 35 | + |
| 36 | + val mockMvc = mockk<MockMvc>() |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `ContentResultMatchersDsl#string accepts Matcher parameterized with String supertype`() { |
| 40 | + getStubResultActionsDsl("some string") |
| 41 | + .andExpect { content { string(hasLength(11)) } } |
| 42 | + } |
| 43 | + |
| 44 | + private fun getStubResultActionsDsl(content: String): ResultActionsDsl { |
| 45 | + val resultActions = object : ResultActions { |
| 46 | + override fun andExpect(matcher: ResultMatcher): ResultActions { |
| 47 | + matcher.match(getStubMvcResult(content)) |
| 48 | + return this |
| 49 | + } |
| 50 | + |
| 51 | + override fun andDo(handler: ResultHandler): ResultActions { |
| 52 | + throw UnsupportedOperationException() |
| 53 | + } |
| 54 | + |
| 55 | + override fun andReturn(): MvcResult { |
| 56 | + throw UnsupportedOperationException() |
| 57 | + } |
| 58 | + |
| 59 | + } |
| 60 | + return ResultActionsDsl(resultActions, mockMvc) |
| 61 | + } |
| 62 | + |
| 63 | + private fun getStubMvcResult(content: String): StubMvcResult { |
| 64 | + val response = MockHttpServletResponse() |
| 65 | + response.outputStream.write(content.toByteArray(StandardCharsets.UTF_8)) |
| 66 | + return StubMvcResult(MockHttpServletRequest(), Any(), emptyArray(), Exception(), ModelAndView(), FlashMap(), response) |
| 67 | + } |
| 68 | +} |
0 commit comments