use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class WebMvcConfigurationSupportExtensionTests method handlerMappings.
@Test
public void handlerMappings() throws Exception {
RequestMappingHandlerMapping rmHandlerMapping = this.config.requestMappingHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
rmHandlerMapping.setApplicationContext(this.context);
rmHandlerMapping.afterPropertiesSet();
assertThat(rmHandlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(rmHandlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
HandlerExecutionChain chain = rmHandlerMapping.getHandler(new MockHttpServletRequest("GET", "/"));
assertThat(chain).isNotNull();
HandlerInterceptor[] interceptors = chain.getInterceptors();
assertThat(interceptors).isNotNull();
assertThat(interceptors.length).isEqualTo(4);
assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
assertThat(interceptors[1].getClass()).isEqualTo(LocaleChangeInterceptor.class);
assertThat(interceptors[2].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
assertThat(interceptors[3].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
Map<RequestMappingInfo, HandlerMethod> map = rmHandlerMapping.getHandlerMethods();
assertThat(map.size()).isEqualTo(2);
RequestMappingInfo info = map.entrySet().stream().filter(entry -> entry.getValue().getBeanType().equals(UserController.class)).findFirst().orElseThrow(() -> new AssertionError("UserController bean not found")).getKey();
assertThat(info.getPatternValues()).isEqualTo(Collections.singleton("/api/user/{id}"));
AbstractHandlerMapping handlerMapping = (AbstractHandlerMapping) this.config.viewControllerHandlerMapping(this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo(1);
assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/path"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/bad"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/old"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
handlerMapping = (AbstractHandlerMapping) this.config.resourceHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo((Integer.MAX_VALUE - 1));
assertThat(handlerMapping.getUrlPathHelper().getClass()).isEqualTo(TestPathHelper.class);
assertThat(handlerMapping.getPathMatcher().getClass()).isEqualTo(TestPathMatcher.class);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/resources/foo.gif"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
interceptors = chain.getInterceptors();
assertThat(interceptors.length).as(Arrays.toString(interceptors)).isEqualTo(5);
assertThat(interceptors[0].getClass().getSimpleName()).isEqualTo("CorsInterceptor");
// PathExposingHandlerInterceptor at interceptors[1]
assertThat(interceptors[2].getClass()).isEqualTo(LocaleChangeInterceptor.class);
assertThat(interceptors[3].getClass()).isEqualTo(ConversionServiceExposingInterceptor.class);
assertThat(interceptors[4].getClass()).isEqualTo(ResourceUrlProviderExposingInterceptor.class);
handlerMapping = (AbstractHandlerMapping) this.config.defaultServletHandlerMapping();
handlerMapping.setApplicationContext(this.context);
assertThat(handlerMapping).isNotNull();
assertThat(handlerMapping.getOrder()).isEqualTo(Integer.MAX_VALUE);
chain = handlerMapping.getHandler(new MockHttpServletRequest("GET", "/anyPath"));
assertThat(chain).isNotNull();
assertThat(chain.getHandler()).isNotNull();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() throws ServletException, IOException {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
EntityResponse<String> entityResponse = EntityResponse.fromObject("bar").lastModified(oneMinuteBeforeNow).build();
MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "https://example.com");
mockRequest.addHeader(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now));
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
ModelAndView mav = entityResponse.writeTo(mockRequest, mockResponse, EMPTY_CONTEXT);
assertThat(mav).isNull();
assertThat(mockResponse.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DefaultEntityResponseBuilderTests method notModifiedEtag.
@Test
public void notModifiedEtag() throws Exception {
String etag = "\"foo\"";
EntityResponse<String> entityResponse = EntityResponse.fromObject("bar").eTag(etag).build();
MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "https://example.com");
mockRequest.addHeader(HttpHeaders.IF_NONE_MATCH, etag);
MockHttpServletResponse mockResponse = new MockHttpServletResponse();
ModelAndView mav = entityResponse.writeTo(mockRequest, mockResponse, EMPTY_CONTEXT);
assertThat(mav).isNull();
assertThat(mockResponse.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method anotherLocaleRequest.
@Test
public void anotherLocaleRequest() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
request.addPreferredLocale(Locale.CANADA);
request.addUserRole("role1");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getForwardedUrl() == null).as("Not forwarded").isTrue();
assertThat(request.getAttribute("test1") != null).isTrue();
assertThat(request.getAttribute("test1x") == null).isTrue();
assertThat(request.getAttribute("test1y") == null).isTrue();
assertThat(request.getAttribute("test2") != null).isTrue();
assertThat(request.getAttribute("test2x") == null).isTrue();
assertThat(request.getAttribute("test2y") == null).isTrue();
assertThat(request.getAttribute("test3") != null).isTrue();
assertThat(request.getAttribute("test3x") != null).isTrue();
assertThat(request.getAttribute("test3y") != null).isTrue();
assertThat(response.getHeader("Last-Modified")).isEqualTo("Wed, 01 Apr 2015 00:00:01 GMT");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method noCleanupAfterInclude.
@Test
public void noCleanupAfterInclude() throws ServletException, IOException {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
MockHttpServletResponse response = new MockHttpServletResponse();
request.setAttribute("test1", "value1");
request.setAttribute("test2", "value2");
WebApplicationContext wac = new StaticWebApplicationContext();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
TestBean command = new TestBean();
request.setAttribute("command", command);
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
simpleDispatcherServlet.setCleanupAfterInclude(false);
simpleDispatcherServlet.service(request, response);
assertThat(request.getAttribute("test1")).isEqualTo("value1");
assertThat(request.getAttribute("test2")).isEqualTo("value2");
assertThat(request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isSameAs(wac);
}
Aggregations