Search in sources :

Example 31 with ModelAndViewContainer

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

the class RequestPartMethodArgumentResolverTests method resolveRequestPartWithoutContentType.

// gh-26501
@Test
public void resolveRequestPartWithoutContentType() throws Exception {
    MockMultipartHttpServletRequest servletRequest = new MockMultipartHttpServletRequest();
    servletRequest.addPart(new MockPart("requestPartString", "part value".getBytes(StandardCharsets.UTF_8)));
    ServletWebRequest webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
    List<HttpMessageConverter<?>> converters = Collections.singletonList(new StringHttpMessageConverter());
    RequestPartMethodArgumentResolver resolver = new RequestPartMethodArgumentResolver(converters);
    MethodParameter parameter = ResolvableMethod.on(getClass()).named("handle").build().arg(String.class);
    Object actualValue = resolver.resolveArgument(parameter, new ModelAndViewContainer(), webRequest, new ValidatingBinderFactory());
    assertThat(actualValue).isEqualTo("part value");
}
Also used : MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MockPart(org.springframework.web.testfixture.servlet.MockPart) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 32 with ModelAndViewContainer

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

the class ServletModelAttributeMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Exception {
    processor = new ServletModelAttributeMethodProcessor(false);
    ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
    initializer.setConversionService(new DefaultConversionService());
    binderFactory = new ServletRequestDataBinderFactory(null, initializer);
    mavContainer = new ModelAndViewContainer();
    request = new MockHttpServletRequest();
    webRequest = new ServletWebRequest(request);
    Method method = getClass().getDeclaredMethod("modelAttribute", TestBean.class, TestBeanWithoutStringConstructor.class, Optional.class);
    testBeanModelAttr = new MethodParameter(method, 0);
    testBeanWithoutStringConstructorModelAttr = new MethodParameter(method, 1);
    testBeanWithOptionalModelAttr = new MethodParameter(method, 2);
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 33 with ModelAndViewContainer

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

the class ServletResponseMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    resolver = new ServletResponseMethodArgumentResolver();
    mavContainer = new ModelAndViewContainer();
    servletResponse = new MockHttpServletResponse();
    webRequest = new ServletWebRequest(new MockHttpServletRequest(), servletResponse);
    method = getClass().getMethod("supportedParams", ServletResponse.class, OutputStream.class, Writer.class);
}
Also used : MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) OutputStream(java.io.OutputStream) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Writer(java.io.Writer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 34 with ModelAndViewContainer

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

the class UriComponentsBuilderMethodArgumentResolverTests method resolveArgument.

@Test
public void resolveArgument() throws Exception {
    this.servletRequest.setContextPath("/myapp");
    this.servletRequest.setServletPath("/main");
    this.servletRequest.setPathInfo("/accounts");
    Object actual = this.resolver.resolveArgument(this.builderParam, new ModelAndViewContainer(), this.webRequest, null);
    assertThat(actual).isNotNull();
    assertThat(actual.getClass()).isEqualTo(ServletUriComponentsBuilder.class);
    assertThat(((ServletUriComponentsBuilder) actual).build().toUriString()).isEqualTo("http://localhost/myapp/main");
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) Test(org.junit.jupiter.api.Test)

Example 35 with ModelAndViewContainer

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

the class ServletRequestMethodArgumentResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    resolver = new ServletRequestMethodArgumentResolver();
    mavContainer = new ModelAndViewContainer();
    servletRequest = new MockHttpServletRequest("GET", "");
    webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
    method = getClass().getMethod("supportedParams", ServletRequest.class, MultipartRequest.class, HttpSession.class, Principal.class, Locale.class, InputStream.class, Reader.class, WebRequest.class, TimeZone.class, ZoneId.class, HttpMethod.class, PushBuilder.class);
}
Also used : Locale(java.util.Locale) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ZoneId(java.time.ZoneId) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.web.testfixture.servlet.MockHttpSession) InputStream(java.io.InputStream) Reader(java.io.Reader) TimeZone(java.util.TimeZone) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) WebRequest(org.springframework.web.context.request.WebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultipartRequest(org.springframework.web.multipart.MultipartRequest) PushBuilder(jakarta.servlet.http.PushBuilder) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Principal(java.security.Principal) HttpMethod(org.springframework.http.HttpMethod) 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