use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DefaultRenderingResponseTests method cookies.
@Test
public void cookies() throws Exception {
MultiValueMap<String, Cookie> newCookies = new LinkedMultiValueMap<>();
newCookies.add("name", new Cookie("name", "value"));
RenderingResponse result = RenderingResponse.create("foo").cookies(cookies -> cookies.addAll(newCookies)).build();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
assertThat(mav).isNotNull();
assertThat(response.getCookies().length).isEqualTo(1);
assertThat(response.getCookies()[0].getName()).isEqualTo("name");
assertThat(response.getCookies()[0].getValue()).isEqualTo("value");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DefaultRenderingResponseTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
RenderingResponse result = RenderingResponse.create("bar").header(HttpHeaders.LAST_MODIFIED, DateTimeFormatter.RFC_1123_DATE_TIME.format(oneMinuteBeforeNow)).build();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "https://example.com");
request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now));
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
assertThat(mav).isNull();
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class RouterFunctionsTests method nestPathVariable.
@Test
public void nestPathVariable() {
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
RequestPredicate requestPredicate = request -> request.pathVariable("foo").equals("bar");
RouterFunction<ServerResponse> nestedFunction = RouterFunctions.route(requestPredicate, handlerFunction);
RouterFunction<ServerResponse> result = RouterFunctions.nest(RequestPredicates.path("/{foo}"), nestedFunction);
assertThat(result).isNotNull();
MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/bar");
ServerRequest request = new DefaultServerRequest(servletRequest, Collections.emptyList());
Optional<HandlerFunction<ServerResponse>> resultHandlerFunction = result.route(request);
assertThat(resultHandlerFunction.isPresent()).isTrue();
assertThat(resultHandlerFunction.get()).isEqualTo(handlerFunction);
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class BeanNameUrlHandlerMappingTests method asteriskMatches.
@Test
public void asteriskMatches() throws Exception {
HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
Object bean = wac.getBean("godCtrl");
MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
HandlerExecutionChain hec = hm.getHandler(req);
assertThat(hec != null && hec.getHandler() == bean).as("Handler is correct bean").isTrue();
req = new MockHttpServletRequest("GET", "/mypath/testarossa");
hec = hm.getHandler(req);
assertThat(hec != null && hec.getHandler() == bean).as("Handler is correct bean").isTrue();
req = new MockHttpServletRequest("GET", "/mypath/tes");
hec = hm.getHandler(req);
assertThat(hec == null).as("Handler is correct bean").isTrue();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class MvcNamespaceTests method doTestCustomValidator.
private void doTestCustomValidator(String xml) throws Exception {
loadBeanDefinitions(xml);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertThat(mapping).isNotNull();
assertThat(mapping.getUrlPathHelper().shouldRemoveSemicolonContent()).isFalse();
RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
assertThat(adapter).isNotNull();
assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isTrue();
// default web binding initializer behavior test
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("date", "2009-10-31");
MockHttpServletResponse response = new MockHttpServletResponse();
adapter.handle(request, response, handlerMethod);
assertThat(appContext.getBean(TestValidator.class).validatorInvoked).isTrue();
assertThat(handler.recordedValidationError).isFalse();
}
Aggregations