aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client/src/test/java/ai/vespa/feed/client/impl/ApacheClusterTest.java
blob: 0c7d94c04ec87fac6fdccdbcc688b8a62ca01501 (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
72
73
74
package ai.vespa.feed.client.impl;

import ai.vespa.feed.client.FeedClientBuilder.Compression;
import ai.vespa.feed.client.HttpResponse;
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.zip.GZIPOutputStream;

import static com.github.tomakehurst.wiremock.client.WireMock.any;
import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ApacheClusterTest {

    @RegisterExtension
    final WireMockExtension server = new WireMockExtension();

    @Test
    void testClient() throws IOException, ExecutionException, InterruptedException, TimeoutException {
        for (Compression compression : Compression.values()) {
            try (ApacheCluster cluster = new ApacheCluster(new FeedClientBuilderImpl(List.of(URI.create("http://localhost:" + server.port())))
                                                                   .setCompression(compression))) {
                server.stubFor(any(anyUrl()))
                      .setResponse(okJson("{}").build());

                CompletableFuture<HttpResponse> vessel = new CompletableFuture<>();
                cluster.dispatch(new HttpRequest("POST",
                                                 "/path",
                                                 Map.of("name1", () -> "value1",
                                                        "name2", () -> "value2"),
                                                 "content".getBytes(UTF_8),
                                                 Duration.ofSeconds(1)),
                                 vessel);
                HttpResponse response = vessel.get(5, TimeUnit.SECONDS);
                assertEquals("{}", new String(response.body(), UTF_8));
                assertEquals(200, response.code());

                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                try (OutputStream zip = new GZIPOutputStream(buffer)) { zip.write("content".getBytes(UTF_8)); }
                server.verify(1, anyRequestedFor(anyUrl()));
                RequestPatternBuilder expected = postRequestedFor(urlEqualTo("/path")).withHeader("name1", equalTo("value1"))
                                                                      .withHeader("name2", equalTo("value2"))
                                                                      .withHeader("Content-Type", equalTo("application/json; charset=UTF-8"))
                                                                      .withRequestBody(equalTo("content"));
                expected = switch (compression) {
                    case auto, none -> expected.withoutHeader("Content-Encoding");
                    case gzip -> expected.withHeader("Content-Encoding", equalTo("gzip"));
                };
                server.verify(1, expected);
                server.resetRequests();
            }
        }
    }

}