fix #I85C9S@Gitee

This commit is contained in:
Looly
2023-10-06 21:24:52 +08:00
parent 6c39380b6a
commit 74916a4c07
9 changed files with 181 additions and 72 deletions

View File

@@ -0,0 +1,35 @@
/*
* 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:
* https://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 org.dromara.hutool.http;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.MapBuilder;
import org.dromara.hutool.http.client.Request;
import org.dromara.hutool.http.client.Response;
import org.dromara.hutool.http.client.engine.httpclient4.HttpClient4Engine;
import org.dromara.hutool.http.meta.Method;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
public class IssueI85C9STest {
@Test
void getWithFormTest() {
final Response send = Request.of("http://localhost:8888/formTest")
.method(Method.GET)
.form(MapBuilder.of(new HashMap<String, Object>()).put("a", 1).put("b", 2).build())
.send(new HttpClient4Engine());
Console.log(send.bodyStr());
}
}

View File

@@ -27,56 +27,58 @@ public class SimpleServerTest {
public static void main(final 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) -> {
final String res = JSONUtil.ofObj()
.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) -> {
final String res = JSONUtil.ofObj()
.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) -> {
Console.log(request.getMethod());
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 (final 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("/getCookie", ((request, response) -> {
response.setHeader(HeaderName.SET_COOKIE.toString(),
ListUtil.of(
new HttpCookie("cc", "123").toString(),
new HttpCookie("cc", "abc").toString()));
response.write("Cookie 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 (final 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("/getCookie", ((request, response) -> {
response.setHeader(HeaderName.SET_COOKIE.toString(),
ListUtil.of(
new HttpCookie("cc", "123").toString(),
new HttpCookie("cc", "abc").toString()));
response.write("Cookie ok");
}))
.start();
}
}