Search in sources :

Example 36 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class MvcNamespaceTests method testContentNegotiationManager.

@Test
public void testContentNegotiationManager() throws Exception {
    loadBeanDefinitions("mvc-config-content-negotiation-manager.xml");
    RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    request.setParameter("format", "xml");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    assertThat(manager.resolveMediaTypes(webRequest)).containsExactly(MediaType.valueOf("application/rss+xml"));
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertThat(compositeResolver).isNotNull();
    assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(1);
    ViewResolver resolver = compositeResolver.getViewResolvers().get(0);
    assertThat(resolver.getClass()).isEqualTo(ContentNegotiatingViewResolver.class);
    ContentNegotiatingViewResolver cnvr = (ContentNegotiatingViewResolver) resolver;
    assertThat(cnvr.getContentNegotiationManager()).isSameAs(manager);
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 37 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class WebMvcConfigurationSupportExtensionTests method contentNegotiation.

@Test
@SuppressWarnings("deprecation")
public void contentNegotiation() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
    NativeWebRequest webRequest = new ServletWebRequest(request);
    RequestMappingHandlerMapping mapping = this.config.requestMappingHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
    request.setParameter("f", "json");
    ContentNegotiationManager manager = mapping.getContentNegotiationManager();
    assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(APPLICATION_JSON));
    request.setParameter("f", "xml");
    assertThat(manager.resolveMediaTypes(webRequest)).isEqualTo(Collections.singletonList(APPLICATION_XML));
    SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) this.config.resourceHandlerMapping(this.config.mvcContentNegotiationManager(), this.config.mvcConversionService(), this.config.mvcResourceUrlProvider());
    handlerMapping.setApplicationContext(this.context);
    request = new MockHttpServletRequest("GET", "/resources/foo.gif");
    HandlerExecutionChain chain = handlerMapping.getHandler(request);
    assertThat(chain).isNotNull();
    ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) chain.getHandler();
    assertThat(handler).isNotNull();
    assertThat(handler.getContentNegotiationManager()).isSameAs(manager);
}
Also used : ResourceHttpRequestHandler(org.springframework.web.servlet.resource.ResourceHttpRequestHandler) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 38 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class ViewMethodReturnValueHandlerTests method setup.

@BeforeEach
public void setup() {
    this.handler = new ViewMethodReturnValueHandler();
    this.mavContainer = new ModelAndViewContainer();
    this.webRequest = new ServletWebRequest(new MockHttpServletRequest());
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 39 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class ServletRequestMethodArgumentResolverTests method pushBuilder.

@Test
public void pushBuilder() throws Exception {
    final PushBuilder pushBuilder = Mockito.mock(PushBuilder.class);
    servletRequest = new MockHttpServletRequest("GET", "") {

        @Override
        public PushBuilder newPushBuilder() {
            return pushBuilder;
        }
    };
    ServletWebRequest webRequest = new ServletWebRequest(servletRequest, new MockHttpServletResponse());
    MethodParameter pushBuilderParameter = new MethodParameter(method, 11);
    assertThat(resolver.supportsParameter(pushBuilderParameter)).as("PushBuilder not supported").isTrue();
    Object result = resolver.resolveArgument(pushBuilderParameter, null, webRequest, null);
    assertThat(result).as("Invalid result").isSameAs(pushBuilder);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PushBuilder(jakarta.servlet.http.PushBuilder) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 40 with ServletWebRequest

use of org.springframework.web.context.request.ServletWebRequest in project spring-framework by spring-projects.

the class ModelAttributeMethodProcessorTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.request = new ServletWebRequest(new MockHttpServletRequest());
    this.container = new ModelAndViewContainer();
    this.processor = new ModelAttributeMethodProcessor(false);
    Method method = ModelAttributeHandler.class.getDeclaredMethod("modelAttribute", TestBean.class, Errors.class, int.class, TestBean.class, TestBean.class, TestBean.class, TestBeanWithConstructorArgs.class);
    this.paramNamedValidModelAttr = new SynthesizingMethodParameter(method, 0);
    this.paramErrors = new SynthesizingMethodParameter(method, 1);
    this.paramInt = new SynthesizingMethodParameter(method, 2);
    this.paramModelAttr = new SynthesizingMethodParameter(method, 3);
    this.paramBindingDisabledAttr = new SynthesizingMethodParameter(method, 4);
    this.paramNonSimpleType = new SynthesizingMethodParameter(method, 5);
    this.beanWithConstructorArgs = new SynthesizingMethodParameter(method, 6);
    method = getClass().getDeclaredMethod("annotatedReturnValue");
    this.returnParamNamedModelAttr = new MethodParameter(method, -1);
    method = getClass().getDeclaredMethod("notAnnotatedReturnValue");
    this.returnParamNonSimpleType = new MethodParameter(method, -1);
}
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) MethodParameter(org.springframework.core.MethodParameter) SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)224 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)80 Test (org.junit.jupiter.api.Test)79 MethodParameter (org.springframework.core.MethodParameter)50 BeforeEach (org.junit.jupiter.api.BeforeEach)41 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)32 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)30 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)28 Method (java.lang.reflect.Method)21 Test (org.junit.Test)21 MockMultipartHttpServletRequest (org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest)21 MockMultipartFile (org.springframework.web.testfixture.servlet.MockMultipartFile)18 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)14 TestBean (org.springframework.beans.testfixture.beans.TestBean)14 RequestParam (org.springframework.web.bind.annotation.RequestParam)14 MockPart (org.springframework.web.testfixture.servlet.MockPart)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 MultipartFile (org.springframework.web.multipart.MultipartFile)13 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)11 IOException (java.io.IOException)10