use of org.springframework.web.servlet.HandlerAdapter in project spring-integration by spring-projects.
the class IntegrationGraphControllerTests method testIntegrationGraphControllerParser.
@Test
public void testIntegrationGraphControllerParser() throws Exception {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("IntegrationGraphControllerParserTests-context.xml", getClass());
HandlerMapping handlerMapping = context.getBean(RequestMappingHandlerMapping.class.getName(), HandlerMapping.class);
HandlerAdapter handlerAdapter = context.getBean(RequestMappingHandlerAdapter.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setMethod("GET");
request.setRequestURI("/foo");
request.addHeader(HttpHeaders.ORIGIN, "http://foo.bar.com");
MockHttpServletResponse response = new MockHttpServletResponse();
HandlerExecutionChain executionChain = handlerMapping.getHandler(request);
assertNotNull(executionChain);
Object handler = executionChain.getHandler();
for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
// Assert the CORS config
assertTrue(handlerInterceptor.preHandle(request, response, handler));
}
handlerAdapter.handle(request, response, handler);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertThat(response.getContentAsString(), containsString("\"name\":\"nullChannel\","));
assertThat(response.getContentAsString(), not(containsString("\"name\":\"myChannel\",")));
context.getBeanFactory().registerSingleton("myChannel", new DirectChannel());
request = new MockHttpServletRequest();
request.setMethod("GET");
request.setRequestURI("/foo/refresh");
response = new MockHttpServletResponse();
executionChain = handlerMapping.getHandler(request);
assertNotNull(executionChain);
handler = executionChain.getHandler();
for (HandlerInterceptor handlerInterceptor : executionChain.getInterceptors()) {
// Assert the CORS config
assertTrue(handlerInterceptor.preHandle(request, response, handler));
}
handlerAdapter.handle(request, response, handler);
assertEquals(HttpStatus.OK.value(), response.getStatus());
assertThat(response.getContentAsString(), containsString("\"name\":\"myChannel\","));
context.close();
}
use of org.springframework.web.servlet.HandlerAdapter in project openmrs-core by openmrs.
the class WebTestHelper method handle.
/**
* Handles the request with a proper controller.
*
* @param request
* @return
* @throws Exception
*/
public Response handle(final HttpServletRequest request) throws Exception {
if (handlerMappings == null || handlerAdapters == null) {
throw new UnsupportedOperationException("The web context is not configured!");
}
// Simulate a request with a fresh Hibernate session
Context.flushSession();
Context.clearSession();
final MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView modelAndView = null;
HandlerExecutionChain handlerChain = null;
for (HandlerMapping handlerMapping : handlerMappings) {
handlerChain = handlerMapping.getHandler(request);
if (handlerChain != null) {
break;
}
}
Assert.assertNotNull("The requested URI has no mapping: " + request.getRequestURI(), handlerChain);
boolean supported = false;
for (HandlerAdapter handlerAdapter : handlerAdapters) {
final Object handler = handlerChain.getHandler();
if (handlerAdapter.supports(handler)) {
Assert.assertFalse("The requested URI has more than one handler: " + request.getRequestURI(), supported);
modelAndView = handlerAdapter.handle(request, response, handler);
supported = true;
}
}
Assert.assertTrue("The requested URI has no handlers: " + request.getRequestURI(), supported);
return new Response(response, request.getSession(), modelAndView);
}
Aggregations