Hi,
I'm having the same problem I guess using vertx 3.4.2:
Your call client.post(12345, "localhost", "/back/application", rep -> { with the callback seems to have been removed.
AFAIK, in 3.4.2 there's a sendStream, sendBuffer, sendForm, sendJson and a sendJsonObject, that are new but I can't find a way to simply test a multipart/formdata
with some form fields and a file upload.
One way or another, the context.fileUploads() method never returns an uploaded file.
The web-client docs mention:
| at the moment multipart files are not supported, it will likely be supported in a later revision of the API.
Does that mean in 3.4.2 the test below won't work at all and file-uploads can't be tested using the web-client?
My test-code is below. Am I missing something?
TIA
Ronald
Sorry had to delete and upload the message again - the code was truncated
@RunWith(VertxUnitRunner.class)
public class UploadHandlerTest extends AbstractBaseTest {
private static final Logger logger = LoggerFactory.getLogger(UploadHandlerTest.class);
private static HttpServer server;
@BeforeClass
public static void beforeClass(TestContext context) throws Exception {
// Start a simple webserver
server = vertx.createHttpServer();
Router mainRouter;
mainRouter = Router.router(vertx);
// Directory remains empty, write access is permitted
mainRouter.route().handler(BodyHandler.create().setUploadsDirectory("my-uploads"));
// Handle the upload
mainRouter.route(HttpMethod.POST, "/upload").handler(h -> {
HttpServerRequest request = h.request();
HttpServerResponse response = h.response().setChunked(true);
Set<FileUpload> fileUploads = h.fileUploads();
context.assertEquals(1, fileUploads.size()); // <-- Point of failure, fileUploads.size() == 0
});
server.requestHandler(mainRouter::accept).listen(8080);
}
@Test
public void testUpload(TestContext context) throws Exception {
Async async = context.async();
WebClient client = WebClient.create(vertx);
File srcFile = new File(UploadHandlerTest.class.getResource("testfile-1.pdf").toURI());
context.assertTrue(srcFile.exists());
Buffer buffer = getBuffer(srcFile);
client.request(HttpMethod.POST, 8080, "localhost", "/upload").
putHeader("accept", "application/json").
putHeader("content-type", "multipart/form-data; boundary=--MyBoundary").
sendBuffer(buffer, h -> {
if (h.failed()) {
context.fail(h.cause());
return;
}
HttpResponse<Buffer> response = h.result();
context.assertEquals(200, response.statusCode());
async.complete();
});
}
private Buffer getBuffer(File file) {
Buffer buffer = Buffer.buffer();
buffer.appendString("--MyBoundary\r\n");
buffer.appendString("Content-Disposition: form-data; name=\"myId\" \r\n");
buffer.appendString("\r\n");
buffer.appendString("1234");
buffer.appendString("\r\n");
buffer.appendString("--MyBoundary\r\n");
buffer.appendString("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n");
buffer.appendString("Content-Type: application/pdf\n\r\n");
buffer.appendString("Content-Transfer-Encoding: binary\r\n");
buffer.appendString("\r\n");
try {
buffer.appendBytes(Files.readAllBytes(Paths.get(file.toURI())));
buffer.appendString("\r\n");
} catch (Exception e) {
e.printStackTrace();
}
buffer.appendString("--MyBoundary\r\n");
return buffer;
}