if (cause != null) { exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod); exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
@Test public void invokeAndHandle_VoidWithTypeLevelResponseStatus() throws Exception { ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseStatusHandler(), "handle"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertTrue(this.mavContainer.isRequestHandled()); assertEquals(HttpStatus.BAD_REQUEST.value(), this.response.getStatus()); }
if (cause != null) { exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, cause, handlerMethod); exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, exception, handlerMethod);
@Test public void invokeAndHandle_VoidWithResponseStatus() throws Exception { ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "responseStatus"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertTrue("Null return value + @ResponseStatus should result in 'request handled'", this.mavContainer.isRequestHandled()); assertEquals(HttpStatus.BAD_REQUEST.value(), this.response.getStatus()); }
@Test public void invokeAndHandle_VoidWithComposedResponseStatus() throws Exception { ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "composedResponseStatus"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertTrue("Null return value + @ComposedResponseStatus should result in 'request handled'", this.mavContainer.isRequestHandled()); assertEquals(HttpStatus.BAD_REQUEST.value(), this.response.getStatus()); }
@Test public void wrapConcurrentResult_ResponseEntity() throws Exception { this.returnValueHandlers.addHandler(new HttpEntityMethodProcessor(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleDeferred"); handlerMethod = handlerMethod.wrapConcurrentResult(new ResponseEntity<>("bar", HttpStatus.OK)); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals("bar", this.response.getContentAsString()); }
private void wrapConcurrentResult_ResponseBody(Object handler, Object result, Class<?> expectedReturnType) throws Exception { this.returnValueHandlers.addHandler(new ModelAndViewMethodReturnValueHandler()); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(handler, "handle"); handlerMethod = handlerMethod.wrapConcurrentResult(result); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals((result != null ? result.toString() : ""), this.response.getContentAsString()); assertEquals(expectedReturnType, handlerMethod.getReturnValueType(result).getParameterType()); }
@Test(expected = HttpMessageNotWritableException.class) public void invokeAndHandle_Exception() throws Exception { this.returnValueHandlers.addHandler(new ExceptionRaisingReturnValueHandler()); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "handle"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); fail("Expected exception"); }
@Test // SPR-9159 public void invokeAndHandle_NotVoidWithResponseStatusAndReason() throws Exception { ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "responseStatusWithReason"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertTrue("When a status reason w/ used, the request is handled", this.mavContainer.isRequestHandled()); assertEquals(HttpStatus.BAD_REQUEST.value(), this.response.getStatus()); assertEquals("400 Bad Request", this.response.getErrorMessage()); }
@Test public void wrapConcurrentResult_CollectedValuesList() throws Exception { List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter()); ResolvableType elementType = ResolvableType.forClass(List.class); ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType); result.add(Arrays.asList("foo1", "bar1")); result.add(Arrays.asList("foo2", "bar2")); ContentNegotiationManager manager = new ContentNegotiationManager(); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager)); ServletInvocableHandlerMethod hm = getHandlerMethod(new MethodLevelResponseBodyHandler(), "handleFluxOfLists"); hm = hm.wrapConcurrentResult(result); hm.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("[[\"foo1\",\"bar1\"],[\"foo2\",\"bar2\"]]", this.response.getContentAsString()); }
@Test // SPR-12287 public void wrapConcurrentResult_ResponseEntityNullBody() throws Exception { this.returnValueHandlers.addHandler(new HttpEntityMethodProcessor(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleDeferred"); handlerMethod = handlerMethod.wrapConcurrentResult(new ResponseEntity<>(HttpStatus.OK)); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("", this.response.getContentAsString()); }
@Test public void invokeAndHandle_VoidWithHttpServletResponseArgument() throws Exception { this.argumentResolvers.addResolver(new ServletResponseMethodArgumentResolver()); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "httpServletResponse", HttpServletResponse.class); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertTrue("Null return value + HttpServletResponse arg should result in 'request handled'", this.mavContainer.isRequestHandled()); }
@Test public void invokeAndHandle_VoidRequestNotModified() throws Exception { this.request.addHeader("If-Modified-Since", 10 * 1000 * 1000); int lastModifiedTimestamp = 1000 * 1000; this.webRequest.checkNotModified(lastModifiedTimestamp); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "notModified"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertTrue("Null return value + 'not modified' request should result in 'request handled'", this.mavContainer.isRequestHandled()); }
@Test public void invokeAndHandle_DynamicReturnValue() throws Exception { this.argumentResolvers.addResolver(new RequestParamMethodArgumentResolver(null, false)); this.returnValueHandlers.addHandler(new ViewMethodReturnValueHandler()); this.returnValueHandlers.addHandler(new ViewNameMethodReturnValueHandler()); // Invoke without a request parameter (String return value) ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new Handler(), "dynamicReturnValue", String.class); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertNotNull(this.mavContainer.getView()); assertEquals(RedirectView.class, this.mavContainer.getView().getClass()); // Invoke with a request parameter (RedirectView return value) this.request.setParameter("param", "value"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals("view", this.mavContainer.getViewName()); }
@Test // SPR-12287 (16/Oct/14 comments) public void responseEntityRawTypeWithNullBody() throws Exception { this.returnValueHandlers.addHandler(new HttpEntityMethodProcessor(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleRawType"); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("", this.response.getContentAsString()); }
@Test // SPR-15478 public void wrapConcurrentResult_CollectedValuesListWithResponseEntity() throws Exception { List<HttpMessageConverter<?>> converters = Collections.singletonList(new MappingJackson2HttpMessageConverter()); ResolvableType elementType = ResolvableType.forClass(Bar.class); ReactiveTypeHandler.CollectedValuesList result = new ReactiveTypeHandler.CollectedValuesList(elementType); result.add(new Bar("foo")); result.add(new Bar("bar")); ContentNegotiationManager manager = new ContentNegotiationManager(); this.returnValueHandlers.addHandler(new RequestResponseBodyMethodProcessor(converters, manager)); ServletInvocableHandlerMethod hm = getHandlerMethod(new ResponseEntityHandler(), "handleFlux"); hm = hm.wrapConcurrentResult(result); hm.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("[{\"value\":\"foo\"},{\"value\":\"bar\"}]", this.response.getContentAsString()); }
@Test public void wrapConcurrentResult_ResponseEntityNullReturnValue() throws Exception { this.returnValueHandlers.addHandler(new HttpEntityMethodProcessor(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new ResponseEntityHandler(), "handleDeferred"); handlerMethod = handlerMethod.wrapConcurrentResult(null); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("", this.response.getContentAsString()); }
@Test public void wrapConcurrentResult_ResponseBodyEmitter() throws Exception { this.returnValueHandlers.addHandler(new ResponseBodyEmitterReturnValueHandler(this.converters)); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new StreamingHandler(), "handleEmitter"); handlerMethod = handlerMethod.wrapConcurrentResult(null); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("", this.response.getContentAsString()); }
@Test public void wrapConcurrentResult_StreamingResponseBody() throws Exception { this.returnValueHandlers.addHandler(new StreamingResponseBodyReturnValueHandler()); ServletInvocableHandlerMethod handlerMethod = getHandlerMethod(new StreamingHandler(), "handleStreamBody"); handlerMethod = handlerMethod.wrapConcurrentResult(null); handlerMethod.invokeAndHandle(this.webRequest, this.mavContainer); assertEquals(200, this.response.getStatus()); assertEquals("", this.response.getContentAsString()); }
invocableMethod.invokeAndHandle(webRequest, mavContainer); if (asyncManager.isConcurrentHandlingStarted()) { return null;