|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
19 | 19 | import java.io.Serializable;
|
20 | 20 | import java.lang.reflect.Method;
|
21 | 21 | import java.util.ArrayList;
|
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.List;
|
23 | 24 |
|
| 25 | +import javax.servlet.FilterChain; |
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | +import javax.servlet.http.HttpServletResponse; |
| 28 | + |
24 | 29 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
25 | 30 | import com.fasterxml.jackson.annotation.JsonTypeName;
|
26 | 31 | import org.junit.jupiter.api.BeforeEach;
|
27 | 32 | import org.junit.jupiter.api.Test;
|
28 | 33 |
|
29 | 34 | import org.springframework.core.MethodParameter;
|
30 | 35 | import org.springframework.http.HttpEntity;
|
| 36 | +import org.springframework.http.HttpHeaders; |
31 | 37 | import org.springframework.http.MediaType;
|
32 | 38 | import org.springframework.http.ResponseEntity;
|
33 | 39 | import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
|
42 | 48 | import org.springframework.web.bind.support.WebDataBinderFactory;
|
43 | 49 | import org.springframework.web.context.request.NativeWebRequest;
|
44 | 50 | import org.springframework.web.context.request.ServletWebRequest;
|
| 51 | +import org.springframework.web.filter.ShallowEtagHeaderFilter; |
45 | 52 | import org.springframework.web.method.HandlerMethod;
|
46 | 53 | import org.springframework.web.method.support.ModelAndViewContainer;
|
47 | 54 | import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
|
@@ -200,6 +207,41 @@ public void handleReturnValueCharSequence() throws Exception {
|
200 | 207 | assertThat(servletResponse.getContentAsString()).isEqualTo("Foo");
|
201 | 208 | }
|
202 | 209 |
|
| 210 | + @Test // SPR-13423 |
| 211 | + public void handleReturnValueWithETagAndETagFilter() throws Exception { |
| 212 | + |
| 213 | + String eTagValue = "\"deadb33f8badf00d\""; |
| 214 | + String content = "body"; |
| 215 | + |
| 216 | + Method method = getClass().getDeclaredMethod("handle"); |
| 217 | + MethodParameter returnType = new MethodParameter(method, -1); |
| 218 | + |
| 219 | + FilterChain chain = (req, res) -> { |
| 220 | + ResponseEntity<String> returnValue = ResponseEntity.ok().eTag(eTagValue).body(content); |
| 221 | + try { |
| 222 | + ServletWebRequest requestToUse = |
| 223 | + new ServletWebRequest((HttpServletRequest) req, (HttpServletResponse) res); |
| 224 | + |
| 225 | + new HttpEntityMethodProcessor(Collections.singletonList(new StringHttpMessageConverter())) |
| 226 | + .handleReturnValue(returnValue, returnType, mavContainer, requestToUse); |
| 227 | + |
| 228 | + assertThat(this.servletResponse.getContentAsString()) |
| 229 | + .as("Response body was cached? It should be written directly to the raw response") |
| 230 | + .isEqualTo(content); |
| 231 | + } |
| 232 | + catch (Exception ex) { |
| 233 | + throw new IllegalStateException(ex); |
| 234 | + } |
| 235 | + }; |
| 236 | + |
| 237 | + this.servletRequest.setMethod("GET"); |
| 238 | + new ShallowEtagHeaderFilter().doFilter(this.servletRequest, this.servletResponse, chain); |
| 239 | + |
| 240 | + assertThat(this.servletResponse.getStatus()).isEqualTo(200); |
| 241 | + assertThat(this.servletResponse.getHeader(HttpHeaders.ETAG)).isEqualTo(eTagValue); |
| 242 | + assertThat(this.servletResponse.getContentAsString()).isEqualTo(content); |
| 243 | + } |
| 244 | + |
203 | 245 |
|
204 | 246 | @SuppressWarnings("unused")
|
205 | 247 | private void handle(HttpEntity<List<SimpleBean>> arg1, HttpEntity<SimpleBean> arg2) {
|
|
0 commit comments