Search in sources :

Example 41 with ServletWebRequest

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

the class RequestResponseBodyMethodProcessorTests method assertContentDisposition.

private void assertContentDisposition(RequestResponseBodyMethodProcessor processor, boolean expectContentDisposition, String requestURI, String comment) throws Exception {
    this.servletRequest.setRequestURI(requestURI);
    processor.handleReturnValue("body", this.returnTypeString, this.container, this.request);
    String header = servletResponse.getHeader("Content-Disposition");
    if (expectContentDisposition) {
        assertThat(header).as("Expected 'Content-Disposition' header. Use case: '" + comment + "'").isEqualTo("inline;filename=f.txt");
    } else {
        assertThat(header).as("Did not expect 'Content-Disposition' header. Use case: '" + comment + "'").isNull();
    }
    this.servletRequest = new MockHttpServletRequest();
    this.servletResponse = new MockHttpServletResponse();
    this.request = new ServletWebRequest(servletRequest, servletResponse);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 42 with ServletWebRequest

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

the class ReactiveTypeHandlerTests method resetRequest.

private void resetRequest() {
    this.servletRequest = new MockHttpServletRequest();
    this.servletResponse = new MockHttpServletResponse();
    this.webRequest = new ServletWebRequest(this.servletRequest, this.servletResponse);
    AsyncWebRequest webRequest = new StandardServletAsyncWebRequest(this.servletRequest, this.servletResponse);
    WebAsyncUtils.getAsyncManager(this.webRequest).setAsyncWebRequest(webRequest);
    this.servletRequest.setAsyncSupported(true);
}
Also used : StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) 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)

Example 43 with ServletWebRequest

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileList.

@Test
public void resolveOptionalMultipartFileList() throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
    request.addFile(expected);
    request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    boolean condition1 = actualValue instanceof Optional;
    assertThat(condition1).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
    actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
    assertThat(actualValue instanceof Optional).isTrue();
    assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
}
Also used : MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MockMultipartFile(org.springframework.web.testfixture.servlet.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) MockMultipartHttpServletRequest(org.springframework.web.testfixture.servlet.MockMultipartHttpServletRequest) Optional(java.util.Optional) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 44 with ServletWebRequest

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

the class RequestPartMethodArgumentResolverTests method resolveOptionalPartArgumentNotPresent.

@Test
public void resolveOptionalPartArgumentNotPresent() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    webRequest = new ServletWebRequest(request);
    Object actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
    assertThat(actualValue).as("Invalid argument value").isEqualTo(Optional.empty());
    actualValue = resolver.resolveArgument(optionalPart, null, webRequest, null);
    assertThat(actualValue).as("Invalid argument value").isEqualTo(Optional.empty());
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

Example 45 with ServletWebRequest

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

the class RequestPartMethodArgumentResolverTests method resolvePartArrayArgument.

@Test
public void resolvePartArrayArgument() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart part1 = new MockPart("requestPart", "Hello World 1".getBytes());
    MockPart part2 = new MockPart("requestPart", "Hello World 2".getBytes());
    request.addPart(part1);
    request.addPart(part2);
    request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramPartArray, null, webRequest, null);
    assertThat(result instanceof Part[]).isTrue();
    Part[] parts = (Part[]) result;
    assertThat(parts.length).isEqualTo(2);
    assertThat(part1).isEqualTo(parts[0]);
    assertThat(part2).isEqualTo(parts[1]);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.web.testfixture.servlet.MockPart) Part(jakarta.servlet.http.Part) MockPart(org.springframework.web.testfixture.servlet.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.jupiter.api.Test)

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