From 9b2cccc9a3c3c919ed56c0bd65ca652a797bd4a1 Mon Sep 17 00:00:00 2001 From: Jon Marius Venstad Date: Mon, 29 Apr 2019 09:39:23 +0200 Subject: Move API things to new module: hosted-api --- .../ai/vespa/hosted/api/MultiPartStreamerTest.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 hosted-api/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java (limited to 'hosted-api/src/test') diff --git a/hosted-api/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java b/hosted-api/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java new file mode 100644 index 00000000000..d94a5b3314c --- /dev/null +++ b/hosted-api/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java @@ -0,0 +1,70 @@ +package ai.vespa.hosted.api; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpRequest; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class MultiPartStreamerTest { + + @Rule + public TemporaryFolder tmp = new TemporaryFolder(); + + @Test + public void test() throws IOException { + Path file = tmp.newFile().toPath(); + Files.write(file, new byte[]{0x48, 0x69}); + MultiPartStreamer streamer = new MultiPartStreamer("My boundary"); + + assertEquals("--My boundary--", + new String(streamer.data().readAllBytes())); + + streamer.addData("data", "uss/enterprise", "lore") + .addJson("json", "{\"xml\":false}") + .addText("text", "Hello!") + .addFile("file", file); + + String expected = "--My boundary\r\n" + + "Content-Disposition: form-data; name=\"data\"\r\n" + + "Content-Type: uss/enterprise\r\n" + + "\r\n" + + "lore\r\n" + + "--My boundary\r\n" + + "Content-Disposition: form-data; name=\"json\"\r\n" + + "Content-Type: application/json\r\n" + + "\r\n" + + "{\"xml\":false}\r\n" + + "--My boundary\r\n" + + "Content-Disposition: form-data; name=\"text\"\r\n" + + "Content-Type: text/plain\r\n" + + "\r\n" + + "Hello!\r\n" + + "--My boundary\r\n" + + "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getFileName() + "\"\r\n" + + "Content-Type: application/octet-stream\r\n" + + "\r\n" + + "Hi\r\n" + + "--My boundary--"; + + assertEquals(expected, + new String(streamer.data().readAllBytes())); + + // Verify that all data is read again for a new builder. + assertEquals(expected, + new String(streamer.data().readAllBytes())); + + assertEquals(List.of("multipart/form-data; boundary=My boundary; charset: utf-8"), + streamer.streamTo(HttpRequest.newBuilder(), Method.POST) + .uri(URI.create("https://uri/path")) + .build().headers().allValues("Content-Type")); + } + +} -- cgit v1.2.3