use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method ambiguousParams.
@PathPatternsParameterizedTest
void ambiguousParams(boolean usePathPatterns) throws Exception {
initDispatcherServlet(AmbiguousParamsController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("noParams");
request = new MockHttpServletRequest("GET", "/test");
request.addParameter("myParam", "42");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myParam-42");
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method relativePathDispatchingController.
@PathPatternsParameterizedTest
void relativePathDispatchingController(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MyRelativePathDispatchingController.class, usePathPatterns, wac -> {
if (!usePathPatterns) {
RootBeanDefinition mappingDef = new RootBeanDefinition(RequestMappingHandlerMapping.class);
mappingDef.getPropertyValues().add("useSuffixPatternMatch", true);
wac.registerBeanDefinition("handlerMapping", mappingDef);
}
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myHandle");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView");
request = new MockHttpServletRequest("GET", "/myApp/myOther");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myOtherView");
request = new MockHttpServletRequest("GET", "/myApp/myLang");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myLangView");
request = new MockHttpServletRequest("GET", "/myApp/surprise.do");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo(!usePathPatterns ? "mySurpriseView" : "myView");
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method methodNotAllowed.
@PathPatternsParameterizedTest
void methodNotAllowed(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MethodNotAllowedController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).as("Invalid response status").isEqualTo(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
String allowHeader = response.getHeader("Allow");
assertThat(allowHeader).as("No Allow header").isNotNull();
Set<String> allowedMethods = new HashSet<>(Arrays.asList(StringUtils.delimitedListToStringArray(allowHeader, ", ")));
assertThat(allowedMethods.size()).as("Invalid amount of supported methods").isEqualTo(6);
assertThat(allowedMethods.contains("PUT")).as("PUT not allowed").isTrue();
assertThat(allowedMethods.contains("DELETE")).as("DELETE not allowed").isTrue();
assertThat(allowedMethods.contains("HEAD")).as("HEAD not allowed").isTrue();
assertThat(allowedMethods.contains("TRACE")).as("TRACE not allowed").isTrue();
assertThat(allowedMethods.contains("OPTIONS")).as("OPTIONS not allowed").isTrue();
assertThat(allowedMethods.contains("POST")).as("POST not allowed").isTrue();
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method acceptHeaders.
@PathPatternsParameterizedTest
void acceptHeaders(boolean usePathPatterns) throws Exception {
initDispatcherServlet(AcceptHeadersController.class, usePathPatterns);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html");
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("html");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/xml, text/html");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "text/html;q=0.9, application/xml");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("xml");
request = new MockHttpServletRequest("GET", "/something");
request.addHeader("Accept", "application/msword");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getStatus()).isEqualTo(406);
}
use of org.springframework.web.servlet.handler.PathPatternsParameterizedTest in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method emptyParameterListHandleMethod.
@PathPatternsParameterizedTest
void emptyParameterListHandleMethod(boolean usePathPatterns) throws Exception {
initDispatcherServlet(EmptyParameterListHandlerMethodController.class, usePathPatterns, wac -> {
RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
vrDef.getPropertyValues().add("suffix", ".jsp");
wac.registerBeanDefinition("viewResolver", vrDef);
});
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
MockHttpServletResponse response = new MockHttpServletResponse();
EmptyParameterListHandlerMethodController.called = false;
getServlet().service(request, response);
assertThat(EmptyParameterListHandlerMethodController.called).isTrue();
assertThat(response.getContentAsString()).isEqualTo("");
}
Aggregations