Search in sources :

Example 1 with MockPart

use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolvePartListMissing.

@Test
public void resolvePartListMissing() throws Throwable {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertThatExceptionOfType(MissingRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(webRequest, param));
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 2 with MockPart

use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolvePartNotAnnot.

@Test
public void resolvePartNotAnnot() throws Throwable {
    MockPart expected = new MockPart("part", "Hello World".getBytes());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof Part;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestPart(cn.taketoday.web.annotation.RequestPart) MvcAnnotationPredicates.requestPart(cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockPart

use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolvePartArrayMissing.

@Test
public void resolvePartArrayMissing() throws Throwable {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertThatExceptionOfType(MissingRequestPartException.class).isThrownBy(() -> resolver.resolveArgument(webRequest, param));
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestPart(cn.taketoday.web.annotation.RequestPart) MvcAnnotationPredicates.requestPart(cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) BindingContext(cn.taketoday.web.BindingContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockPart

use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.

the class RequestParamMethodArgumentResolverTests method resolvePartArray.

@Test
public void resolvePartArray() throws Throwable {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart expected1 = new MockPart("pfilearray", "Hello World 1".getBytes());
    MockPart expected2 = new MockPart("pfilearray", "Hello World 2".getBytes());
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(new MockPart("other", "Hello World 3".getBytes()));
    webRequest = new ServletRequestContext(null, request, null);
    webRequest.setBindingContext(new BindingContext());
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof Part[];
    assertThat(condition).isTrue();
    Part[] parts = (Part[]) result;
    assertThat(parts.length).isEqualTo(2);
    assertThat(expected1).isEqualTo(parts[0]);
    assertThat(expected2).isEqualTo(parts[1]);
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestPart(cn.taketoday.web.annotation.RequestPart) MvcAnnotationPredicates.requestPart(cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 5 with MockPart

use of cn.taketoday.web.testfixture.servlet.MockPart in project today-framework by TAKETODAY.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfPart.

@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfPart() throws Throwable {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType("multipart/form-data");
    Part expected1 = new MockPart("mfilelist", "Hello World 1".getBytes());
    Part expected2 = new MockPart("mfilelist", "Hello World 2".getBytes());
    Part expected3 = new MockPart("other", "Hello World 3".getBytes());
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(expected3);
    request.setMethod("POST");
    webRequest = new ServletRequestContext(null, request, null);
    ResolvableMethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, Part.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    MultiValueMap<String, Part> resultMap = (MultiValueMap<String, Part>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").get(0)).isEqualTo(expected1);
    assertThat(resultMap.get("mfilelist").get(1)).isEqualTo(expected2);
    assertThat(resultMap.get("other").size()).isEqualTo(1);
    assertThat(resultMap.get("other").get(0)).isEqualTo(expected3);
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) Part(jakarta.servlet.http.Part) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) MockPart(cn.taketoday.web.testfixture.servlet.MockPart) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)8 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)8 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)8 MockPart (cn.taketoday.web.testfixture.servlet.MockPart)8 Test (org.junit.jupiter.api.Test)8 BindingContext (cn.taketoday.web.BindingContext)6 RequestParam (cn.taketoday.web.annotation.RequestParam)6 Part (jakarta.servlet.http.Part)6 RequestPart (cn.taketoday.web.annotation.RequestPart)4 MvcAnnotationPredicates.requestPart (cn.taketoday.web.bind.resolver.MvcAnnotationPredicates.requestPart)4 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)1 List (java.util.List)1 Map (java.util.Map)1