Search in sources :

Example 6 with MockMultipartHttpServletRequest

use of org.springframework.mock.web.MockMultipartHttpServletRequest in project spring-framework by spring-projects.

the class MockMultipartHttpServletRequestBuilderTests method addFileWithoutFilename.

// gh-26261, gh-26400
@Test
void addFileWithoutFilename() throws Exception {
    MockPart jsonPart = new MockPart("data", "{\"node\":\"node\"}".getBytes(UTF_8));
    jsonPart.getHeaders().setContentType(MediaType.APPLICATION_JSON);
    MockMultipartHttpServletRequest mockRequest = (MockMultipartHttpServletRequest) new MockMultipartHttpServletRequestBuilder("/upload").file(new MockMultipartFile("file", "Test".getBytes(UTF_8))).part(jsonPart).buildRequest(new MockServletContext());
    assertThat(mockRequest.getFileMap()).containsOnlyKeys("file");
    assertThat(mockRequest.getParameterMap()).hasSize(1);
    assertThat(mockRequest.getParameter("data")).isEqualTo("{\"node\":\"node\"}");
    assertThat(mockRequest.getParts()).extracting(Part::getName).containsExactly("data");
}
Also used : MockMultipartFile(org.springframework.mock.web.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) MockPart(org.springframework.mock.web.MockPart) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 7 with MockMultipartHttpServletRequest

use of org.springframework.mock.web.MockMultipartHttpServletRequest in project gocd by gocd.

the class ArtifactsControllerTest method shouldReturnBadRequestIfRequiredHeadersAreMissingOnACreateArtifactRequest.

@Test
public void shouldReturnBadRequestIfRequiredHeadersAreMissingOnACreateArtifactRequest() throws Exception {
    MultipartHttpServletRequest multipartHttpServletRequest = new MockMultipartHttpServletRequest();
    when(systemEnvironment.isApiSafeModeEnabled()).thenReturn(true);
    ModelAndView modelAndView = artifactsController.postArtifact("pipeline", "invalid-label", "stage", "stage-counter", "job-name", 3L, "file-path", 3, multipartHttpServletRequest);
    ResponseCodeView codeView = (ResponseCodeView) modelAndView.getView();
    assertThat(codeView.getStatusCode(), is(HttpServletResponse.SC_BAD_REQUEST));
    assertThat(codeView.getContent(), is("Missing required header 'Confirm'"));
}
Also used : MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) ResponseCodeView(com.thoughtworks.go.server.web.ResponseCodeView) ModelAndView(org.springframework.web.servlet.ModelAndView) MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 8 with MockMultipartHttpServletRequest

use of org.springframework.mock.web.MockMultipartHttpServletRequest in project spring-framework by spring-projects.

the class MockMultipartHttpServletRequestBuilder method createServletRequest.

/**
	 * Create a new {@link MockMultipartHttpServletRequest} based on the
	 * supplied {@code ServletContext} and the {@code MockMultipartFiles}
	 * added to this builder.
	 */
@Override
protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext);
    this.files.stream().forEach(request::addFile);
    this.parts.values().stream().flatMap(Collection::stream).forEach(request::addPart);
    if (!this.parts.isEmpty()) {
        new StandardMultipartHttpServletRequest(request).getMultiFileMap().values().stream().flatMap(Collection::stream).forEach(request::addFile);
    }
    return request;
}
Also used : MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) StandardMultipartHttpServletRequest(org.springframework.web.multipart.support.StandardMultipartHttpServletRequest)

Example 9 with MockMultipartHttpServletRequest

use of org.springframework.mock.web.MockMultipartHttpServletRequest in project iaf by ibissource.

the class StreamPipeTest method createMultipartHttpRequest.

private MockMultipartHttpServletRequest createMultipartHttpRequest(StreamPipe pipe, boolean addAntiVirusParts, boolean antiVirusLastPartFailed) throws Exception {
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.setBoundary("gc0p4Jq0M2Yt08jU534c0p");
    builder.addTextBody("string1", "<hello>test</hello>");
    URL url = ClassUtils.getResourceURL("/Documents/doc001.pdf");
    File file = new File(url.toURI());
    builder.addBinaryBody("file1", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
    if (addAntiVirusParts) {
        builder.addTextBody(pipe.getAntiVirusPartName(), pipe.getAntiVirusPassedMessage());
    }
    URL url2 = ClassUtils.getResourceURL("/Documents/doc002.pdf");
    File file2 = new File(url2.toURI());
    builder.addBinaryBody("file2", file2, ContentType.APPLICATION_OCTET_STREAM, file2.getName());
    if (addAntiVirusParts) {
        String antiVirusLastPartMessage;
        if (antiVirusLastPartFailed) {
            antiVirusLastPartMessage = "Fail";
            if (antiVirusLastPartMessage.equalsIgnoreCase(pipe.getAntiVirusPassedMessage())) {
                throw new Exception("fail message [" + antiVirusLastPartMessage + "] must differ from pass message [" + pipe.getAntiVirusPassedMessage() + "]");
            }
        } else {
            antiVirusLastPartMessage = pipe.getAntiVirusPassedMessage();
        }
        builder.addTextBody(pipe.getAntiVirusPartName(), antiVirusLastPartMessage);
    }
    ByteArrayOutputStream requestContent = new ByteArrayOutputStream();
    HttpEntity entity = builder.build();
    entity.writeTo(requestContent);
    request.setContent(requestContent.toByteArray());
    request.setContentType(entity.getContentType().getValue());
    return request;
}
Also used : MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) HttpEntity(org.apache.http.HttpEntity) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) URL(java.net.URL)

Example 10 with MockMultipartHttpServletRequest

use of org.springframework.mock.web.MockMultipartHttpServletRequest in project iaf by ibissource.

the class StreamPipeTest method doPipeHttpRequestTest.

@Test
public void doPipeHttpRequestTest() throws Exception {
    MockMultipartHttpServletRequest request = createMultipartHttpRequest();
    pipe.addParameter(createHttpRequestParameter(request, session));
    pipe.configure();
    pipe.start();
    PipeRunResult pipeRunResult = doPipe(pipe, "", session);
    assertEquals("success", pipeRunResult.getPipeForward().getName());
    String expectedResult = "<parts>" + "<part type=\"string\" name=\"string1\" sessionKey=\"part_string\" size=\"19\"/>" + "<part type=\"file\" name=\"doc001.pdf\" sessionKey=\"part_file\" size=\"26358\" mimeType=\"application/octet-stream\"/>" + "<part type=\"file\" name=\"doc002.pdf\" sessionKey=\"part_file2\" size=\"25879\" mimeType=\"application/octet-stream\"/>" + "</parts>";
    assertEquals(expectedResult, pipeRunResult.getResult().asString());
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) MockMultipartHttpServletRequest(org.springframework.mock.web.MockMultipartHttpServletRequest) Test(org.junit.Test)

Aggregations

MockMultipartHttpServletRequest (org.springframework.mock.web.MockMultipartHttpServletRequest)13 File (java.io.File)5 Test (org.junit.Test)5 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)4 Test (org.junit.jupiter.api.Test)4 MockMultipartFile (org.springframework.mock.web.MockMultipartFile)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ResponseCodeView (com.thoughtworks.go.server.web.ResponseCodeView)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)2 Parameter (nl.nn.adapterframework.parameters.Parameter)2 DomBuilderException (nl.nn.adapterframework.util.DomBuilderException)2 PostMethod (org.apache.commons.httpclient.methods.PostMethod)2