use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method relativeMethodPathDispatchingController.
@Test
public void relativeMethodPathDispatchingController() throws Exception {
initServletWithControllers(MyRelativeMethodPathDispatchingController.class);
getServlet().init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/yourApp/myOther");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myOtherView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/hisApp/myLang");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("myLangView", response.getContentAsString());
request = new MockHttpServletRequest("GET", "/herApp/surprise.do");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("mySurpriseView", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method prototypeController.
@Test
public void prototypeController() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext context) {
RootBeanDefinition beanDef = new RootBeanDefinition(PrototypeController.class);
beanDef.setScope(BeanDefinition.SCOPE_PROTOTYPE);
context.registerBeanDefinition("controller", beanDef);
}
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
request.addParameter("param", "1");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("count:3", response.getContentAsString());
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("count:3", response.getContentAsString());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method requiredParamMissing.
@Test
public void requiredParamMissing() throws Exception {
WebApplicationContext webAppContext = initServletWithControllers(RequiredParamController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
assertTrue(webAppContext.isSingleton(RequiredParamController.class.getSimpleName()));
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsHtml.
@Test
public void responseBodyAsHtml() 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", "/a1.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());
assertEquals("inline;filename=f.txt", response.getHeader("Content-Disposition"));
assertArrayEquals(content, response.getContentAsByteArray());
}
use of org.springframework.mock.web.test.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method typeNestedSetBinding.
@Test
public void typeNestedSetBinding() throws Exception {
initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
@Override
public void initialize(GenericWebApplicationContext context) {
RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
csDef.getPropertyValues().add("converters", new TestBeanConverter());
RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
wbiDef.getPropertyValues().add("conversionService", csDef);
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
context.registerBeanDefinition("handlerAdapter", adapterDef);
}
}, NestedSetController.class);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
request.addParameter("testBeanSet", new String[] { "1", "2" });
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertEquals("[1, 2]-org.springframework.tests.sample.beans.TestBean", response.getContentAsString());
}
Aggregations