Search in sources :

Example 21 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class RequestMappingInfoHandlerMappingTests method testHttpOptions.

private void testHttpOptions(TestRequestMappingInfoHandlerMapping mapping, String requestURI, String allowHeader, @Nullable MediaType acceptPatch) throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", requestURI);
    HandlerMethod handlerMethod = getHandler(mapping, request);
    ServletWebRequest webRequest = new ServletWebRequest(request);
    ModelAndViewContainer mavContainer = new ModelAndViewContainer();
    Object result = new InvocableHandlerMethod(handlerMethod).invokeForRequest(webRequest, mavContainer);
    assertThat(result).isNotNull();
    assertThat(result.getClass()).isEqualTo(HttpHeaders.class);
    HttpHeaders headers = (HttpHeaders) result;
    Set<HttpMethod> allowedMethods = Arrays.stream(allowHeader.split(",")).map(HttpMethod::valueOf).collect(Collectors.toSet());
    assertThat(headers.getAllow()).hasSameElementsAs(allowedMethods);
    if (acceptPatch != null && headers.getAllow().contains(HttpMethod.PATCH)) {
        assertThat(headers.getAcceptPatch()).containsExactly(acceptPatch);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) HttpMethod(org.springframework.http.HttpMethod)

Example 22 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Exception {
    container = new ModelAndViewContainer();
    servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletResponse = new MockHttpServletResponse();
    request = new ServletWebRequest(servletRequest, servletResponse);
    this.factory = new ValidatingBinderFactory();
    Method method = getClass().getDeclaredMethod("handle", List.class, SimpleBean.class, MultiValueMap.class, String.class);
    paramGenericList = new MethodParameter(method, 0);
    paramSimpleBean = new MethodParameter(method, 1);
    paramMultiValueMap = new MethodParameter(method, 2);
    paramString = new MethodParameter(method, 3);
    returnTypeString = new MethodParameter(method, -1);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 23 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class PathVariableMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    resolver = new PathVariableMethodArgumentResolver();
    mavContainer = new ModelAndViewContainer();
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
    Method method = ReflectionUtils.findMethod(getClass(), "handle", (Class<?>[]) null);
    paramNamedString = new SynthesizingMethodParameter(method, 0);
    paramString = new SynthesizingMethodParameter(method, 1);
    paramNotRequired = new SynthesizingMethodParameter(method, 2);
    paramOptional = new SynthesizingMethodParameter(method, 3);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 24 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class MatrixVariablesMapMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.resolver = new MatrixVariableMapMethodArgumentResolver();
    this.mavContainer = new ModelAndViewContainer();
    this.request = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
    Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
    this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedHashMap(java.util.LinkedHashMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 25 with ModelAndViewContainer

use of org.springframework.web.method.support.ModelAndViewContainer in project spring-framework by spring-projects.

the class MatrixVariablesMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.resolver = new MatrixVariableMethodArgumentResolver();
    this.mavContainer = new ModelAndViewContainer();
    this.request = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
    Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
    this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedHashMap(java.util.LinkedHashMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)44 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)29 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)22 BeforeEach (org.junit.jupiter.api.BeforeEach)20 MethodParameter (org.springframework.core.MethodParameter)14 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 Method (java.lang.reflect.Method)10 Test (org.junit.jupiter.api.Test)10 WebDataBinderFactory (org.springframework.web.bind.support.WebDataBinderFactory)5 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)4 HandlerMethod (org.springframework.web.method.HandlerMethod)4 HttpMethod (org.springframework.http.HttpMethod)3 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)3 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 WebDataBinder (org.springframework.web.bind.WebDataBinder)3 LinkedHashMap (java.util.LinkedHashMap)2 Before (org.junit.Before)2 Test (org.junit.Test)2 HttpInputMessage (org.springframework.http.HttpInputMessage)2 Nullable (org.springframework.lang.Nullable)2