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);
}
});
}
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");
});
}
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);
}
Aggregations