Search in sources :

Example 1 with LinkedMultiValueMap

use of cn.taketoday.core.LinkedMultiValueMap in project today-infrastructure by TAKETODAY.

the class TomcatServletWebServerFactoryTests method nonExistentUploadDirectoryIsCreatedUponMultipartUpload.

@Test
void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() throws IOException, URISyntaxException {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
    AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
    factory.addInitializers((servletContext) -> {
        servletContextReference.set(servletContext);
        Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {

            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                req.getParts();
            }
        });
        servlet.addMapping("/upload");
        servlet.setMultipartConfig(new MultipartConfigElement((String) null));
    });
    this.webServer = factory.getWebServer();
    this.webServer.start();
    File temp = (File) servletContextReference.get().getAttribute(ServletContext.TEMPDIR);
    FileSystemUtils.deleteRecursively(temp);
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = HttpHeaders.create();
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    ResponseEntity<String> response = restTemplate.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) HttpEntity(cn.taketoday.http.HttpEntity) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) RestTemplate(cn.taketoday.web.client.RestTemplate) ServletContext(jakarta.servlet.ServletContext) File(java.io.File) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 2 with LinkedMultiValueMap

use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.

the class DefaultClientResponseTests method cookies.

@Test
public void cookies() {
    ResponseCookie cookie = ResponseCookie.from("foo", "bar").build();
    MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
    cookies.add("foo", cookie);
    given(mockResponse.getCookies()).willReturn(cookies);
    assertThat(defaultClientResponse.cookies()).isSameAs(cookies);
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) ResponseCookie(cn.taketoday.http.ResponseCookie) Test(org.junit.jupiter.api.Test)

Example 3 with LinkedMultiValueMap

use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.

the class BodyInsertersTests method fromFormDataMap.

@Test
public void fromFormDataMap() {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData(body);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        DataBufferUtils.release(dataBuffer);
        assertThat(resultBytes).isEqualTo("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8));
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) ClientHttpRequest(cn.taketoday.http.client.reactive.ClientHttpRequest) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 4 with LinkedMultiValueMap

use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfString.

@Test
public void resolveMultiValueMapOfString() throws Throwable {
    String name = "foo";
    String value1 = "bar";
    String value2 = "baz";
    request.addParameter(name, value1, value2);
    MultiValueMap<String, String> expected = new LinkedMultiValueMap<>(1);
    expected.add(name, value1);
    expected.add(name, value2);
    ResolvableMethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class, String.class, String.class);
    Object result = resolver.resolveArgument(webRequest, param);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    assertThat(result).as("Invalid result").isEqualTo(expected);
}
Also used : RequestParam(cn.taketoday.web.annotation.RequestParam) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 5 with LinkedMultiValueMap

use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.

the class TomcatServletWebServerFactoryTests method nonExistentUploadDirectoryIsCreatedUponMultipartUpload.

@Test
void nonExistentUploadDirectoryIsCreatedUponMultipartUpload() throws IOException, URISyntaxException {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(0);
    AtomicReference<ServletContext> servletContextReference = new AtomicReference<>();
    factory.addInitializers((servletContext) -> {
        servletContextReference.set(servletContext);
        Dynamic servlet = servletContext.addServlet("upload", new HttpServlet() {

            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                req.getParts();
            }
        });
        servlet.addMapping("/upload");
        servlet.setMultipartConfig(new MultipartConfigElement((String) null));
    });
    this.webServer = factory.getWebServer();
    this.webServer.start();
    File temp = (File) servletContextReference.get().getAttribute(ServletContext.TEMPDIR);
    FileSystemUtils.deleteRecursively(temp);
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = HttpHeaders.create();
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    body.add("file", new ByteArrayResource(new byte[1024 * 1024]));
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
    ResponseEntity<String> response = restTemplate.postForEntity(getLocalUrl("/upload"), requestEntity, String.class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) Dynamic(jakarta.servlet.ServletRegistration.Dynamic) HttpEntity(cn.taketoday.http.HttpEntity) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) HttpServlet(jakarta.servlet.http.HttpServlet) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteArrayResource(cn.taketoday.core.io.ByteArrayResource) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletException(jakarta.servlet.ServletException) MultipartConfigElement(jakarta.servlet.MultipartConfigElement) RestTemplate(cn.taketoday.web.client.RestTemplate) ServletContext(jakarta.servlet.ServletContext) File(java.io.File) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)14 Test (org.junit.jupiter.api.Test)12 MultiValueMap (cn.taketoday.core.MultiValueMap)6 MockClientHttpRequest (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest)6 ServletException (jakarta.servlet.ServletException)4 IOException (java.io.IOException)4 ByteArrayResource (cn.taketoday.core.io.ByteArrayResource)2 ContentDisposition (cn.taketoday.http.ContentDisposition)2 HttpEntity (cn.taketoday.http.HttpEntity)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 ResponseCookie (cn.taketoday.http.ResponseCookie)2 ClientHttpRequest (cn.taketoday.http.client.reactive.ClientHttpRequest)2 MultipartException (cn.taketoday.web.bind.MultipartException)2 NotMultipartRequestException (cn.taketoday.web.bind.NotMultipartRequestException)2 RestTemplate (cn.taketoday.web.client.RestTemplate)2 MaxUploadSizeExceededException (cn.taketoday.web.multipart.MaxUploadSizeExceededException)2 MultipartFile (cn.taketoday.web.multipart.MultipartFile)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)2 ServletContext (jakarta.servlet.ServletContext)2 Dynamic (jakarta.servlet.ServletRegistration.Dynamic)2