use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterizedAnnotatedInterface.
@SuppressWarnings("rawtypes")
@Test
public void parameterizedAnnotatedInterface() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext context) {
context.registerBeanDefinition("viewResolver", new RootBeanDefinition(ModelExposingViewResolver.class));
}
}, MyParameterizedControllerImpl.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPage");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("page1", request.getAttribute("viewName"));
HttpSession session = request.getSession();
assertTrue(session.getAttribute("object1") != null);
assertTrue(session.getAttribute("object2") != null);
assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
request = new MockHttpServletRequest("POST", "/myPage");
request.setSession(session);
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("page2", request.getAttribute("viewName"));
assertTrue(session.getAttribute("object1") != null);
assertTrue(session.getAttribute("object2") != null);
assertTrue(((Map) session.getAttribute("model")).containsKey("object1"));
assertTrue(((Map) session.getAttribute("model")).containsKey("object2"));
assertTrue(((Map) session.getAttribute("model")).containsKey("testBeanList"));
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsHtmlWithSuffixPresent.
@Test
public void responseBodyAsHtmlWithSuffixPresent() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
factoryBean.afterPropertiesSet();
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, TextRestController.class);
byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
request.setContent(content);
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(200, response.getStatus());
assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
assertNull(response.getHeader("Content-Disposition"));
assertArrayEquals(content, response.getContentAsByteArray());
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method emptyParameterListHandleMethod.
@Test
public void emptyParameterListHandleMethod() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext context) {
RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
vrDef.getPropertyValues().add("suffix", ".jsp");
context.registerBeanDefinition("viewResolver", vrDef);
}
}, EmptyParameterListHandlerMethodController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
MockHttpServletResponse response = new MockHttpServletResponse();
EmptyParameterListHandlerMethodController.called = false;
getServlet().service(request, response);
assertTrue(EmptyParameterListHandlerMethodController.called);
assertEquals("", response.getContentAsString());
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterCsvAsStringArray.
@Test
public void parameterCsvAsStringArray() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
wbiDef.getPropertyValues().add("conversionService", csDef);
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, CsvController.class);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/integerArray");
request.setMethod("POST");
request.addParameter("content", "1,2");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("1-2", response.getContentAsString());
}
use of org.springframework.web.context.support.GenericWebApplicationContext in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyArgMismatch.
@Test
public void responseBodyArgMismatch() throws ServletException, IOException {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext wac) {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(A.class, B.class);
try {
marshaller.afterPropertiesSet();
} catch (Exception ex) {
throw new BeanCreationException(ex.getMessage(), ex);
}
MarshallingHttpMessageConverter messageConverter = new MarshallingHttpMessageConverter(marshaller);
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("messageConverters", messageConverter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, RequestBodyArgMismatchController.class);
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "<b/>";
request.setContent(requestBody.getBytes("UTF-8"));
request.addHeader("Content-Type", "application/xml; charset=utf-8");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals(400, response.getStatus());
}
Aggregations