Browse Source

Polish

pull/1014/merge
Rossen Stoyanchev 9 years ago
parent
commit
6bfe0c050d
  1. 154
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

154
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/HttpEntityMethodProcessorMockTests.java

@ -20,8 +20,10 @@ import java.lang.reflect.Method;
import java.net.URI; import java.net.URI;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
@ -48,9 +50,18 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer; import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.*; import static org.junit.Assert.assertFalse;
import static org.springframework.web.servlet.HandlerMapping.*; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.isA;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.reset;
import static org.mockito.BDDMockito.verify;
import static org.springframework.web.servlet.HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE;
/** /**
* Test fixture for {@link HttpEntityMethodProcessor} delegating to a mock * Test fixture for {@link HttpEntityMethodProcessor} delegating to a mock
@ -96,11 +107,14 @@ public class HttpEntityMethodProcessorMockTests {
messageConverter = mock(HttpMessageConverter.class); messageConverter = mock(HttpMessageConverter.class);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
List<HttpMessageConverter<?>> converters = new ArrayList<>();
processor = new HttpEntityMethodProcessor(Collections.<HttpMessageConverter<?>>singletonList(messageConverter)); converters.add(messageConverter);
processor = new HttpEntityMethodProcessor(converters);
reset(messageConverter); reset(messageConverter);
Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class, Integer.TYPE, RequestEntity.class); Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class,
Integer.TYPE, RequestEntity.class);
paramHttpEntity = new MethodParameter(handle1, 0); paramHttpEntity = new MethodParameter(handle1, 0);
paramRequestEntity = new MethodParameter(handle1, 3); paramRequestEntity = new MethodParameter(handle1, 3);
paramResponseEntity = new MethodParameter(handle1, 1); paramResponseEntity = new MethodParameter(handle1, 1);
@ -205,14 +219,12 @@ public class HttpEntityMethodProcessorMockTests {
@Test @Test
public void handleReturnValue() throws Exception { public void handleReturnValue() throws Exception {
String body = "Foo"; String body = "Foo";
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
MediaType accepted = MediaType.TEXT_PLAIN; MediaType accepted = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Accept", accepted.toString()); servletRequest.addHeader("Accept", accepted.toString());
given(messageConverter.canWrite(String.class, null)).willReturn(true); initStringMessageConversion(accepted);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
@ -223,7 +235,7 @@ public class HttpEntityMethodProcessorMockTests {
@Test @Test
public void handleReturnValueProduces() throws Exception { public void handleReturnValueProduces() throws Exception {
String body = "Foo"; String body = "Foo";
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
servletRequest.addHeader("Accept", "text/*"); servletRequest.addHeader("Accept", "text/*");
servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML)); servletRequest.setAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.TEXT_HTML));
@ -263,7 +275,7 @@ public class HttpEntityMethodProcessorMockTests {
@Test(expected = HttpMediaTypeNotAcceptableException.class) @Test(expected = HttpMediaTypeNotAcceptableException.class)
public void handleReturnValueNotAcceptable() throws Exception { public void handleReturnValueNotAcceptable() throws Exception {
String body = "Foo"; String body = "Foo";
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
MediaType accepted = MediaType.APPLICATION_ATOM_XML; MediaType accepted = MediaType.APPLICATION_ATOM_XML;
servletRequest.addHeader("Accept", accepted.toString()); servletRequest.addHeader("Accept", accepted.toString());
@ -280,7 +292,7 @@ public class HttpEntityMethodProcessorMockTests {
@Test(expected = HttpMediaTypeNotAcceptableException.class) @Test(expected = HttpMediaTypeNotAcceptableException.class)
public void handleReturnValueNotAcceptableProduces() throws Exception { public void handleReturnValueNotAcceptableProduces() throws Exception {
String body = "Foo"; String body = "Foo";
ResponseEntity<String> returnValue = new ResponseEntity<String>(body, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>(body, HttpStatus.OK);
MediaType accepted = MediaType.TEXT_PLAIN; MediaType accepted = MediaType.TEXT_PLAIN;
servletRequest.addHeader("Accept", accepted.toString()); servletRequest.addHeader("Accept", accepted.toString());
@ -298,7 +310,7 @@ public class HttpEntityMethodProcessorMockTests {
@Test(expected=HttpMediaTypeNotAcceptableException.class) @Test(expected=HttpMediaTypeNotAcceptableException.class)
public void handleReturnValueNotAcceptableParseError() throws Exception { public void handleReturnValueNotAcceptableParseError() throws Exception {
ResponseEntity<String> returnValue = new ResponseEntity<String>("Body", HttpStatus.ACCEPTED); ResponseEntity<String> returnValue = new ResponseEntity<>("Body", HttpStatus.ACCEPTED);
servletRequest.addHeader("Accept", "01"); servletRequest.addHeader("Accept", "01");
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
@ -306,10 +318,10 @@ public class HttpEntityMethodProcessorMockTests {
} }
@Test @Test
public void responseHeaderNoBody() throws Exception { public void handleReturnValueResponseHeaderNoBody() throws Exception {
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.set("headerName", "headerValue"); headers.set("headerName", "headerValue");
ResponseEntity<String> returnValue = new ResponseEntity<String>(headers, HttpStatus.ACCEPTED); ResponseEntity<String> returnValue = new ResponseEntity<>(headers, HttpStatus.ACCEPTED);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
@ -318,15 +330,12 @@ public class HttpEntityMethodProcessorMockTests {
} }
@Test @Test
public void responseHeaderAndBody() throws Exception { public void handleReturnValueResponseHeaderAndBody() throws Exception {
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("header", "headerValue"); responseHeaders.set("header", "headerValue");
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.ACCEPTED); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.ACCEPTED);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class); ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
@ -336,18 +345,15 @@ public class HttpEntityMethodProcessorMockTests {
} }
@Test @Test
public void handleReturnTypeLastModified() throws Exception { public void handleReturnValueLastModified() throws Exception {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60); long oneMinuteAgo = currentTime - (1000 * 60);
servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(currentTime)); servletRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, dateFormat.format(currentTime));
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo); responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseNotModified(); assertResponseNotModified();
@ -356,17 +362,14 @@ public class HttpEntityMethodProcessorMockTests {
} }
@Test @Test
public void handleReturnTypeEtag() throws Exception { public void handleReturnValueEtag() throws Exception {
String etagValue = "\"deadb33f8badf00d\""; String etagValue = "\"deadb33f8badf00d\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseNotModified(); assertResponseNotModified();
@ -375,7 +378,7 @@ public class HttpEntityMethodProcessorMockTests {
} }
@Test @Test
public void handleReturnTypeETagAndLastModified() throws Exception { public void handleReturnValueETagAndLastModified() throws Exception {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60); long oneMinuteAgo = currentTime - (1000 * 60);
String etagValue = "\"deadb33f8badf00d\""; String etagValue = "\"deadb33f8badf00d\"";
@ -384,12 +387,9 @@ public class HttpEntityMethodProcessorMockTests {
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo); responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseNotModified(); assertResponseNotModified();
@ -400,19 +400,16 @@ public class HttpEntityMethodProcessorMockTests {
} }
@Test @Test
public void handleReturnTypeNotModified() throws Exception { public void handleReturnValueNotModified() throws Exception {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60); long oneMinuteAgo = currentTime - (1000 * 60);
String etagValue = "\"deadb33f8badf00d\""; String etagValue = "\"deadb33f8badf00d\"";
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo); responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.NOT_MODIFIED); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.NOT_MODIFIED);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseNotModified(); assertResponseNotModified();
@ -422,14 +419,8 @@ public class HttpEntityMethodProcessorMockTests {
assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG)); assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG));
} }
private void assertResponseNotModified() {
assertTrue(mavContainer.isRequestHandled());
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
assertEquals(0, servletResponse.getContentAsByteArray().length);
}
@Test @Test
public void handleReturnTypeChangedETagAndLastModified() throws Exception { public void handleReturnValueChangedETagAndLastModified() throws Exception {
long currentTime = new Date().getTime(); long currentTime = new Date().getTime();
long oneMinuteAgo = currentTime - (1000 * 60); long oneMinuteAgo = currentTime - (1000 * 60);
String etagValue = "\"deadb33f8badf00d\""; String etagValue = "\"deadb33f8badf00d\"";
@ -439,12 +430,9 @@ public class HttpEntityMethodProcessorMockTests {
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo); responseHeaders.setDate(HttpHeaders.LAST_MODIFIED, oneMinuteAgo);
responseHeaders.set(HttpHeaders.ETAG, changedEtagValue); responseHeaders.set(HttpHeaders.ETAG, changedEtagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertTrue(mavContainer.isRequestHandled()); assertTrue(mavContainer.isRequestHandled());
@ -458,19 +446,16 @@ public class HttpEntityMethodProcessorMockTests {
// SPR-13496 // SPR-13496
@Test @Test
public void handleReturnTypePostRequestWithIfNotModified() throws Exception { public void handleReturnValuePostRequestWithIfNotModified() throws Exception {
String wildcardValue = "*"; String wildcardValue = "*";
String etagValue = "\"some-etag\""; String etagValue = "\"some-etag\"";
servletRequest.setMethod("POST"); servletRequest.setMethod("POST");
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseOkWithBody("body"); assertResponseOkWithBody("body");
@ -480,18 +465,15 @@ public class HttpEntityMethodProcessorMockTests {
// SPR-13626 // SPR-13626
@Test @Test
public void handleReturnTypeGetIfNoneMatchWildcard() throws Exception { public void handleReturnValueGetIfNoneMatchWildcard() throws Exception {
String wildcardValue = "*"; String wildcardValue = "*";
String etagValue = "\"some-etag\""; String etagValue = "\"some-etag\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, wildcardValue);
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseOkWithBody("body"); assertResponseOkWithBody("body");
@ -501,18 +483,15 @@ public class HttpEntityMethodProcessorMockTests {
// SPR-13626 // SPR-13626
@Test @Test
public void handleReturnTypeIfNoneMatchIfMatch() throws Exception { public void handleReturnValueIfNoneMatchIfMatch() throws Exception {
String etagValue = "\"some-etag\""; String etagValue = "\"some-etag\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
servletRequest.addHeader(HttpHeaders.IF_MATCH, "ifmatch"); servletRequest.addHeader(HttpHeaders.IF_MATCH, "ifmatch");
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseOkWithBody("body"); assertResponseOkWithBody("body");
@ -522,18 +501,15 @@ public class HttpEntityMethodProcessorMockTests {
// SPR-13626 // SPR-13626
@Test @Test
public void handleReturnTypeIfNoneMatchIfUnmodifiedSince() throws Exception { public void handleReturnValueIfNoneMatchIfUnmodifiedSince() throws Exception {
String etagValue = "\"some-etag\""; String etagValue = "\"some-etag\"";
servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue); servletRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etagValue);
servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, dateFormat.format(new Date().getTime())); servletRequest.addHeader(HttpHeaders.IF_UNMODIFIED_SINCE, dateFormat.format(new Date().getTime()));
HttpHeaders responseHeaders = new HttpHeaders(); HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set(HttpHeaders.ETAG, etagValue); responseHeaders.set(HttpHeaders.ETAG, etagValue);
ResponseEntity<String> returnValue = new ResponseEntity<String>("body", responseHeaders, HttpStatus.OK); ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.OK);
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, MediaType.TEXT_PLAIN)).willReturn(true);
initStringMessageConversion(MediaType.TEXT_PLAIN);
processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest); processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
assertResponseOkWithBody("body"); assertResponseOkWithBody("body");
@ -541,11 +517,23 @@ public class HttpEntityMethodProcessorMockTests {
assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG)); assertEquals(etagValue, servletResponse.getHeader(HttpHeaders.ETAG));
} }
private void initStringMessageConversion(MediaType accepted) {
given(messageConverter.canWrite(String.class, null)).willReturn(true);
given(messageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
given(messageConverter.canWrite(String.class, accepted)).willReturn(true);
}
private void assertResponseNotModified() {
assertTrue(mavContainer.isRequestHandled());
assertEquals(HttpStatus.NOT_MODIFIED.value(), servletResponse.getStatus());
assertEquals(0, servletResponse.getContentAsByteArray().length);
}
private void assertResponseOkWithBody(String body) throws Exception { private void assertResponseOkWithBody(String body) throws Exception {
assertTrue(mavContainer.isRequestHandled()); assertTrue(mavContainer.isRequestHandled());
assertEquals(HttpStatus.OK.value(), servletResponse.getStatus()); assertEquals(HttpStatus.OK.value(), servletResponse.getStatus());
ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class); ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
verify(messageConverter).write(eq("body"), eq(MediaType.TEXT_PLAIN), outputMessage.capture()); verify(messageConverter).write(eq(body), eq(MediaType.TEXT_PLAIN), outputMessage.capture());
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")

Loading…
Cancel
Save