From 284571485c4a78974da1acc6c592d718b245e564 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Wed, 24 Jan 2024 15:00:28 +0800 Subject: [PATCH] =?UTF-8?q?test(forest):=20=E4=B8=80=E7=82=B9=E5=B0=8F?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/forest/TestClient.java | 24 +++++++++++ .../service/forest/TestServer.java | 41 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java create mode 100644 service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java diff --git a/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java new file mode 100644 index 0000000..d99bee1 --- /dev/null +++ b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestClient.java @@ -0,0 +1,24 @@ +package com.lanyuanxiaoyao.service.forest; + +import com.dtflys.forest.Forest; +import com.dtflys.forest.annotation.Get; + +/** + * @author lanyuanxiaoyao + * @date 2024-01-24 + */ +public class TestClient { + public static void main(String[] args) { + TestService testService = Forest.client(TestService.class); + System.out.println(testService.success()); + System.out.println(testService.error()); + } + + public interface TestService { + @Get("http://localhost:8000/success") + String success(); + + @Get("http://localhost:8000/error") + String error(); + } +} diff --git a/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java new file mode 100644 index 0000000..6fca7a3 --- /dev/null +++ b/service-forest/src/test/java/com/lanyuanxiaoyao/service/forest/TestServer.java @@ -0,0 +1,41 @@ +package com.lanyuanxiaoyao.service.forest; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +/** + * @author lanyuanxiaoyao + * @date 2024-01-24 + */ +public class TestServer { + public static void main(String[] args) throws IOException { + HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0); + server.createContext("/success", new SuccessHandler()); + server.createContext("/error", new ErrorHandler()); + server.start(); + } + + public static class SuccessHandler implements HttpHandler { + @Override + public void handle(HttpExchange exchange) throws IOException { + exchange.sendResponseHeaders(200, 0); + OutputStream body = exchange.getResponseBody(); + body.write("Success".getBytes()); + body.close(); + } + } + + public static class ErrorHandler implements HttpHandler { + @Override + public void handle(HttpExchange exchange) throws IOException { + exchange.sendResponseHeaders(500, 0); + OutputStream body = exchange.getResponseBody(); + body.write("Error".getBytes()); + body.close(); + } + } +}