aboutsummaryrefslogtreecommitdiffstats
path: root/hosted-api/src/test/java/ai/vespa/hosted/api/MultiPartStreamerTest.java
blob: 19d3d3b8d6ff8947b58176edd1051a2d1a4a91c9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.hosted.api;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

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.jupiter.api.Assertions.assertEquals;

class MultiPartStreamerTest {

    @Test
    void test(@TempDir Path tmp) throws IOException {
        Path file = tmp.resolve("file");
        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
                          Content-Disposition: form-data; name="data"\r
                          Content-Type: uss/enterprise\r
                          \r
                          lore\r
                          --My boundary\r
                          Content-Disposition: form-data; name="json"\r
                          Content-Type: application/json\r
                          \r
                          {"xml":false}\r
                          --My boundary\r
                          Content-Disposition: form-data; name="text"\r
                          Content-Type: text/plain\r
                          \r
                          Hello!\r
                          --My boundary\r
                          Content-Disposition: form-data; name="file"; filename="%s"\r
                          Content-Type: application/octet-stream\r
                          \r
                          Hi\r
                          --My boundary--""".formatted(file.getFileName());

        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"));
    }

}