|
| 1 | +/* |
| 2 | + * Copyright 2014 Red Hat, Inc. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Apache License v2.0 which accompanies this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * |
| 11 | + * The Apache License v2.0 is available at |
| 12 | + * http://www.opensource.org/licenses/apache2.0.php |
| 13 | + * |
| 14 | + * You may elect to redistribute this code under either of these licenses. |
| 15 | + */ |
| 16 | +package io.vertx.tests.http.fileupload; |
| 17 | + |
| 18 | +import io.netty.handler.codec.http.multipart.HttpPostRequestEncoder; |
| 19 | +import io.vertx.core.Vertx; |
| 20 | +import io.vertx.core.buffer.Buffer; |
| 21 | +import io.vertx.core.http.ClientForm; |
| 22 | +import io.vertx.core.http.ClientMultipartForm; |
| 23 | +import io.vertx.core.http.impl.ClientMultipartFormImpl; |
| 24 | +import io.vertx.core.http.impl.ClientMultipartFormUpload; |
| 25 | +import io.vertx.core.internal.ContextInternal; |
| 26 | +import io.vertx.core.internal.VertxInternal; |
| 27 | +import io.vertx.test.core.TestUtils; |
| 28 | +import io.vertx.test.http.HttpTestBase; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.ClassRule; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.rules.TemporaryFolder; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | + |
| 36 | +import java.io.File; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.Collections; |
| 40 | +import java.util.List; |
| 41 | +import java.util.concurrent.atomic.AtomicInteger; |
| 42 | + |
| 43 | +import static org.junit.Assume.assumeTrue; |
| 44 | + |
| 45 | +public class MultipartFormUploadTest extends HttpTestBase { |
| 46 | + |
| 47 | + @ClassRule |
| 48 | + public static TemporaryFolder testFolder = new TemporaryFolder(); |
| 49 | + |
| 50 | + private VertxInternal vertx; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() throws Exception { |
| 54 | + super.setUp(); |
| 55 | + vertx = (VertxInternal) Vertx.vertx(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testSimpleAttribute() throws Exception { |
| 60 | + Buffer result = Buffer.buffer(); |
| 61 | + ContextInternal context = vertx.getOrCreateContext(); |
| 62 | + ClientMultipartFormUpload upload = new ClientMultipartFormUpload(context, (ClientMultipartFormImpl) ClientForm.form().attribute("foo", "bar"), false, HttpPostRequestEncoder.EncoderMode.RFC1738); |
| 63 | + upload.endHandler(v -> { |
| 64 | + assertEquals("foo=bar", result.toString()); |
| 65 | + testComplete(); |
| 66 | + }); |
| 67 | + upload.handler(result::appendBuffer); |
| 68 | + upload.resume(); |
| 69 | + context.runOnContext(v -> upload.pump()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testFileUploadEventLoopContext() throws Exception { |
| 74 | + testFileUpload(vertx.createEventLoopContext(), false); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testFileUploadWorkerContext() throws Exception { |
| 79 | + testFileUpload(vertx.createWorkerContext(), false); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testFileUploadVirtualThreadContext() throws Exception { |
| 84 | + assumeTrue(vertx.isVirtualThreadAvailable()); |
| 85 | + testFileUpload(vertx.createVirtualThreadContext(), false); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testFileUploadPausedEventLoopContext() throws Exception { |
| 90 | + testFileUpload(vertx.createEventLoopContext(), true); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testFileUploadPausedWorkerContext() throws Exception { |
| 95 | + testFileUpload(vertx.createWorkerContext(), true); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testFileUploadPausedVirtualThreadContext() throws Exception { |
| 100 | + assumeTrue(vertx.isVirtualThreadAvailable()); |
| 101 | + testFileUpload(vertx.createVirtualThreadContext(), true); |
| 102 | + } |
| 103 | + |
| 104 | + private void testFileUpload(ContextInternal context, boolean paused) throws Exception { |
| 105 | + File file = testFolder.newFile(); |
| 106 | + Files.write(file.toPath(), TestUtils.randomByteArray(32 * 1024)); |
| 107 | + |
| 108 | + String filename = file.getName(); |
| 109 | + String pathname = file.getAbsolutePath(); |
| 110 | + |
| 111 | + context.runOnContext(v1 -> { |
| 112 | + try { |
| 113 | + ClientMultipartFormUpload upload = new ClientMultipartFormUpload(context, (ClientMultipartFormImpl) ClientMultipartForm |
| 114 | + .multipartForm() |
| 115 | + .textFileUpload("the-file", filename, "text/plain", pathname) |
| 116 | + , true, HttpPostRequestEncoder.EncoderMode.RFC1738); |
| 117 | + List<Buffer> buffers = Collections.synchronizedList(new ArrayList<>()); |
| 118 | + AtomicInteger end = new AtomicInteger(); |
| 119 | + upload.endHandler(v2 -> { |
| 120 | + assertEquals(0, end.getAndIncrement()); |
| 121 | + assertFalse(buffers.isEmpty()); |
| 122 | + testComplete(); |
| 123 | + }); |
| 124 | + upload.handler(buffer -> { |
| 125 | + assertEquals(0, end.get()); |
| 126 | + buffers.add(buffer); |
| 127 | + }); |
| 128 | + if (!paused) { |
| 129 | + upload.resume(); |
| 130 | + } |
| 131 | + upload.pump(); |
| 132 | + if (paused) { |
| 133 | + context.runOnContext(v3 -> upload.resume()); |
| 134 | + } |
| 135 | + } catch (Exception e) { |
| 136 | + fail(e); |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | +} |
0 commit comments