Search in sources :

Example 71 with ServletWebRequest

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

the class ResponseEntityExceptionHandlerTests method setup.

@Before
public void setup() {
    this.servletRequest = new MockHttpServletRequest("GET", "/");
    this.servletResponse = new MockHttpServletResponse();
    this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
    this.exceptionHandlerSupport = new ApplicationExceptionHandler();
    this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 72 with ServletWebRequest

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

the class RequestResponseBodyMethodProcessorMockTests method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    stringMessageConverter = mock(HttpMessageConverter.class);
    given(stringMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    resourceMessageConverter = mock(HttpMessageConverter.class);
    given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    processor = new RequestResponseBodyMethodProcessor(Arrays.asList(stringMessageConverter, resourceMessageConverter));
    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.mock.web.test.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.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 73 with ServletWebRequest

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

the class RequestResponseBodyMethodProcessorTests method setup.

@Before
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.mock.web.test.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.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 74 with ServletWebRequest

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

the class RequestMappingHandlerAdapterIntegrationTests method setup.

@Before
public void setup() throws Exception {
    ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer();
    bindingInitializer.setValidator(new StubValidator());
    List<HandlerMethodArgumentResolver> customResolvers = new ArrayList<>();
    customResolvers.add(new ServletWebArgumentResolverAdapter(new ColorArgumentResolver()));
    GenericWebApplicationContext context = new GenericWebApplicationContext();
    context.refresh();
    handlerAdapter = new RequestMappingHandlerAdapter();
    handlerAdapter.setWebBindingInitializer(bindingInitializer);
    handlerAdapter.setCustomArgumentResolvers(customResolvers);
    handlerAdapter.setApplicationContext(context);
    handlerAdapter.setBeanFactory(context.getBeanFactory());
    handlerAdapter.afterPropertiesSet();
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
    request.setMethod("POST");
    // Expose request to the current thread (for SpEL expressions)
    RequestContextHolder.setRequestAttributes(new ServletWebRequest(request));
}
Also used : ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ArrayList(java.util.ArrayList) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 75 with ServletWebRequest

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileListNotPresent.

@Test
public void resolveOptionalMultipartFileListNotPresent() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertEquals("Invalid argument value", Optional.empty(), actualValue);
    actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertEquals("Invalid argument value", Optional.empty(), actualValue);
}
Also used : MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)114 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)76 Test (org.junit.Test)49 Before (org.junit.Before)45 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)29 MethodParameter (org.springframework.core.MethodParameter)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)25 Method (java.lang.reflect.Method)20 MockMultipartHttpServletRequest (org.springframework.mock.web.test.MockMultipartHttpServletRequest)16 MockMultipartFile (org.springframework.mock.web.test.MockMultipartFile)14 ITestBean (org.springframework.tests.sample.beans.ITestBean)13 TestBean (org.springframework.tests.sample.beans.TestBean)13 RequestParam (org.springframework.web.bind.annotation.RequestParam)10 MultipartFile (org.springframework.web.multipart.MultipartFile)10 MockPart (org.springframework.mock.web.test.MockPart)9 SynthesizingMethodParameter (org.springframework.core.annotation.SynthesizingMethodParameter)8 List (java.util.List)6 Optional (java.util.Optional)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)5 IOException (java.io.IOException)4