|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.web.filter;
|
18 | 18 |
|
19 |
| -import java.nio.charset.StandardCharsets; |
20 |
| - |
21 | 19 | import javax.servlet.http.HttpServletResponse;
|
22 | 20 |
|
23 | 21 | import org.junit.jupiter.api.Test;
|
24 | 22 |
|
25 |
| -import org.springframework.http.HttpHeaders; |
26 | 23 | import org.springframework.util.FileCopyUtils;
|
27 | 24 | import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
|
28 | 25 | import org.springframework.web.util.ContentCachingResponseWrapper;
|
29 | 26 |
|
| 27 | +import static java.nio.charset.StandardCharsets.UTF_8; |
30 | 28 | import static org.assertj.core.api.Assertions.assertThat;
|
| 29 | +import static org.springframework.http.HttpHeaders.CONTENT_LENGTH; |
| 30 | +import static org.springframework.http.HttpHeaders.TRANSFER_ENCODING; |
31 | 31 |
|
32 | 32 | /**
|
33 | 33 | * Unit tests for {@link ContentCachingResponseWrapper}.
|
| 34 | + * |
34 | 35 | * @author Rossen Stoyanchev
|
35 | 36 | */
|
36 | 37 | public class ContentCachingResponseWrapperTests {
|
37 | 38 |
|
38 | 39 | @Test
|
39 | 40 | void copyBodyToResponse() throws Exception {
|
40 |
| - byte[] responseBody = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 41 | + byte[] responseBody = "Hello World".getBytes(UTF_8); |
41 | 42 | MockHttpServletResponse response = new MockHttpServletResponse();
|
42 | 43 |
|
43 | 44 | ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
|
44 |
| - responseWrapper.setStatus(HttpServletResponse.SC_OK); |
| 45 | + responseWrapper.setStatus(HttpServletResponse.SC_CREATED); |
45 | 46 | FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
|
46 | 47 | responseWrapper.copyBodyToResponse();
|
47 | 48 |
|
48 |
| - assertThat(response.getStatus()).isEqualTo(200); |
49 |
| - assertThat(response.getContentLength() > 0).isTrue(); |
| 49 | + assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_CREATED); |
| 50 | + assertThat(response.getContentLength()).isGreaterThan(0); |
50 | 51 | assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
|
51 | 52 | }
|
52 | 53 |
|
53 | 54 | @Test
|
54 | 55 | void copyBodyToResponseWithTransferEncoding() throws Exception {
|
55 |
| - byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(StandardCharsets.UTF_8); |
| 56 | + byte[] responseBody = "6\r\nHello 5\r\nWorld0\r\n\r\n".getBytes(UTF_8); |
56 | 57 | MockHttpServletResponse response = new MockHttpServletResponse();
|
57 | 58 |
|
58 | 59 | ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
|
59 |
| - responseWrapper.setStatus(HttpServletResponse.SC_OK); |
60 |
| - responseWrapper.setHeader(HttpHeaders.TRANSFER_ENCODING, "chunked"); |
| 60 | + responseWrapper.setStatus(HttpServletResponse.SC_CREATED); |
| 61 | + responseWrapper.setHeader(TRANSFER_ENCODING, "chunked"); |
61 | 62 | FileCopyUtils.copy(responseBody, responseWrapper.getOutputStream());
|
62 | 63 | responseWrapper.copyBodyToResponse();
|
63 | 64 |
|
64 |
| - assertThat(response.getStatus()).isEqualTo(200); |
65 |
| - assertThat(response.getHeader(HttpHeaders.TRANSFER_ENCODING)).isEqualTo("chunked"); |
66 |
| - assertThat(response.getHeader(HttpHeaders.CONTENT_LENGTH)).isNull(); |
| 65 | + assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_CREATED); |
| 66 | + assertHeader(response, TRANSFER_ENCODING, "chunked"); |
| 67 | + assertHeader(response, CONTENT_LENGTH, null); |
67 | 68 | assertThat(response.getContentAsByteArray()).isEqualTo(responseBody);
|
68 | 69 | }
|
69 | 70 |
|
| 71 | + private void assertHeader(HttpServletResponse response, String header, String value) { |
| 72 | + if (value == null) { |
| 73 | + assertThat(response.containsHeader(header)).as(header).isFalse(); |
| 74 | + assertThat(response.getHeader(header)).as(header).isNull(); |
| 75 | + assertThat(response.getHeaders(header)).as(header).isEmpty(); |
| 76 | + } |
| 77 | + else { |
| 78 | + assertThat(response.containsHeader(header)).as(header).isTrue(); |
| 79 | + assertThat(response.getHeader(header)).as(header).isEqualTo(value); |
| 80 | + assertThat(response.getHeaders(header)).as(header).containsExactly(value); |
| 81 | + } |
| 82 | + } |
| 83 | + |
70 | 84 | }
|
0 commit comments