summaryrefslogtreecommitdiffstats
path: root/hosted-api/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java
blob: a55c0d91cd3471645d176229b14739559a0a77b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
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"));
    }

}