|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hive.service; |
| 20 | + |
| 21 | +import org.apache.hadoop.hive.conf.HiveConf; |
| 22 | +import org.apache.hadoop.hive.metastore.conf.MetastoreConf; |
| 23 | +import org.apache.hc.client5.http.classic.methods.HttpGet; |
| 24 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; |
| 25 | +import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; |
| 26 | +import org.apache.hc.client5.http.impl.classic.HttpClients; |
| 27 | +import org.apache.hc.core5.http.Header; |
| 28 | +import org.apache.hive.jdbc.miniHS2.MiniHS2; |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.HashMap; |
| 36 | + |
| 37 | +public class TestHttpServices { |
| 38 | + |
| 39 | + private static MiniHS2 miniHS2 = null; |
| 40 | + |
| 41 | + @BeforeClass |
| 42 | + public static void startServices() throws Exception { |
| 43 | + HiveConf hiveConf = new HiveConf(); |
| 44 | + hiveConf.set(MetastoreConf.ConfVars.THRIFT_TRANSPORT_MODE.toString(), "http"); // HS2 -> HMS thrift on http |
| 45 | + |
| 46 | + miniHS2 = new MiniHS2.Builder() |
| 47 | + .withConf(hiveConf) |
| 48 | + .withHTTPTransport() // Cli service -> HS2 thrift on http |
| 49 | + .withRemoteMetastore() |
| 50 | + .build(); |
| 51 | + |
| 52 | + miniHS2.start(new HashMap<>()); |
| 53 | + } |
| 54 | + |
| 55 | + @AfterClass |
| 56 | + public static void stopServices() { |
| 57 | + if (miniHS2 != null && miniHS2.isStarted()) { |
| 58 | + miniHS2.stop(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testWebUIResponseDoesNotContainServerVersionAndXPoweredBy() throws Exception { |
| 64 | + testHttpServiceDoesNotContainServerVersionAndXPoweredBy( |
| 65 | + "http://" + miniHS2.getHost() + ":" + miniHS2.getWebPort()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testCliServiceResponseDoesNotContainServerVersionAndXPoweredBy() throws Exception { |
| 70 | + testHttpServiceDoesNotContainServerVersionAndXPoweredBy( |
| 71 | + "http://" + miniHS2.getHost() + ":" + miniHS2.getWebPort() + "/cliservice"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testHMSServiceResponseDoesNotContainServerVersionAndXPoweredBy() throws Exception { |
| 76 | + testHttpServiceDoesNotContainServerVersionAndXPoweredBy( |
| 77 | + "http://" + miniHS2.getHost() + ":" + miniHS2.getWebPort() + "/" + |
| 78 | + MetastoreConf.ConfVars.METASTORE_CLIENT_THRIFT_HTTP_PATH.getDefaultVal()); |
| 79 | + } |
| 80 | + |
| 81 | + private void testHttpServiceDoesNotContainServerVersionAndXPoweredBy(String miniHS2) throws IOException { |
| 82 | + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { |
| 83 | + HttpGet request = new HttpGet(miniHS2); |
| 84 | + |
| 85 | + try (CloseableHttpResponse response = httpClient.execute(request)) { |
| 86 | + for (Header header : response.getHeaders()) { |
| 87 | + Assert.assertNotEquals("x-powered-by", header.getName().toLowerCase()); |
| 88 | + Assert.assertNotEquals("server", header.getName().toLowerCase()); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments