From d307a95f37b005cba258c52156158a012516ab77 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 20 Apr 2023 21:27:34 +0800 Subject: [PATCH] add tes --- .../java/cn/hutool/http/Issue3074Test.java | 28 ++++++ .../{ => cn/hutool/http}/IssueI6RE7JTest.java | 4 +- .../hutool/http/server/SimpleServerTest.java | 92 ++++++++++--------- 3 files changed, 78 insertions(+), 46 deletions(-) create mode 100644 hutool-http/src/test/java/cn/hutool/http/Issue3074Test.java rename hutool-http/src/test/java/{ => cn/hutool/http}/IssueI6RE7JTest.java (93%) diff --git a/hutool-http/src/test/java/cn/hutool/http/Issue3074Test.java b/hutool-http/src/test/java/cn/hutool/http/Issue3074Test.java new file mode 100644 index 000000000..8afc6ed63 --- /dev/null +++ b/hutool-http/src/test/java/cn/hutool/http/Issue3074Test.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package cn.hutool.http; + +import org.junit.Ignore; +import org.junit.Test; + +public class Issue3074Test { + + @Test + @Ignore + public void bodyTest() { + HttpUtil.createPost("http://localhost:8888/body") + .contentType(ContentType.JSON.getValue()) + .body("aaa") + .execute(); + } +} diff --git a/hutool-http/src/test/java/IssueI6RE7JTest.java b/hutool-http/src/test/java/cn/hutool/http/IssueI6RE7JTest.java similarity index 93% rename from hutool-http/src/test/java/IssueI6RE7JTest.java rename to hutool-http/src/test/java/cn/hutool/http/IssueI6RE7JTest.java index cc1e9b6ba..d7c8d7b9e 100644 --- a/hutool-http/src/test/java/IssueI6RE7JTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/IssueI6RE7JTest.java @@ -10,8 +10,8 @@ * See the Mulan PSL v2 for more details. */ -import cn.hutool.http.HttpGlobalConfig; -import cn.hutool.http.HttpUtil; +package cn.hutool.http; + import org.junit.Ignore; import org.junit.Test; diff --git a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java index 55a1ea51b..beecc12d5 100644 --- a/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java +++ b/hutool-http/src/test/java/cn/hutool/http/server/SimpleServerTest.java @@ -11,50 +11,54 @@ public class SimpleServerTest { public static void main(String[] args) { HttpUtil.createServer(8888) - .addFilter(((req, res, chain) -> { - Console.log("Filter: " + req.getPath()); - chain.doFilter(req.getHttpExchange()); - })) - // 设置默认根目录,classpath/html - .setRoot(FileUtil.file("html")) - // get数据测试,返回请求的PATH - .addAction("/get", (request, response) -> - response.write(request.getURI().toString(), ContentType.TEXT_PLAIN.toString()) - ) - // 返回JSON数据测试 - .addAction("/restTest", (request, response) -> { - String res = JSONUtil.createObj() - .set("id", 1) - .set("method", request.getMethod()) - .set("request", request.getBody()) - .toStringPretty(); - response.write(res, ContentType.JSON.toString()); - }) - // 获取表单数据测试 - // http://localhost:8888/formTest?a=1&a=2&b=3 - .addAction("/formTest", (request, response) -> - response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString()) - ) + .addFilter(((req, res, chain) -> { + Console.log("Filter: " + req.getPath()); + chain.doFilter(req.getHttpExchange()); + })) + // 设置默认根目录,classpath/html + .setRoot(FileUtil.file("html")) + // get数据测试,返回请求的PATH + .addAction("/get", (request, response) -> + response.write(request.getURI().toString(), ContentType.TEXT_PLAIN.toString()) + ) + // 返回JSON数据测试 + .addAction("/restTest", (request, response) -> { + String res = JSONUtil.createObj() + .set("id", 1) + .set("method", request.getMethod()) + .set("request", request.getBody()) + .toStringPretty(); + response.write(res, ContentType.JSON.toString()); + }) + // 获取表单数据测试 + // http://localhost:8888/formTest?a=1&a=2&b=3 + .addAction("/formTest", (request, response) -> + response.write(request.getParams().toString(), ContentType.TEXT_PLAIN.toString()) + ) - // 文件上传测试 - // http://localhost:8888/formForUpload.html - .addAction("/file", (request, response) -> { - Console.log("Upload file..."); - Console.log(request.getParams()); - final UploadFile[] files = request.getMultipart().getFiles("file"); - // 传入目录,默认读取HTTP头中的文件名然后创建文件 - for (UploadFile file : files) { - file.write("d:/test/"); - Console.log("Write file: d:/test/" + file.getFileName()); - } - response.write(request.getMultipart().getParamMap().toString(), ContentType.TEXT_PLAIN.toString()); - } - ) - // 测试输出响应内容是否能正常返回Content-Length头信息 - .addAction("test/zeroStr", (req, res)-> { - res.write("0"); - Console.log("Write 0 OK"); - }) - .start(); + // 文件上传测试 + // http://localhost:8888/formForUpload.html + .addAction("/file", (request, response) -> { + Console.log("Upload file..."); + Console.log(request.getParams()); + final UploadFile[] files = request.getMultipart().getFiles("file"); + // 传入目录,默认读取HTTP头中的文件名然后创建文件 + for (UploadFile file : files) { + file.write("d:/test/"); + Console.log("Write file: d:/test/" + file.getFileName()); + } + response.write(request.getMultipart().getParamMap().toString(), ContentType.TEXT_PLAIN.toString()); + } + ) + // 测试输出响应内容是否能正常返回Content-Length头信息 + .addAction("test/zeroStr", (req, res) -> { + res.write("0"); + Console.log("Write 0 OK"); + }) + .addAction("/body", (req, resp) -> { + Console.log(req.getBody()); + resp.write(req.getBody()); + }) + .start(); } }