From b2879c0e056b48c37d250b7b93cafdfba4621129 Mon Sep 17 00:00:00 2001 From: juzimao <578961953@qq.com> Date: Thu, 23 Jun 2022 10:46:50 +0800 Subject: [PATCH] [Fix] fix the HttpUtilsTest' test case , it is always time out. (#10294) --- dolphinscheduler-common/pom.xml | 7 ++ .../common/utils/LocalJettyHttpServer.java | 77 ++++++++++++++++++ .../utils/LocalServerHttpUtilsTest.java | 80 +++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalJettyHttpServer.java create mode 100644 dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalServerHttpUtilsTest.java diff --git a/dolphinscheduler-common/pom.xml b/dolphinscheduler-common/pom.xml index eb81c671f2..3b0ed2e5e7 100644 --- a/dolphinscheduler-common/pom.xml +++ b/dolphinscheduler-common/pom.xml @@ -293,5 +293,12 @@ io.netty netty-all + + + servlet-api + javax.servlet + test + + diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalJettyHttpServer.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalJettyHttpServer.java new file mode 100644 index 0000000000..c4ff8bfc13 --- /dev/null +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalJettyHttpServer.java @@ -0,0 +1,77 @@ +/* + * 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.dolphinscheduler.common.utils; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.BindException; +import junit.extensions.TestSetup; +import junit.framework.Test; +import org.mortbay.jetty.*; +import org.mortbay.jetty.handler.AbstractHandler; +import org.mortbay.jetty.handler.ContextHandler; +import org.mortbay.util.ByteArrayISO8859Writer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class LocalJettyHttpServer extends TestSetup { + protected static Server server; + private static Logger logger = LoggerFactory.getLogger(LocalJettyHttpServer.class); + private Integer serverPort = 0; + + public Integer getServerPort() { + return serverPort; + } + + public LocalJettyHttpServer(Test suite) { + super(suite); + } + + + protected void setUp() throws Exception { + server = new Server(serverPort); + ContextHandler context = new ContextHandler("/test.json"); + context.setHandler(new AbstractHandler() { + @Override + public void handle(String s, HttpServletRequest request, HttpServletResponse response, int i) throws IOException { + ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(); + writer.write("{\"name\":\"Github\"}"); + writer.flush(); + response.setContentLength(writer.size()); + OutputStream out = response.getOutputStream(); + writer.writeTo(out); + out.flush(); + Request baseRequest = request instanceof Request ? (Request) request : HttpConnection.getCurrentConnection().getRequest(); + baseRequest.setHandled(true); + } + }); + server.setHandler(context); + logger.info("server for " + context.getBaseResource()); + server.start(); + serverPort = server.getConnectors()[0].getLocalPort(); + logger.info("server is starting in port: " + serverPort); + } + + protected void tearDown() throws Exception { + logger.info("server stopping..."); + server.stop(); + } + +} diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalServerHttpUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalServerHttpUtilsTest.java new file mode 100644 index 0000000000..6352f5826f --- /dev/null +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/LocalServerHttpUtilsTest.java @@ -0,0 +1,80 @@ +/* + * 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.dolphinscheduler.common.utils; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import org.apache.dolphinscheduler.common.Constants; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.junit.Assert; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class LocalServerHttpUtilsTest extends TestCase{ + + public static final Logger logger = LoggerFactory.getLogger(LocalServerHttpUtilsTest.class); + private static LocalJettyHttpServer server = null; + public static Test suite(){ + TestSuite suite=new TestSuite(); + suite.addTestSuite(LocalServerHttpUtilsTest.class); + server = new LocalJettyHttpServer(suite); + return server; + } + + public void testGetTest() throws Exception { + // success + String result = null; + result = HttpUtils.get("http://localhost:" + server.getServerPort()+ "/test.json"); + Assert.assertNotNull(result); + ObjectNode jsonObject = JSONUtils.parseObject(result); + Assert.assertEquals("Github",jsonObject.path("name").asText()); + result = HttpUtils.get("http://123.333.111.33/ccc"); + Assert.assertNull(result); + } + + public void testGetResponseContentString() { + CloseableHttpClient httpclient = HttpClients.createDefault(); + HttpGet httpget = new HttpGet("http://localhost:" +server.getServerPort()+"/test.json"); + /** set timeout、request time、socket timeout */ + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(Constants.HTTP_CONNECT_TIMEOUT) + .setConnectionRequestTimeout(Constants.HTTP_CONNECTION_REQUEST_TIMEOUT) + .setSocketTimeout(Constants.SOCKET_TIMEOUT).setRedirectsEnabled(true).build(); + httpget.setConfig(requestConfig); + + String responseContent = null; + responseContent = HttpUtils.getResponseContentString(httpget, httpclient); + Assert.assertNotNull(responseContent); + + responseContent = HttpUtils.getResponseContentString(null, httpclient); + Assert.assertNull(responseContent); + + responseContent = HttpUtils.getResponseContentString(httpget, null); + Assert.assertNull(responseContent); + } + + public void testGetHttpClient() { + CloseableHttpClient httpClient1 = HttpUtils.getInstance(); + CloseableHttpClient httpClient2 = HttpUtils.getInstance(); + Assert.assertEquals(httpClient1, httpClient2); + } +}