FrankChen021 commented on code in PR #17937: URL: https://github.com/apache/druid/pull/17937#discussion_r2069908537
########## integration-tests/src/test/java/org/apache/druid/tests/query/ITSqlQueryTest.java: ########## @@ -0,0 +1,259 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.druid.tests.query; + +import com.google.inject.Inject; +import org.apache.druid.java.util.common.ISE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.testing.IntegrationTestingConfig; +import org.apache.druid.testing.guice.DruidTestModuleFactory; +import org.apache.druid.tests.TestNGGroup; +import org.apache.http.HttpEntity; +import org.apache.http.HttpStatus; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +/** + * Test the SQL endpoint with different Content-Type + */ +@Test(groups = {TestNGGroup.QUERY, TestNGGroup.CENTRALIZED_DATASOURCE_SCHEMA}) +@Guice(moduleFactory = DruidTestModuleFactory.class) +public class ITSqlQueryTest +{ + private static final Logger LOG = new Logger(ITSqlQueryTest.class); + + @Inject + IntegrationTestingConfig config; + + interface IExecutable + { + void execute(String endpoint) throws IOException; + } + + private void executeWithRetry(String contentType, IExecutable executable) + { + executeWithRetry(StringUtils.format("%s/druid/v2/sql/", config.getBrokerUrl()), contentType, executable); + executeWithRetry(StringUtils.format("%s/druid/v2/sql/", config.getRouterUrl()), contentType, executable); + } + + private void executeWithRetry(String endpoint, String contentType, IExecutable executable) + { + Throwable lastException = null; + for (int i = 1; i <= 5; i++) { + LOG.info("Query to %s with Content-Type = %s, tries = %s", endpoint, contentType, i); + try { + executable.execute(endpoint); + return; + } + catch (IOException e) { + // Only catch IOException + lastException = e; + } + try { + Thread.sleep(200); + } + catch (InterruptedException ignored) { + break; + } + } + throw new ISE(contentType + " failed after 5 tries, last exception: " + lastException); + } + + @Test + public void testUnsupportedMediaType() + { + executeWithRetry( + "<EMPTY>", + (endpoint) -> { + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + HttpPost request = new HttpPost(endpoint); + request.addHeader("Content-Type", "application/xml"); + request.setEntity(new StringEntity("select 1")); + + try (CloseableHttpResponse response = client.execute(request)) { + assertEquals(HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE, response.getStatusLine().getStatusCode()); + } + } + } + ); + } + + @Test + public void testTextPlain() + { + executeWithRetry( + "text/plain", + (endpoint) -> { + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + HttpPost request = new HttpPost(endpoint); + request.addHeader("Content-Type", "text/plain"); + request.setEntity(new StringEntity("select 1")); + + try (CloseableHttpResponse response = client.execute(request)) { + assertEquals(200, response.getStatusLine().getStatusCode()); + + HttpEntity entity = response.getEntity(); + assertNotNull(entity); + String responseBody = EntityUtils.toString(entity).trim(); + assertEquals("[{\"EXPR$0\":1}]", responseBody); + } + } + } + ); + } + + @Test + public void testFormURLEncoded() + { + executeWithRetry( + "application/x-www-form-urlencoded", + (endpoint) -> { + try (CloseableHttpClient client = HttpClientBuilder.create().build()) { + HttpPost request = new HttpPost(endpoint); + request.addHeader("Content-Type", "application/x-www-form-urlencoded"); Review Comment: the following line, which is at line 142, sends queries in URL-encoded way ```java request.setEntity(new StringEntity(URLEncoder.encode("select 1", StandardCharsets.UTF_8))); ``` I can add one more test which contains illegal encoded queries -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
