Search in sources :

Example 36 with ModelAndViewContainer

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

the class RequestPartMethodArgumentResolverTests method isMultipartRequest.

@Test
public void isMultipartRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    assertThatExceptionOfType(MultipartException.class).isThrownBy(() -> resolver.resolveArgument(paramMultipartFile, new ModelAndViewContainer(), new ServletWebRequest(request), null));
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MultipartException(org.springframework.web.multipart.MultipartException) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 37 with ModelAndViewContainer

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

the class RequestResponseBodyMethodProcessorMockTests method setup.

@BeforeEach
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    stringMessageConverter = mock(HttpMessageConverter.class);
    given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    given(stringMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resourceMessageConverter = mock(HttpMessageConverter.class);
    given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
    resourceRegionMessageConverter = mock(HttpMessageConverter.class);
    given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
    processor = new RequestResponseBodyMethodProcessor(Arrays.asList(stringMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
    mavContainer = new ModelAndViewContainer();
    servletRequest = new MockHttpServletRequest();
    servletRequest.setMethod("POST");
    servletResponse = new MockHttpServletResponse();
    webRequest = new ServletWebRequest(servletRequest, servletResponse);
    Method methodHandle1 = getClass().getMethod("handle1", String.class, Integer.TYPE);
    paramRequestBodyString = new MethodParameter(methodHandle1, 0);
    paramInt = new MethodParameter(methodHandle1, 1);
    paramValidBean = new MethodParameter(getClass().getMethod("handle2", SimpleBean.class), 0);
    paramStringNotRequired = new MethodParameter(getClass().getMethod("handle3", String.class), 0);
    paramOptionalString = new MethodParameter(getClass().getMethod("handle4", Optional.class), 0);
    returnTypeString = new MethodParameter(methodHandle1, -1);
    returnTypeInt = new MethodParameter(getClass().getMethod("handle5"), -1);
    returnTypeStringProduces = new MethodParameter(getClass().getMethod("handle6"), -1);
    returnTypeResource = new MethodParameter(getClass().getMethod("handle7"), -1);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) 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 38 with ModelAndViewContainer

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

the class StreamingResponseBodyReturnValueHandlerTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.handler = new StreamingResponseBodyReturnValueHandler();
    this.mavContainer = new ModelAndViewContainer();
    this.request = new MockHttpServletRequest("GET", "/path");
    this.response = new MockHttpServletResponse();
    this.webRequest = new ServletWebRequest(this.request, this.response);
    AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(asyncWebRequest);
    this.request.setAsyncSupported(true);
}
Also used : StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) AsyncWebRequest(org.springframework.web.context.request.async.AsyncWebRequest) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 39 with ModelAndViewContainer

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

the class ModelFactoryTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
    this.attributeStore = new DefaultSessionAttributeStore();
    this.attributeHandler = new SessionAttributesHandler(TestController.class, this.attributeStore);
    this.controller = new TestController();
    this.mavContainer = new ModelAndViewContainer();
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DefaultSessionAttributeStore(org.springframework.web.bind.support.DefaultSessionAttributeStore) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 40 with ModelAndViewContainer

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

the class ModelFactoryTests method updateModelBindingResult.

@Test
public void updateModelBindingResult() throws Exception {
    String commandName = "attr1";
    Object command = new Object();
    ModelAndViewContainer container = new ModelAndViewContainer();
    container.addAttribute(commandName, command);
    WebDataBinder dataBinder = new WebDataBinder(command, commandName);
    WebDataBinderFactory binderFactory = mock(WebDataBinderFactory.class);
    given(binderFactory.createBinder(this.webRequest, command, commandName)).willReturn(dataBinder);
    ModelFactory modelFactory = new ModelFactory(null, binderFactory, this.attributeHandler);
    modelFactory.updateModel(this.webRequest, container);
    assertThat(container.getModel().get(commandName)).isEqualTo(command);
    String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + commandName;
    assertThat(container.getModel().get(bindingResultKey)).isSameAs(dataBinder.getBindingResult());
    assertThat(container.getModel().size()).isEqualTo(2);
}
Also used : WebDataBinder(org.springframework.web.bind.WebDataBinder) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) WebDataBinderFactory(org.springframework.web.bind.support.WebDataBinderFactory) Test(org.junit.jupiter.api.Test)

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