Search in sources :

Example 1 with MIMEParsingException

use of org.jvnet.mimepull.MIMEParsingException in project jersey by jersey.

the class FormDataMultiPartReaderWriterTest method mimeTempFileRemovedAfterAbortedUpload.

/**
     * Mocked JERSEY-2794 reproducer. Real test is under integration tests.
     */
@Test
public void mimeTempFileRemovedAfterAbortedUpload(@Mocked final MIMEMessage message) throws Exception {
    new Expectations() {

        {
            message.getAttachments();
            result = new MIMEParsingException();
        }
    };
    final URL url = new URL(getBaseUri().toString() + "MediaTypeWithBoundaryResource");
    final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("PUT");
    connection.setRequestProperty("Accept", "text/plain");
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=XXXX_YYYY");
    connection.setDoOutput(true);
    connection.connect();
    final OutputStream outputStream = connection.getOutputStream();
    outputStream.write("--XXXX_YYYY".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Type: text/plain".getBytes());
    outputStream.write('\n');
    outputStream.write("Content-Disposition: form-data; name=\"big-part\"".getBytes());
    outputStream.write('\n');
    outputStream.write('\n');
    // Send big chunk of data.
    for (int i = 0; i < 16 * 4096; i++) {
        outputStream.write('E');
        if (i % 1024 == 0) {
            outputStream.flush();
        }
    }
    // Do NOT send end of the MultiPart message to simulate the issue.
    // Get Response ...
    final int response = connection.getResponseCode();
    // ... Disconnect.
    connection.disconnect();
    assertThat("Bad Request expected", response, is(400));
    // Make sure that the Mimepull message and it's parts have been closed and temporary files deleted.
    new Verifications() {

        {
            message.close();
            times = 1;
        }
    };
}
Also used : Expectations(mockit.Expectations) HttpURLConnection(java.net.HttpURLConnection) MIMEParsingException(org.jvnet.mimepull.MIMEParsingException) OutputStream(java.io.OutputStream) Verifications(mockit.Verifications) URL(java.net.URL) Test(org.junit.Test)

Aggregations

OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 Expectations (mockit.Expectations)1 Verifications (mockit.Verifications)1 Test (org.junit.Test)1 MIMEParsingException (org.jvnet.mimepull.MIMEParsingException)1