use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithOptionalParameter.
@PathPatternsParameterizedTest
void dataClassBindingWithOptionalParameter(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ValidatedDataClassController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind");
request.addParameter("param1", "value1");
request.addParameter("param2", "true");
request.addParameter("optionalParam", "8");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("value1-true-8");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method responseBodyNoAcceptableMediaType.
@PathPatternsParameterizedTest
void responseBodyNoAcceptableMediaType(boolean usePathPatterns) throws Exception {
initDispatcherServlet(RequestResponseBodyProducesController.class, usePathPatterns, wac -> {
RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
StringHttpMessageConverter converter = new StringHttpMessageConverter();
adapterDef.getPropertyValues().add("messageConverters", converter);
wac.registerBeanDefinition("handlerAdapter", adapterDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
String requestBody = "Hello World";
request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
request.addHeader("Content-Type", "text/plain; charset=utf-8");
request.addHeader("Accept", "application/pdf, application/msword");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(406);
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method httpHead.
@PathPatternsParameterizedTest
void httpHead(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ResponseEntityController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/baz");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader("MyResponseHeader")).isEqualTo("MyValue");
assertThat(response.getContentLength()).isEqualTo(4);
assertThat(response.getContentAsByteArray().length == 0).isTrue();
// Now repeat with GET
request = new MockHttpServletRequest("GET", "/baz");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getHeader("MyResponseHeader")).isEqualTo("MyValue");
assertThat(response.getContentLength()).isEqualTo(4);
assertThat(response.getContentAsString()).isEqualTo("body");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method contentTypeHeaders.
@PathPatternsParameterizedTest
void contentTypeHeaders(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ContentTypeHeadersController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
request.setContentType("application/pdf");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("pdf");
request = new MockHttpServletRequest("POST", "/something");
request.setContentType("text/html");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("text");
request = new MockHttpServletRequest("POST", "/something");
request.setContentType("application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(415);
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method restController.
@PathPatternsParameterizedTest
void restController(boolean usePathPatterns) throws Exception {
initDispatcherServlet(ThisWillActuallyRun.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("Hello World!");
}
Aggregations