Search in sources :

Example 1 with KeyDataPair

use of com.gargoylesoftware.htmlunit.util.KeyDataPair in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileDataAsParameter.

// gh-27199
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileDataAsParameter() throws Exception {
    String data = "{}";
    KeyDataPair keyDataPair = new KeyDataPair("key", new File("test.json"), null, MimeType.APPLICATION_JSON, StandardCharsets.UTF_8);
    keyDataPair.setData(data.getBytes());
    webRequest.setRequestParameters(Collections.singletonList(keyDataPair));
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getParts()).hasSize(1);
    Part part = actualRequest.getPart("key");
    assertSoftly(softly -> {
        softly.assertThat(part).as("part").isNotNull();
        softly.assertThat(part.getName()).as("name").isEqualTo("key");
        softly.assertThat(part.getSubmittedFileName()).as("file name").isEqualTo("test.json");
        softly.assertThat(part.getContentType()).as("content type").isEqualTo(MimeType.APPLICATION_JSON);
        try {
            softly.assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).as("content").isEqualTo(data);
        } catch (IOException ex) {
            softly.fail("failed to get InputStream", ex);
        }
    });
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Part(jakarta.servlet.http.Part) IOException(java.io.IOException) File(java.io.File) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) Test(org.junit.jupiter.api.Test)

Example 2 with KeyDataPair

use of com.gargoylesoftware.htmlunit.util.KeyDataPair in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter.

// gh-26799
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception {
    webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null)));
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getParts()).hasSize(1);
    Part part = actualRequest.getPart("key");
    assertSoftly(softly -> {
        softly.assertThat(part).as("part").isNotNull();
        softly.assertThat(part.getName()).as("name").isEqualTo("key");
        softly.assertThat(part.getSize()).as("size").isEqualTo(0);
        try {
            softly.assertThat(part.getInputStream()).as("input stream").isEmpty();
        } catch (IOException ex) {
            softly.fail("failed to get InputStream", ex);
        }
        softly.assertThat(part.getSubmittedFileName()).as("filename").isEqualTo("");
        softly.assertThat(part.getContentType()).as("content-type").isEqualTo("application/octet-stream");
    });
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Part(jakarta.servlet.http.Part) IOException(java.io.IOException) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) Test(org.junit.jupiter.api.Test)

Example 3 with KeyDataPair

use of com.gargoylesoftware.htmlunit.util.KeyDataPair in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter.

// gh-24926
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter() throws Exception {
    webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(), "test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getParts()).hasSize(1);
    Part part = actualRequest.getPart("key");
    assertThat(part).isNotNull();
    assertThat(part.getName()).isEqualTo("key");
    assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("test file");
    assertThat(part.getSubmittedFileName()).isEqualTo("test.txt");
    assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Part(jakarta.servlet.http.Part) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Aggregations

KeyDataPair (com.gargoylesoftware.htmlunit.util.KeyDataPair)3 Part (jakarta.servlet.http.Part)3 Test (org.junit.jupiter.api.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 IOException (java.io.IOException)2 File (java.io.File)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1