|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.transport.endpoints; |
| 21 | + |
| 22 | +import co.elastic.clients.elasticsearch.ElasticsearchClient; |
| 23 | +import co.elastic.clients.json.SimpleJsonpMapper; |
| 24 | +import co.elastic.clients.transport.rest_client.RestClientTransport; |
| 25 | +import com.sun.net.httpserver.HttpServer; |
| 26 | +import org.apache.http.HttpHost; |
| 27 | +import org.elasticsearch.client.RestClient; |
| 28 | +import org.junit.jupiter.api.AfterAll; |
| 29 | +import org.junit.jupiter.api.Assertions; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import java.io.ByteArrayOutputStream; |
| 34 | +import java.io.IOException; |
| 35 | +import java.io.InputStream; |
| 36 | +import java.net.InetAddress; |
| 37 | +import java.net.InetSocketAddress; |
| 38 | +import java.nio.charset.StandardCharsets; |
| 39 | + |
| 40 | +public class BinaryEndpointTest extends Assertions { |
| 41 | + |
| 42 | + /** Collected headers by test name */ |
| 43 | + private static HttpServer httpServer; |
| 44 | + |
| 45 | + @BeforeAll |
| 46 | + public static void setup() throws IOException { |
| 47 | + httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0); |
| 48 | + |
| 49 | + // Register a handler on the core.exists("capture-handler/{name}") endpoint that will capture request headers. |
| 50 | + httpServer.createContext("/foo/_mvt/bar/1/0/0", exchange -> { |
| 51 | + // Reply with an empty 200 response |
| 52 | + exchange.getResponseHeaders().set("X-Elastic-Product", "Elasticsearch"); |
| 53 | + exchange.getResponseHeaders().set("Content-Type", "application/vnd.hello-world"); |
| 54 | + exchange.sendResponseHeaders(200, 0); |
| 55 | + exchange.getResponseBody().write("Hello world".getBytes(StandardCharsets.UTF_8)); |
| 56 | + exchange.close(); |
| 57 | + }); |
| 58 | + |
| 59 | + httpServer.start(); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterAll |
| 63 | + public static void cleanup() { |
| 64 | + httpServer.stop(0); |
| 65 | + httpServer = null; |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testMvtSearch() throws IOException { |
| 70 | + RestClient llrc = RestClient.builder( |
| 71 | + new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort(), "http") |
| 72 | + ).build(); |
| 73 | + |
| 74 | + RestClientTransport transport = new RestClientTransport(llrc, new SimpleJsonpMapper()); |
| 75 | + ElasticsearchClient esClient = new ElasticsearchClient(transport); |
| 76 | + |
| 77 | + BinaryResponse resp = esClient.searchMvt(s -> s |
| 78 | + .index("foo") |
| 79 | + .field("bar") |
| 80 | + .x(0) |
| 81 | + .y(0) |
| 82 | + .zoom(1) |
| 83 | + ); |
| 84 | + |
| 85 | + |
| 86 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 87 | + byte[] buffer = new byte[256]; |
| 88 | + |
| 89 | + try(InputStream input = resp.content()) { |
| 90 | + int len; |
| 91 | + while ((len = input.read(buffer)) > 0) { |
| 92 | + baos.write(buffer, 0, len); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + assertEquals("application/vnd.hello-world", resp.contentType()); |
| 97 | + assertEquals("Hello world", baos.toString(StandardCharsets.UTF_8.name())); |
| 98 | + } |
| 99 | +} |
0 commit comments