|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 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 | +package org.springframework.web.filter; |
| 17 | + |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | + |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.http.HttpHeaders; |
| 25 | +import org.springframework.mock.web.test.MockHttpServletResponse; |
| 26 | +import org.springframework.util.FileCopyUtils; |
| 27 | +import org.springframework.web.util.ContentCachingResponseWrapper; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertArrayEquals; |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertNull; |
| 32 | +import static org.junit.Assert.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * Unit tests for {@link ContentCachingResponseWrapper}. |
| 36 | + * @author Rossen Stoyanchev |
| 37 | + */ |
| 38 | +public class ContentCachingResponseWrapperTests { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void copyBodyToResponse() throws Exception { |
| 42 | + byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 43 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 44 | + |
| 45 | + ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); |
| 46 | + responseWrapper.setStatus(HttpServletResponse.SC_OK); |
| 47 | + FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream()); |
| 48 | + responseWrapper.copyBodyToResponse(); |
| 49 | + |
| 50 | + assertEquals(200, response.getStatus()); |
| 51 | + assertTrue(response.getContentLength() > 0); |
| 52 | + assertArrayEquals(responseBody, response.getContentAsByteArray()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void copyBodyToResponseWithTransferEncoding() throws Exception { |
| 57 | + byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(StandardCharsets.UTF_8); |
| 58 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 59 | + |
| 60 | + ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response); |
| 61 | + responseWrapper.setStatus(HttpServletResponse.SC_OK); |
| 62 | + responseWrapper.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
| 63 | + FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream()); |
| 64 | + responseWrapper.copyBodyToResponse(); |
| 65 | + |
| 66 | + assertEquals(200, response.getStatus()); |
| 67 | + assertEquals("chunked", response.getHeader(HttpHeaders.TRANSFER_ENCODING)); |
| 68 | + assertNull(response.getHeader(HttpHeaders.CONTENT_LENGTH)); |
| 69 | + assertArrayEquals(responseBody, response.getContentAsByteArray()); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments