diff --git a/hippo4j-common/pom.xml b/hippo4j-common/pom.xml
index e5be28cf..aeff902a 100644
--- a/hippo4j-common/pom.xml
+++ b/hippo4j-common/pom.xml
@@ -77,5 +77,10 @@
org.springframework.boot
spring-boot-configuration-processor
+
+ org.apache.tomcat.embed
+ tomcat-embed-core
+ test
+
diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HomeServlet.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HomeServlet.java
new file mode 100644
index 00000000..1f377b0b
--- /dev/null
+++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HomeServlet.java
@@ -0,0 +1,42 @@
+/*
+ * 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 cn.hippo4j.common.toolkit.http;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * Implement HttpServlet and receive post and get requests
+ * This HttpServlet represents the home page
+ */
+public class HomeServlet extends HttpServlet {
+
+ int status = 200;
+ String result = "success";
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ resp.setStatus(status);
+ PrintWriter writer = resp.getWriter();
+ writer.println(result);
+ }
+
+}
diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java
index 55eb6675..fa6a1a6f 100644
--- a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java
+++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/HttpUtilsTest.java
@@ -20,36 +20,106 @@ package cn.hippo4j.common.toolkit.http;
import cn.hippo4j.common.toolkit.JSONUtil;
import lombok.Getter;
import lombok.Setter;
+import org.apache.catalina.Context;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.connector.Connector;
+import org.apache.catalina.startup.Tomcat;
+import org.junit.AfterClass;
import org.junit.Assert;
+import org.junit.BeforeClass;
import org.junit.Test;
+import java.io.File;
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.Map;
public class HttpUtilsTest {
- /**
- * test post url
- */
- static String postUrl = "http://console.hippo4j.cn/hippo4j/v1/cs/";
+ static int PORT = 8080;
+ static Tomcat tomcat;
+ static String PROTOCOL = "org.apache.coyote.http11.Http11NioProtocol";
+ static final String HOME_PAGE_URL = "/home";
+ static final String HOME_PAGE_NAME = "homeServlet";
+ static final String LOGIN_URL = "/login";
+ static final String LOGIN_NAME = "loginServlet";
+ static final String CONTEXT_PATH = "/";
+ static final String PATH_NAME = ".";
+
+ @BeforeClass
+ public static void startWeb() throws IOException, LifecycleException {
+ tomcat = new Tomcat();
+ // get a random port
+ ServerSocket socket = new ServerSocket(0);
+ PORT = socket.getLocalPort();
+ socket.close();
+ tomcat.setPort(PORT);
+ Connector connector = new Connector(PROTOCOL);
+ connector.setThrowOnFailure(true);
+ connector.setPort(PORT);
+ tomcat.setConnector(connector);
+ String absolutePath = new File(PATH_NAME).getAbsolutePath();
+ Context context = tomcat.addContext(CONTEXT_PATH, absolutePath);
+ Tomcat.addServlet(context, HOME_PAGE_NAME, new HomeServlet()).setAsyncSupported(true);
+ context.addServletMappingDecoded(HOME_PAGE_URL, HOME_PAGE_NAME);
+ Tomcat.addServlet(context, LOGIN_NAME, new LoginServlet()).setAsyncSupported(true);
+ context.addServletMappingDecoded(LOGIN_URL, LOGIN_NAME);
+ tomcat.start();
+ }
+
+ @AfterClass
+ public static void stopWeb() throws LifecycleException, IOException {
+ // stop tomcat
+ tomcat.stop();
+ // del dir
+ String userUrl = System.getProperty("user.dir");
+ File file = new File(userUrl + "\\tomcat." + PORT);
+ Files.walkFileTree(file.toPath(), new SimpleFileVisitor() {
+
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
+ Files.delete(dir);
+ return FileVisitResult.CONTINUE;
+ }
+
+ });
+ }
/**
- * test get url
+ * test url
*/
- static String getUrl = "https://hippo4j.cn/";
+ String url = "http://localhost:";
+
+ String passwordValue = "hippo4jtest";
+ String usernameValue = "hippo4j";
+ String password = "password";
+ String username = "username";
+ String suffix = "?password=hippo4jtest&username=hippo4j";
@Test
public void get() {
- String s = HttpUtil.get(getUrl);
+ String s = HttpUtil.get(url + PORT + HOME_PAGE_URL);
Assert.assertNotNull(s);
}
@Test
public void restApiPost() {
- String loginUrl = postUrl + "auth/login";
+ String loginUrl = url + PORT + LOGIN_URL;
LoginInfo loginInfo = new LoginInfo();
- loginInfo.setPassword("hippo4jtest");
- loginInfo.setUsername("hippo4j");
+ loginInfo.setPassword(passwordValue);
+ loginInfo.setUsername(usernameValue);
loginInfo.setRememberMe(1);
String s = HttpUtil.post(loginUrl, loginInfo);
Result result = JSONUtil.parseObject(s, Result.class);
@@ -60,10 +130,10 @@ public class HttpUtilsTest {
@Test
public void testRestApiPost() {
- String loginUrl = postUrl + "auth/login";
+ String loginUrl = url + PORT + LOGIN_URL;
LoginInfo loginInfo = new LoginInfo();
- loginInfo.setPassword("hippo4jtest");
- loginInfo.setUsername("hippo4j");
+ loginInfo.setPassword(passwordValue);
+ loginInfo.setUsername(usernameValue);
loginInfo.setRememberMe(1);
Result result = HttpUtil.post(loginUrl, loginInfo, Result.class);
Assert.assertNotNull(result);
@@ -73,10 +143,10 @@ public class HttpUtilsTest {
// @Test(expected = SocketTimeoutException.class)
public void testRestApiPostTimeout() {
- String loginUrl = postUrl + "auth/login";
+ String loginUrl = url + PORT + LOGIN_URL;
LoginInfo loginInfo = new LoginInfo();
- loginInfo.setPassword("hippo4jtest");
- loginInfo.setUsername("hippo4j");
+ loginInfo.setPassword(passwordValue);
+ loginInfo.setUsername(usernameValue);
loginInfo.setRememberMe(1);
HttpUtil.post(loginUrl, loginInfo, 1, Result.class);
}
@@ -84,15 +154,15 @@ public class HttpUtilsTest {
@Test
public void buildUrl() {
Map map = new HashMap<>();
- map.put("password", "hippo4jtest");
- map.put("username", "hippo4j");
- String s = HttpUtil.buildUrl(getUrl, map);
- Assert.assertEquals(getUrl + "?password=hippo4jtest&username=hippo4j", s);
+ map.put(password, passwordValue);
+ map.put(username, usernameValue);
+ String s = HttpUtil.buildUrl(url + PORT, map);
+ Assert.assertEquals(url + PORT + suffix, s);
}
@Getter
@Setter
- private static class LoginInfo {
+ protected static class LoginInfo {
private String username;
@@ -103,7 +173,7 @@ public class HttpUtilsTest {
@Getter
@Setter
- private static class Result {
+ protected static class Result {
private String code;
@@ -112,7 +182,7 @@ public class HttpUtilsTest {
@Getter
@Setter
- private static class ResultData {
+ protected static class ResultData {
private String data;
diff --git a/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/LoginServlet.java b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/LoginServlet.java
new file mode 100644
index 00000000..9c085fd4
--- /dev/null
+++ b/hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/http/LoginServlet.java
@@ -0,0 +1,50 @@
+/*
+ * 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 cn.hippo4j.common.toolkit.http;
+
+import cn.hippo4j.common.toolkit.JSONUtil;
+
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
+
+/**
+ * his HttpServlet represents the login request
+ */
+public class LoginServlet extends HttpServlet {
+
+ String passwordAttr = "password";
+ String usernameAttr = "username";
+ String status = "200";
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ String password = (String) req.getAttribute(passwordAttr);
+ String username = (String) req.getAttribute(usernameAttr);
+ HttpUtilsTest.ResultData resultData = new HttpUtilsTest.ResultData();
+ resultData.setData(username + password);
+ HttpUtilsTest.Result result = new HttpUtilsTest.Result();
+ result.setCode(status);
+ result.setData(resultData);
+ String s = JSONUtil.toJSONString(result);
+ PrintWriter writer = resp.getWriter();
+ writer.println(s);
+ }
+}