Search in sources :

Example 11 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project jersey by jersey.

the class MultiPartWebAppTest method testPartWithFileName.

@Test
public void testPartWithFileName() {
    final WebTarget target = target().path("form/part-file-name");
    final FormDataMultiPart mp = new FormDataMultiPart();
    final FormDataBodyPart p = new FormDataBodyPart(FormDataContentDisposition.name("part").fileName("file").build(), "CONTENT");
    mp.bodyPart(p);
    final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
    assertEquals("CONTENT:file", s);
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 12 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method testInjectedFileNotCopied.

/**
     * JERSEY-2663 reproducer. Make sure that temporary file created by MIMEPull is not copied into new temporary file created
     * by Jersey.
     */
@Test
public void testInjectedFileNotCopied(@Mocked final BodyPartEntity entity) throws Exception {
    final FormDataMultiPart multipart = new FormDataMultiPart();
    final FormDataBodyPart bodypart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(), "CONTENT");
    multipart.bodyPart(bodypart);
    final Response response = target().path("FileResource").path("InjectedFileNotCopied").request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA));
    // Make sure that the Mimepull temp file has been moved to specific file.
    new Verifications() {

        {
            entity.moveTo(withInstanceOf(File.class));
            times = 1;
        }
    };
    // Make sure that the temp file has been removed.
    final String pathname = response.readEntity(String.class);
    // Wait a second to make sure the file doesn't exist.
    Thread.sleep(1000);
    assertThat("Temporary file, " + pathname + ", on the server has not been removed", new File(pathname).exists(), is(false));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) Verifications(mockit.Verifications) File(java.io.File) Test(org.junit.Test)

Example 13 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method tempFileDeletedAfterExceptionInMethod.

/**
     * JERSEY-2846 reproducer. Make sure that temporary file created by MIMEPull deleted after an unsuccessful request.
     */
@Test
public void tempFileDeletedAfterExceptionInMethod() throws Exception {
    final FormDataMultiPart multipart = new FormDataMultiPart();
    final FormDataBodyPart bodypart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(), "CONTENT");
    multipart.bodyPart(bodypart);
    final Response response = target().path("FileResource").path("ExceptionInMethod").request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA));
    // Make sure that the temp file has been removed.
    final String pathname = response.readEntity(String.class);
    // Wait a second to make sure the file doesn't exist.
    Thread.sleep(1000);
    assertThat("Temporary file, " + pathname + ", on the server has not been removed", new File(pathname).exists(), is(false));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) File(java.io.File) Test(org.junit.Test)

Example 14 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method tempFileDeletedAfterSuccessfulProcessing.

/**
     * JERSEY-2846 reproducer. Make sure that temporary file created by MIMEPull deleted after a successful request.
     */
@Test
public void tempFileDeletedAfterSuccessfulProcessing() throws Exception {
    final FormDataMultiPart multipart = new FormDataMultiPart();
    final FormDataBodyPart bodypart = new FormDataBodyPart(FormDataContentDisposition.name("file").fileName("file").build(), "CONTENT");
    multipart.bodyPart(bodypart);
    final Response response = target().path("FileResource").path("SuccessfulMethod").request().post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA));
    // Make sure that the temp file has been removed.
    final String pathname = response.readEntity(String.class);
    // Wait a second to make sure the file doesn't exist.
    Thread.sleep(1000);
    assertThat("Temporary file, " + pathname + ", on the server has not been removed", new File(pathname).exists(), is(false));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) File(java.io.File) Test(org.junit.Test)

Example 15 with FormDataBodyPart

use of org.glassfish.jersey.media.multipart.FormDataBodyPart in project jersey by jersey.

the class MultipartTest method testSpecificListAsParameter.

/**
     * Test that injection of a list (specific type) works.
     */
@Test
public void testSpecificListAsParameter() throws Exception {
    final MyObject object = new MyObject("object");
    final List<MyObject> list = Arrays.asList(new MyObject("list1"), new MyObject("list2"));
    final FormDataMultiPart mp = new FormDataMultiPart();
    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("object").fileName("object").build(), object, MediaType.APPLICATION_JSON_TYPE));
    mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("list").fileName("list").build(), list, MediaType.APPLICATION_JSON_TYPE));
    final Response response = target("listAsParameter").request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE));
    assertThat(response.readEntity(String.class), is("object_list1_list2"));
}
Also used : Response(javax.ws.rs.core.Response) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Aggregations

FormDataBodyPart (org.glassfish.jersey.media.multipart.FormDataBodyPart)15 FormDataMultiPart (org.glassfish.jersey.media.multipart.FormDataMultiPart)15 Test (org.junit.Test)13 Response (javax.ws.rs.core.Response)9 JerseyTest (org.glassfish.jersey.test.JerseyTest)6 WebTarget (javax.ws.rs.client.WebTarget)5 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)4 File (java.io.File)3 Client (javax.ws.rs.client.Client)3 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)2 ErrorMessage (io.dropwizard.jersey.errors.ErrorMessage)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BadRequestException (javax.ws.rs.BadRequestException)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Verifications (mockit.Verifications)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1 ClientResponse (org.glassfish.jersey.client.ClientResponse)1 BodyPart (org.glassfish.jersey.media.multipart.BodyPart)1