mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
clean history
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package cn.hutool.http.test;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.StreamProgress;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
/**
|
||||
* 下载单元测试
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class DownloadTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadPicTest() {
|
||||
String url = "http://wx.qlogo.cn/mmopen/vKhlFcibVUtNBVDjcIowlg0X8aJfHXrTNCEFBukWVH9ta99pfEN88lU39MKspCUCOP3yrFBH3y2NbV7sYtIIlon8XxLwAEqv2/0";
|
||||
HttpUtil.downloadFile(url, "e:/pic/t3.jpg");
|
||||
Console.log("ok");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadSizeTest() {
|
||||
String url = "https://res.t-io.org/im/upload/img/67/8948/1119501/88097554/74541310922/85/231910/366466 - 副本.jpg";
|
||||
HttpRequest.get(url).setSSLProtocol("TLSv1.2").executeAsync().writeBody("e:/pic/366466.jpg");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadTest1() {
|
||||
long size = HttpUtil.downloadFile("http://explorer.bbfriend.com/crossdomain.xml", "e:/temp/");
|
||||
System.out.println("Download size: " + size);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadTest() {
|
||||
// 带进度显示的文件下载
|
||||
HttpUtil.downloadFile("http://mirrors.sohu.com/centos/7/isos/x86_64/CentOS-7-x86_64-DVD-1810.iso", FileUtil.file("d:/"), new StreamProgress() {
|
||||
|
||||
long time = System.currentTimeMillis();
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
Console.log("开始下载。。。。");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void progress(long progressSize) {
|
||||
long speed = progressSize / (System.currentTimeMillis() - time) * 1000;
|
||||
Console.log("已下载:{}, 速度:{}/s", FileUtil.readableFileSize(progressSize), FileUtil.readableFileSize(speed));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
Console.log("下载完成!");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package cn.hutool.http.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.http.HtmlUtil;
|
||||
|
||||
/**
|
||||
* Html单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class HtmlUtilTest {
|
||||
|
||||
@Test
|
||||
public void removeHtmlTagTest() {
|
||||
//非闭合标签
|
||||
String str = "pre<img src=\"xxx/dfdsfds/test.jpg\">";
|
||||
String result = HtmlUtil.removeHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img>";
|
||||
result = HtmlUtil.removeHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img src=\"xxx/dfdsfds/test.jpg\" />";
|
||||
result = HtmlUtil.removeHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img />";
|
||||
result = HtmlUtil.removeHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//包含内容标签
|
||||
str = "pre<div class=\"test_div\">dfdsfdsfdsf</div>";
|
||||
result = HtmlUtil.removeHtmlTag(str, "div");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//带换行
|
||||
str = "pre<div class=\"test_div\">\r\n\t\tdfdsfdsfdsf\r\n</div>";
|
||||
result = HtmlUtil.removeHtmlTag(str, "div");
|
||||
Assert.assertEquals("pre", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unwrapHtmlTagTest() {
|
||||
//非闭合标签
|
||||
String str = "pre<img src=\"xxx/dfdsfds/test.jpg\">";
|
||||
String result = HtmlUtil.unwrapHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img>";
|
||||
result = HtmlUtil.unwrapHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img src=\"xxx/dfdsfds/test.jpg\" />";
|
||||
result = HtmlUtil.unwrapHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img />";
|
||||
result = HtmlUtil.unwrapHtmlTag(str, "img");
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//包含内容标签
|
||||
str = "pre<div class=\"test_div\">abc</div>";
|
||||
result = HtmlUtil.unwrapHtmlTag(str, "div");
|
||||
Assert.assertEquals("preabc", result);
|
||||
|
||||
//带换行
|
||||
str = "pre<div class=\"test_div\">\r\n\t\tabc\r\n</div>";
|
||||
result = HtmlUtil.unwrapHtmlTag(str, "div");
|
||||
Assert.assertEquals("pre\r\n\t\tabc\r\n", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void escapeTest() {
|
||||
String html = "<html><body>123'123'</body></html>";
|
||||
String escape = HtmlUtil.escape(html);
|
||||
String restoreEscaped = HtmlUtil.unescape(escape);
|
||||
Assert.assertEquals(html, restoreEscaped);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterTest() {
|
||||
String html = "<alert></alert>";
|
||||
String filter = HtmlUtil.filter(html);
|
||||
Assert.assertEquals("", filter);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package cn.hutool.http.test;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.date.TimeInterval;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.ssl.SSLSocketFactoryBuilder;
|
||||
|
||||
/**
|
||||
* {@link HttpRequest}单元测试
|
||||
*
|
||||
* @author Looly
|
||||
*
|
||||
*/
|
||||
public class HttpRequestTest {
|
||||
final String url = "http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2";
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getHttpsTest() {
|
||||
String body = HttpRequest.get("https://www.gjifa.com/pc/").execute().body();
|
||||
Console.log(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWithParamsTest() {
|
||||
String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
|
||||
|
||||
HttpRequest request = HttpRequest.get(url).setEncodeUrlParams(true).body("a=乌海");
|
||||
String body = request.execute().body();
|
||||
Console.log(body);
|
||||
|
||||
// String body2 = HttpUtil.get(url);
|
||||
// Console.log(body2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void asyncHeadTest() {
|
||||
HttpResponse response = HttpRequest.head(url).execute();
|
||||
Map<String, List<String>> headers = response.headers();
|
||||
Console.log(headers);
|
||||
Console.log(response.body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void asyncGetTest() {
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
HttpResponse body = HttpRequest.get(url).charset("GBK").executeAsync();
|
||||
long interval = timer.interval();
|
||||
timer.restart();
|
||||
Console.log(body.body());
|
||||
long interval2 = timer.interval();
|
||||
Console.log("Async response spend {}ms, body spend {}ms", interval, interval2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void syncGetTest() {
|
||||
TimeInterval timer = DateUtil.timer();
|
||||
HttpResponse body = HttpRequest.get(url).charset("GBK").execute();
|
||||
long interval = timer.interval();
|
||||
timer.restart();
|
||||
Console.log(body.body());
|
||||
long interval2 = timer.interval();
|
||||
Console.log("Async response spend {}ms, body spend {}ms", interval, interval2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void customGetTest() {
|
||||
// 自定义构建HTTP GET请求,发送Http GET请求,针对HTTPS安全加密,可以自定义SSL
|
||||
HttpRequest request = HttpRequest.get(url)
|
||||
// 自定义返回编码
|
||||
.charset(CharsetUtil.CHARSET_GBK)
|
||||
// 禁用缓存
|
||||
.disableCache()
|
||||
// 自定义SSL版本
|
||||
.setSSLProtocol(SSLSocketFactoryBuilder.TLSv12);
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
}
|
||||
265
hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java
Normal file
265
hutool-http/src/test/java/cn/hutool/http/test/HttpUtilTest.java
Normal file
@@ -0,0 +1,265 @@
|
||||
package cn.hutool.http.test;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ReUtil;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
public class HttpUtilTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest() {
|
||||
String result = HttpUtil.createPost("api.uhaozu.com/goods/description/1120448506").charset(CharsetUtil.UTF_8).execute().body();
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest() {
|
||||
String result1 = HttpUtil.get("http://photo.qzone.qq.com/fcgi-bin/fcg_list_album?uin=88888&outstyle=2", CharsetUtil.CHARSET_GBK);
|
||||
Console.log(result1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest2() {
|
||||
// 自定义的默认header无效
|
||||
String result = HttpRequest
|
||||
.get("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=101457313&redirect_uri=http%3A%2F%2Fwww.benmovip.com%2Fpay-cloud%2Fqqlogin%2FgetCode&state=ok")
|
||||
.removeHeader(Header.USER_AGENT).execute().body();
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest3() {
|
||||
// 测试url中带有空格的情况
|
||||
String result1 = HttpUtil.get("http://122.152.198.206:5000/kf?abc= d");
|
||||
Console.log(result1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest4() {
|
||||
// 测试url中带有空格的情况
|
||||
byte[] str = HttpRequest.get("http://img01.fs.yiban.cn/mobile/2D0Y71").execute().bodyBytes();
|
||||
|
||||
FileUtil.writeBytes(str, "f:/test/2D.jpg");
|
||||
Console.log(str);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest5() {
|
||||
String res = HttpUtil.get("https://comment.bilibili.com/67573272.xml");
|
||||
Console.log(res);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void get12306Test() {
|
||||
String result = HttpUtil.get("https://kyfw.12306.cn/otn/");
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadStringTest() {
|
||||
String url = "https://www.baidu.com";
|
||||
// 从远程直接读取字符串,需要自定义编码,直接调用JDK方法
|
||||
String content2 = HttpUtil.downloadString(url, CharsetUtil.UTF_8);
|
||||
Console.log(content2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void oschinaTest() {
|
||||
// 请求列表页
|
||||
String listContent = HttpUtil.get("https://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=2");
|
||||
// 使用正则获取所有标题
|
||||
List<String> titles = ReUtil.findAll("<span class=\"text-ellipsis\">(.*?)</span>", listContent, 1);
|
||||
for (String title : titles) {
|
||||
// 打印标题
|
||||
Console.log(title);
|
||||
}
|
||||
|
||||
// 请求下一页,检查Cookie是否复用
|
||||
listContent = HttpUtil.get("https://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeParamsTest() {
|
||||
String paramsStr = "uuuu=0&a=b&c=%3F%23%40!%24%25%5E%26%3Ddsssss555555";
|
||||
Map<String, List<String>> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("0", map.get("uuuu").get(0));
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("?#@!$%^&=dsssss555555", map.get("c").get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toParamsTest() {
|
||||
String paramsStr = "uuuu=0&a=b&c=3Ddsssss555555";
|
||||
Map<String, List<String>> map = HttpUtil.decodeParams(paramsStr, CharsetUtil.UTF_8);
|
||||
|
||||
String encodedParams = HttpUtil.toParams((Map<String, List<String>>) map);
|
||||
Assert.assertEquals(paramsStr, encodedParams);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void encodeParamTest() {
|
||||
// ?单独存在去除之,&单位位于末尾去除之
|
||||
String paramsStr = "?a=b&c=d&";
|
||||
String encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=b&c=d", encode);
|
||||
|
||||
// url不参与转码
|
||||
paramsStr = "http://www.abc.dd?a=b&c=d&";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("http://www.abc.dd?a=b&c=d", encode);
|
||||
|
||||
// b=b中的=被当作值的一部分,不做encode
|
||||
paramsStr = "a=b=b&c=d&";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=b=b&c=d", encode);
|
||||
|
||||
// =d的情况被处理为key为空
|
||||
paramsStr = "a=bbb&c=d&=d";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=bbb&c=d&=d", encode);
|
||||
|
||||
// d=的情况被处理为value为空
|
||||
paramsStr = "a=bbb&c=d&d=";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=bbb&c=d&d=", encode);
|
||||
|
||||
// 多个&&被处理为单个,相当于空条件
|
||||
paramsStr = "a=bbb&c=d&&&d=";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=bbb&c=d&d=", encode);
|
||||
|
||||
// &d&相当于只有键,无值得情况
|
||||
paramsStr = "a=bbb&c=d&d&";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=bbb&c=d&d=", encode);
|
||||
|
||||
// 中文的键和值被编码
|
||||
paramsStr = "a=bbb&c=你好&哈喽&";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("a=bbb&c=%E4%BD%A0%E5%A5%BD&%E5%93%88%E5%96%BD=", encode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeParamTest() {
|
||||
// 开头的?被去除
|
||||
String a = "?a=b&c=d&";
|
||||
Map<String, List<String>> map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("d", map.get("c").get(0));
|
||||
|
||||
// =e被当作空为key,e为value
|
||||
a = "?a=b&c=d&=e";
|
||||
map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("d", map.get("c").get(0));
|
||||
Assert.assertEquals("e", map.get("").get(0));
|
||||
|
||||
// 多余的&去除
|
||||
a = "?a=b&c=d&=e&&&&";
|
||||
map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("d", map.get("c").get(0));
|
||||
Assert.assertEquals("e", map.get("").get(0));
|
||||
|
||||
// 值为空
|
||||
a = "?a=b&c=d&e=";
|
||||
map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("d", map.get("c").get(0));
|
||||
Assert.assertEquals("", map.get("e").get(0));
|
||||
|
||||
// &=被作为键和值都为空
|
||||
a = "a=b&c=d&=";
|
||||
map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("d", map.get("c").get(0));
|
||||
Assert.assertEquals("", map.get("").get(0));
|
||||
|
||||
// &e&这类单独的字符串被当作key
|
||||
a = "a=b&c=d&e&";
|
||||
map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("b", map.get("a").get(0));
|
||||
Assert.assertEquals("d", map.get("c").get(0));
|
||||
Assert.assertEquals("", map.get("e").get(0));
|
||||
|
||||
// 被编码的键和值被还原
|
||||
a = "a=bbb&c=%E4%BD%A0%E5%A5%BD&%E5%93%88%E5%96%BD=";
|
||||
map = HttpUtil.decodeParams(a, CharsetUtil.UTF_8);
|
||||
Assert.assertEquals("bbb", map.get("a").get(0));
|
||||
Assert.assertEquals("你好", map.get("c").get(0));
|
||||
Assert.assertEquals("", map.get("哈喽").get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void patchTest() {
|
||||
String body = HttpRequest.post("https://www.baidu.com").execute().body();
|
||||
Console.log(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void urlWithFormTest() {
|
||||
Map<String, Object> param = new LinkedHashMap<>();
|
||||
param.put("AccessKeyId", "123");
|
||||
param.put("Action", "DescribeDomainRecords");
|
||||
param.put("Format", "date");
|
||||
param.put("DomainName", "lesper.cn"); // 域名地址
|
||||
param.put("SignatureMethod", "POST");
|
||||
param.put("SignatureNonce", "123");
|
||||
param.put("SignatureVersion", "4.3.1");
|
||||
param.put("Timestamp", 123432453);
|
||||
param.put("Version", "1.0");
|
||||
|
||||
String urlWithForm = HttpUtil.urlWithForm("http://api.hutool.cn/login?type=aaa", param, CharsetUtil.CHARSET_UTF_8, false);
|
||||
Assert.assertEquals(
|
||||
"http://api.hutool.cn/login?type=aaa&AccessKeyId=123&Action=DescribeDomainRecords&Format=date&DomainName=lesper.cn&SignatureMethod=POST&SignatureNonce=123&SignatureVersion=4.3.1&Timestamp=123432453&Version=1.0",
|
||||
urlWithForm);
|
||||
|
||||
urlWithForm = HttpUtil.urlWithForm("http://api.hutool.cn/login?type=aaa", param, CharsetUtil.CHARSET_UTF_8, false);
|
||||
Assert.assertEquals(
|
||||
"http://api.hutool.cn/login?type=aaa&AccessKeyId=123&Action=DescribeDomainRecords&Format=date&DomainName=lesper.cn&SignatureMethod=POST&SignatureNonce=123&SignatureVersion=4.3.1&Timestamp=123432453&Version=1.0",
|
||||
urlWithForm);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getCharsetTest() {
|
||||
String charsetName = ReUtil.get(HttpUtil.CHARSET_PATTERN, "Charset=UTF-8;fq=0.9", 1);
|
||||
Assert.assertEquals("UTF-8", charsetName);
|
||||
|
||||
charsetName = ReUtil.get(HttpUtil.META_CHARSET_PATTERN, "<meta charset=utf-8", 1);
|
||||
Assert.assertEquals("utf-8", charsetName);
|
||||
charsetName = ReUtil.get(HttpUtil.META_CHARSET_PATTERN, "<meta charset='utf-8'", 1);
|
||||
Assert.assertEquals("utf-8", charsetName);
|
||||
charsetName = ReUtil.get(HttpUtil.META_CHARSET_PATTERN, "<meta charset=\"utf-8\"", 1);
|
||||
Assert.assertEquals("utf-8", charsetName);
|
||||
charsetName = ReUtil.get(HttpUtil.META_CHARSET_PATTERN, "<meta charset = \"utf-8\"", 1);
|
||||
Assert.assertEquals("utf-8", charsetName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void normalizeParamsTest() {
|
||||
String encodeResult = HttpUtil.normalizeParams("参数", CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("%E5%8F%82%E6%95%B0", encodeResult);
|
||||
}
|
||||
}
|
||||
50
hutool-http/src/test/java/cn/hutool/http/test/RestTest.java
Normal file
50
hutool-http/src/test/java/cn/hutool/http/test/RestTest.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package cn.hutool.http.test;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
/**
|
||||
* Rest类型请求单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class RestTest {
|
||||
|
||||
@Test
|
||||
public void contentTypeTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2"));
|
||||
Assert.assertEquals("application/json;charset=UTF-8", request.header("Content-Type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2"));
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest2() {
|
||||
String result = HttpUtil.post("http://localhost:8090/rest/restTest/", JSONUtil.createObj()//
|
||||
.put("aaa", "aaaValue").put("键2", "值2").toString());
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest3() {
|
||||
HttpRequest request = HttpRequest.post("http://211.162.39.204:8181/jeesite-simple/a/open/bizGwbnService/test")//
|
||||
.body(JSONUtil.createObj().put("aaa", "aaaValue").put("键2", "值2"));
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package cn.hutool.http.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
/**
|
||||
* 文件上传单元测试
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class UploadTest {
|
||||
|
||||
/**
|
||||
* 多文件上传测试
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void uploadFilesTest() {
|
||||
File file = FileUtil.file("e:\\face.jpg");
|
||||
File file2 = FileUtil.file("e:\\face2.jpg");
|
||||
|
||||
// 方法一:自定义构建表单
|
||||
HttpRequest request = HttpRequest//
|
||||
.post("http://localhost:8090/file/upload")//
|
||||
.form("file", file2, file)//
|
||||
.form("fileType", "图片");
|
||||
HttpResponse response = request.execute();
|
||||
System.out.println(response.body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void uploadFileTest() {
|
||||
File file = FileUtil.file("D:\\face.jpg");
|
||||
|
||||
// 方法二:使用统一的表单,Http模块会自动识别参数类型,并完成上传
|
||||
HashMap<String, Object> paramMap = new HashMap<>();
|
||||
paramMap.put("city", "北京");
|
||||
paramMap.put("file", file);
|
||||
String result = HttpUtil.post("http://wthrcdn.etouch.cn/weather_mini", paramMap);
|
||||
System.out.println(result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package cn.hutool.http.useragent;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class UserAgentUtilTest {
|
||||
|
||||
@Test
|
||||
public void parseDesktopTest() {
|
||||
String uaStr = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1";
|
||||
|
||||
UserAgent ua = UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("Chrome", ua.getBrowser().toString());
|
||||
Assert.assertEquals("14.0.835.163", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("535.1", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 7 or Windows Server 2008R2", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMobileTest() {
|
||||
String uaStr = "User-Agent:Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5";
|
||||
|
||||
UserAgent ua = UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("Safari", ua.getBrowser().toString());
|
||||
Assert.assertEquals("5.0.2", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("533.17.9", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMiui10WithChromeTest(){
|
||||
String uaStr="Mozilla/5.0 (Linux; Android 9; MIX 3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.80 Mobile Safari/537.36";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("Chrome", ua.getBrowser().toString());
|
||||
Assert.assertEquals("70.0.3538.80", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("537.36", ua.getEngineVersion());
|
||||
Assert.assertEquals("Android", ua.getOs().toString());
|
||||
Assert.assertEquals("Android", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWindows10WithChromeTest(){
|
||||
String uaStr="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("Chrome", ua.getBrowser().toString());
|
||||
Assert.assertEquals("70.0.3538.102", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("537.36", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 10 or Windows Server 2016", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWindows10WithIe11Test(){
|
||||
String uaStr="Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSIE11", ua.getBrowser().toString());
|
||||
Assert.assertEquals("11.0", ua.getVersion());
|
||||
Assert.assertEquals("Trident", ua.getEngine().toString());
|
||||
Assert.assertEquals("7.0", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 10 or Windows Server 2016", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWindows10WithIeMobileLumia520Test(){
|
||||
String uaStr="Mozilla/5.0 (Mobile; Windows Phone 8.1; Android 4.0; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 520) like iPhone OS 7_0_3 Mac OS X AppleWebKit/537 (KHTML, like Gecko) Mobile Safari/537 ";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("IEMobile", ua.getBrowser().toString());
|
||||
Assert.assertEquals("11.0", ua.getVersion());
|
||||
Assert.assertEquals("Trident", ua.getEngine().toString());
|
||||
Assert.assertEquals("7.0", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows Phone", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows Phone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWindows10WithIe8EmulatorTest(){
|
||||
String uaStr="Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSIE", ua.getBrowser().toString());
|
||||
Assert.assertEquals("8.0", ua.getVersion());
|
||||
Assert.assertEquals("Trident", ua.getEngine().toString());
|
||||
Assert.assertEquals("4.0", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 7 or Windows Server 2008R2", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWindows10WithEdgeTest(){
|
||||
String uaStr="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17763";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSEdge", ua.getBrowser().toString());
|
||||
Assert.assertEquals("18.17763", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("537.36", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 10 or Windows Server 2016", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
@Test
|
||||
public void parseEdgeOnLumia950XLTest(){
|
||||
String uaStr="Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Microsoft; Lumia 950XL) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36 Edge/15.14900";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSEdge", ua.getBrowser().toString());
|
||||
Assert.assertEquals("15.14900", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("537.36", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows Phone", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows Phone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseChromeOnWindowsServer2012R2Test(){
|
||||
String uaStr="Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("Chrome", ua.getBrowser().toString());
|
||||
Assert.assertEquals("63.0.3239.132", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("537.36", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 8.1 or Winsows Server 2012R2", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseIE11OnWindowsServer2008R2Test(){
|
||||
String uaStr="Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
|
||||
UserAgent ua=UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSIE11", ua.getBrowser().toString());
|
||||
Assert.assertEquals("11.0", ua.getVersion());
|
||||
Assert.assertEquals("Trident", ua.getEngine().toString());
|
||||
Assert.assertEquals("7.0", ua.getEngineVersion());
|
||||
Assert.assertEquals("Windows 7 or Windows Server 2008R2", ua.getOs().toString());
|
||||
Assert.assertEquals("Windows", ua.getPlatform().toString());
|
||||
Assert.assertFalse(ua.isMobile());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package cn.hutool.http.webservice;
|
||||
|
||||
import javax.xml.soap.SOAPException;
|
||||
import javax.xml.soap.SOAPMessage;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
|
||||
/**
|
||||
* SOAP相关单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class SoapClientTest {
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void requestTest() {
|
||||
SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx")
|
||||
.setMethod("web:getCountryCityByIp", "http://WebXml.com.cn/")
|
||||
.setCharset(CharsetUtil.CHARSET_GBK)
|
||||
.setParam("theIpAddress", "218.21.240.106");
|
||||
|
||||
Console.log(client.getMsgStr(true));
|
||||
|
||||
Console.log(client.send(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void requestForMessageTest() throws SOAPException {
|
||||
SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx")
|
||||
.setMethod("web:getCountryCityByIp", "http://WebXml.com.cn/")
|
||||
.setParam("theIpAddress", "218.21.240.106");
|
||||
|
||||
SOAPMessage message = client.sendForMessage();
|
||||
Console.log(message.getSOAPBody().getTextContent());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user