mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
change line sep
This commit is contained in:
@@ -1,193 +1,193 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.StreamProgress;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 下载单元测试
|
||||
*
|
||||
* @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() {
|
||||
|
||||
final 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("下载完成!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest1() {
|
||||
File file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", "d:/download/temp");
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest2() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), 1, new StreamProgress() {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void progress(long progressSize) {
|
||||
System.out.println("download size:" + progressSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
System.out.println("end");
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
Assert.assertTrue(file.getName().length() > 0);
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof IORuntimeException);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest3() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), new StreamProgress() {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void progress(long progressSize) {
|
||||
System.out.println("download size:" + progressSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
System.out.println("end");
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
Assert.assertTrue(file.getName().length() > 0);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest4() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
Assert.assertTrue(file.getName().length() > 0);
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof IORuntimeException);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest5() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
|
||||
File file1 = null;
|
||||
try {
|
||||
file1 = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"));
|
||||
|
||||
Assert.assertNotNull(file1);
|
||||
Assert.assertTrue(file1.exists());
|
||||
Assert.assertTrue(file1.isFile());
|
||||
Assert.assertTrue(file1.length() > 0);
|
||||
} finally {
|
||||
FileUtil.del(file1);
|
||||
}
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IORuntimeException;
|
||||
import cn.hutool.core.io.StreamProgress;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 下载单元测试
|
||||
*
|
||||
* @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() {
|
||||
|
||||
final 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("下载完成!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest1() {
|
||||
File file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", "d:/download/temp");
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest2() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), 1, new StreamProgress() {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void progress(long progressSize) {
|
||||
System.out.println("download size:" + progressSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
System.out.println("end");
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
Assert.assertTrue(file.getName().length() > 0);
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof IORuntimeException);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest3() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.4.0/hutool-all-5.4.0-sources.jar", FileUtil.file("d:/download/temp"), new StreamProgress() {
|
||||
@Override
|
||||
public void start() {
|
||||
System.out.println("start");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void progress(long progressSize) {
|
||||
System.out.println("download size:" + progressSize);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void finish() {
|
||||
System.out.println("end");
|
||||
}
|
||||
});
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
Assert.assertTrue(file.getName().length() > 0);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest4() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"), 1);
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
Assert.assertTrue(file.getName().length() > 0);
|
||||
} catch (Exception e) {
|
||||
Assert.assertTrue(e instanceof IORuntimeException);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void downloadFileFromUrlTest5() {
|
||||
File file = null;
|
||||
try {
|
||||
file = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp", UUID.randomUUID().toString()));
|
||||
|
||||
Assert.assertNotNull(file);
|
||||
Assert.assertTrue(file.exists());
|
||||
Assert.assertTrue(file.isFile());
|
||||
Assert.assertTrue(file.length() > 0);
|
||||
} finally {
|
||||
FileUtil.del(file);
|
||||
}
|
||||
|
||||
File file1 = null;
|
||||
try {
|
||||
file1 = HttpUtil.downloadFileFromUrl("http://groovy-lang.org/changelogs/changelog-3.0.5.html", FileUtil.file("d:/download/temp"));
|
||||
|
||||
Assert.assertNotNull(file1);
|
||||
Assert.assertTrue(file1.exists());
|
||||
Assert.assertTrue(file1.isFile());
|
||||
Assert.assertTrue(file1.length() > 0);
|
||||
} finally {
|
||||
FileUtil.del(file1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,162 +1,162 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
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 cleanHtmlTagTest() {
|
||||
//非闭合标签
|
||||
String str = "pre<img src=\"xxx/dfdsfds/test.jpg\">";
|
||||
String result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img>";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img src=\"xxx/dfdsfds/test.jpg\" />";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img />";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//包含内容标签
|
||||
str = "pre<div class=\"test_div\">dfdsfdsfdsf</div>";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("predfdsfdsfdsf", result);
|
||||
|
||||
//带换行
|
||||
str = "pre<div class=\"test_div\">\r\n\t\tdfdsfdsfdsf\r\n</div><div class=\"test_div\">BBBB</div>";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre\r\n\t\tdfdsfdsfdsf\r\nBBBB", 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);
|
||||
Assert.assertEquals("<html><body>123'123'</body></html>", escape);
|
||||
String restoreEscaped = HtmlUtil.unescape(escape);
|
||||
Assert.assertEquals(html, restoreEscaped);
|
||||
Assert.assertEquals("'", HtmlUtil.unescape("'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterTest() {
|
||||
String html = "<alert></alert>";
|
||||
String filter = HtmlUtil.filter(html);
|
||||
Assert.assertEquals("", filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeHtmlAttrTest() {
|
||||
|
||||
// 去除的属性加双引号测试
|
||||
String html = "<div class=\"test_div\"></div><span class=\"test_div\"></span>";
|
||||
String result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div></div><span></span>", result);
|
||||
|
||||
// 去除的属性后跟空格、加单引号、不加引号测试
|
||||
html = "<div class=test_div></div><span Class='test_div' ></span>";
|
||||
result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div></div><span></span>", result);
|
||||
|
||||
// 去除的属性位于标签末尾、其它属性前测试
|
||||
html = "<div style=\"margin:100%\" class=test_div></div><span Class='test_div' width=100></span>";
|
||||
result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div style=\"margin:100%\"></div><span width=100></span>", result);
|
||||
|
||||
// 去除的属性名和值之间存在空格
|
||||
html = "<div style = \"margin:100%\" class = test_div></div><span Class = 'test_div' width=100></span>";
|
||||
result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div style = \"margin:100%\"></div><span width=100></span>", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAllHtmlAttrTest() {
|
||||
String html = "<div class=\"test_div\" width=\"120\"></div>";
|
||||
String result = HtmlUtil.removeAllHtmlAttr(html, "div");
|
||||
Assert.assertEquals("<div></div>", result);
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
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 cleanHtmlTagTest() {
|
||||
//非闭合标签
|
||||
String str = "pre<img src=\"xxx/dfdsfds/test.jpg\">";
|
||||
String result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img>";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img src=\"xxx/dfdsfds/test.jpg\" />";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//闭合标签
|
||||
str = "pre<img />";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre", result);
|
||||
|
||||
//包含内容标签
|
||||
str = "pre<div class=\"test_div\">dfdsfdsfdsf</div>";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("predfdsfdsfdsf", result);
|
||||
|
||||
//带换行
|
||||
str = "pre<div class=\"test_div\">\r\n\t\tdfdsfdsfdsf\r\n</div><div class=\"test_div\">BBBB</div>";
|
||||
result = HtmlUtil.cleanHtmlTag(str);
|
||||
Assert.assertEquals("pre\r\n\t\tdfdsfdsfdsf\r\nBBBB", 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);
|
||||
Assert.assertEquals("<html><body>123'123'</body></html>", escape);
|
||||
String restoreEscaped = HtmlUtil.unescape(escape);
|
||||
Assert.assertEquals(html, restoreEscaped);
|
||||
Assert.assertEquals("'", HtmlUtil.unescape("'"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterTest() {
|
||||
String html = "<alert></alert>";
|
||||
String filter = HtmlUtil.filter(html);
|
||||
Assert.assertEquals("", filter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeHtmlAttrTest() {
|
||||
|
||||
// 去除的属性加双引号测试
|
||||
String html = "<div class=\"test_div\"></div><span class=\"test_div\"></span>";
|
||||
String result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div></div><span></span>", result);
|
||||
|
||||
// 去除的属性后跟空格、加单引号、不加引号测试
|
||||
html = "<div class=test_div></div><span Class='test_div' ></span>";
|
||||
result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div></div><span></span>", result);
|
||||
|
||||
// 去除的属性位于标签末尾、其它属性前测试
|
||||
html = "<div style=\"margin:100%\" class=test_div></div><span Class='test_div' width=100></span>";
|
||||
result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div style=\"margin:100%\"></div><span width=100></span>", result);
|
||||
|
||||
// 去除的属性名和值之间存在空格
|
||||
html = "<div style = \"margin:100%\" class = test_div></div><span Class = 'test_div' width=100></span>";
|
||||
result = HtmlUtil.removeHtmlAttr(html, "class");
|
||||
Assert.assertEquals("<div style = \"margin:100%\"></div><span width=100></span>", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeAllHtmlAttrTest() {
|
||||
String html = "<div class=\"test_div\" width=\"120\"></div>";
|
||||
String result = HtmlUtil.removeAllHtmlAttr(html, "div");
|
||||
Assert.assertEquals("<div></div>", result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,143 +1,143 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
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.ssl.SSLSocketFactoryBuilder;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@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.hutool.cn/").timeout(10).execute().body();
|
||||
Console.log(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getCookiesTest() {
|
||||
// 检查在Connection关闭情况下Cookie是否可以正常获取
|
||||
HttpResponse res = HttpRequest.get("https://www.oschina.net/").execute();
|
||||
String body = res.body();
|
||||
Console.log(res.getCookies());
|
||||
Console.log(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void toStringTest() {
|
||||
String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
|
||||
|
||||
HttpRequest request = HttpRequest.get(url).body("a=乌海");
|
||||
Console.log(request.toString());
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getDeflateTest() {
|
||||
String res = HttpRequest.get("https://comment.bilibili.com/67573272.xml")
|
||||
.execute().body();
|
||||
Console.log(res);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void bodyTest() {
|
||||
String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().body();
|
||||
Console.log(ddddd1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试GET请求附带body体是否会变更为POST
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void getLocalTest() {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("hhhhh");
|
||||
list.add("sssss");
|
||||
|
||||
Map<String, Object> map = new HashMap<>(16);
|
||||
map.put("recordId", "12321321");
|
||||
map.put("page", "1");
|
||||
map.put("size", "2");
|
||||
map.put("sizes", list);
|
||||
|
||||
String s = JSONUtil.toJsonStr(map);
|
||||
HttpRequest request = HttpUtil.createGet("http://localhost:8888/get");
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWithoutEncodeTest(){
|
||||
String url = "https://img-cloud.voc.com.cn/140/2020/09/03/c3d41b93e0d32138574af8e8b50928b376ca5ba61599127028157.png?imageMogr2/auto-orient/thumbnail/500&pid=259848";
|
||||
HttpRequest get = HttpUtil.createGet(url);
|
||||
Console.log(get.getUrl());
|
||||
HttpResponse execute = get.execute();
|
||||
Console.log(execute.body());
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
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.ssl.SSLSocketFactoryBuilder;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@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.hutool.cn/").timeout(10).execute().body();
|
||||
Console.log(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getCookiesTest() {
|
||||
// 检查在Connection关闭情况下Cookie是否可以正常获取
|
||||
HttpResponse res = HttpRequest.get("https://www.oschina.net/").execute();
|
||||
String body = res.body();
|
||||
Console.log(res.getCookies());
|
||||
Console.log(body);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void toStringTest() {
|
||||
String url = "http://gc.ditu.aliyun.com/geocoding?ccc=你好";
|
||||
|
||||
HttpRequest request = HttpRequest.get(url).body("a=乌海");
|
||||
Console.log(request.toString());
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getDeflateTest() {
|
||||
String res = HttpRequest.get("https://comment.bilibili.com/67573272.xml")
|
||||
.execute().body();
|
||||
Console.log(res);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void bodyTest() {
|
||||
String ddddd1 = HttpRequest.get("https://baijiahao.baidu.com/s").body("id=1625528941695652600").execute().body();
|
||||
Console.log(ddddd1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试GET请求附带body体是否会变更为POST
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void getLocalTest() {
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("hhhhh");
|
||||
list.add("sssss");
|
||||
|
||||
Map<String, Object> map = new HashMap<>(16);
|
||||
map.put("recordId", "12321321");
|
||||
map.put("page", "1");
|
||||
map.put("size", "2");
|
||||
map.put("sizes", list);
|
||||
|
||||
String s = JSONUtil.toJsonStr(map);
|
||||
HttpRequest request = HttpUtil.createGet("http://localhost:8888/get");
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWithoutEncodeTest(){
|
||||
String url = "https://img-cloud.voc.com.cn/140/2020/09/03/c3d41b93e0d32138574af8e8b50928b376ca5ba61599127028157.png?imageMogr2/auto-orient/thumbnail/500&pid=259848";
|
||||
HttpRequest get = HttpUtil.createGet(url);
|
||||
Console.log(get.getUrl());
|
||||
HttpResponse execute = get.execute();
|
||||
Console.log(execute.body());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,321 +1,321 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
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 org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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 postTest2() {
|
||||
// 某些接口对Accept头有特殊要求,此处自定义头
|
||||
String result = HttpUtil
|
||||
.createPost("http://cmp.ishanghome.com/cmp/v1/community/queryClusterCommunity")
|
||||
.header(Header.ACCEPT, "*/*")
|
||||
.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 url2 = "http://storage.chancecloud.com.cn/20200413_%E7%B2%A4B12313_386.pdf";
|
||||
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
|
||||
HttpUtil.download(url2, os2, false);
|
||||
|
||||
url2 = "http://storage.chancecloud.com.cn/20200413_粤B12313_386.pdf";
|
||||
HttpUtil.download(url2, os2, false);
|
||||
}
|
||||
|
||||
@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");
|
||||
Console.log(listContent);
|
||||
}
|
||||
|
||||
@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 decodeParamMapTest() {
|
||||
// 参数值存在分界标记等号时
|
||||
Map<String, String> paramMap = HttpUtil.decodeParamMap("https://www.xxx.com/api.action?aa=123&f_token=NzBkMjQxNDM1MDVlMDliZTk1OTU3ZDI1OTI0NTBiOWQ=", CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("123",paramMap.get("aa"));
|
||||
Assert.assertEquals("NzBkMjQxNDM1MDVlMDliZTk1OTU3ZDI1OTI0NTBiOWQ=",paramMap.get("f_token"));
|
||||
}
|
||||
|
||||
@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);
|
||||
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);
|
||||
|
||||
// URL原样输出
|
||||
paramsStr = "https://www.hutool.cn/";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals(paramsStr, encode);
|
||||
|
||||
// URL原样输出
|
||||
paramsStr = "https://www.hutool.cn/?";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("https://www.hutool.cn/", 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.assertNull(map.get("e").get(0));
|
||||
Assert.assertNull(map.get("").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.patch("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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMimeTypeTest() {
|
||||
String mimeType = HttpUtil.getMimeType("aaa.aaa");
|
||||
Assert.assertNull(mimeType);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWeixinTest(){
|
||||
// 测试特殊URL,即URL中有&情况是否请求正常
|
||||
String url = "https://mp.weixin.qq.com/s?__biz=MzI5NjkyNTIxMg==&mid=100000465&idx=1&sn=1044c0d19723f74f04f4c1da34eefa35&chksm=6cbda3a25bca2ab4516410db6ce6e125badaac2f8c5548ea6e18eab6dc3c5422cb8cbe1095f7";
|
||||
final String s = HttpUtil.get(url);
|
||||
Console.log(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getNocovTest(){
|
||||
String url = "https://qiniu.nocov.cn/medical-manage%2Ftest%2FBANNER_IMG%2F444004467954556928%2F1595215173047icon.png~imgReduce?e=1597081986&token=V2lJYVgQgAv_sbypfEZ0qpKs6TzD1q5JIDVr0Tw8:89cbBkLLwEc9JsMoCLkAEOu820E=";
|
||||
final String s = HttpUtil.get(url);
|
||||
Console.log(s);
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
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 org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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 postTest2() {
|
||||
// 某些接口对Accept头有特殊要求,此处自定义头
|
||||
String result = HttpUtil
|
||||
.createPost("http://cmp.ishanghome.com/cmp/v1/community/queryClusterCommunity")
|
||||
.header(Header.ACCEPT, "*/*")
|
||||
.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 url2 = "http://storage.chancecloud.com.cn/20200413_%E7%B2%A4B12313_386.pdf";
|
||||
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
|
||||
HttpUtil.download(url2, os2, false);
|
||||
|
||||
url2 = "http://storage.chancecloud.com.cn/20200413_粤B12313_386.pdf";
|
||||
HttpUtil.download(url2, os2, false);
|
||||
}
|
||||
|
||||
@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");
|
||||
Console.log(listContent);
|
||||
}
|
||||
|
||||
@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 decodeParamMapTest() {
|
||||
// 参数值存在分界标记等号时
|
||||
Map<String, String> paramMap = HttpUtil.decodeParamMap("https://www.xxx.com/api.action?aa=123&f_token=NzBkMjQxNDM1MDVlMDliZTk1OTU3ZDI1OTI0NTBiOWQ=", CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("123",paramMap.get("aa"));
|
||||
Assert.assertEquals("NzBkMjQxNDM1MDVlMDliZTk1OTU3ZDI1OTI0NTBiOWQ=",paramMap.get("f_token"));
|
||||
}
|
||||
|
||||
@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);
|
||||
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);
|
||||
|
||||
// URL原样输出
|
||||
paramsStr = "https://www.hutool.cn/";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals(paramsStr, encode);
|
||||
|
||||
// URL原样输出
|
||||
paramsStr = "https://www.hutool.cn/?";
|
||||
encode = HttpUtil.encodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
|
||||
Assert.assertEquals("https://www.hutool.cn/", 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.assertNull(map.get("e").get(0));
|
||||
Assert.assertNull(map.get("").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.patch("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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMimeTypeTest() {
|
||||
String mimeType = HttpUtil.getMimeType("aaa.aaa");
|
||||
Assert.assertNull(mimeType);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWeixinTest(){
|
||||
// 测试特殊URL,即URL中有&情况是否请求正常
|
||||
String url = "https://mp.weixin.qq.com/s?__biz=MzI5NjkyNTIxMg==&mid=100000465&idx=1&sn=1044c0d19723f74f04f4c1da34eefa35&chksm=6cbda3a25bca2ab4516410db6ce6e125badaac2f8c5548ea6e18eab6dc3c5422cb8cbe1095f7";
|
||||
final String s = HttpUtil.get(url);
|
||||
Console.log(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getNocovTest(){
|
||||
String url = "https://qiniu.nocov.cn/medical-manage%2Ftest%2FBANNER_IMG%2F444004467954556928%2F1595215173047icon.png~imgReduce?e=1597081986&token=V2lJYVgQgAv_sbypfEZ0qpKs6TzD1q5JIDVr0Tw8:89cbBkLLwEc9JsMoCLkAEOu820E=";
|
||||
final String s = HttpUtil.get(url);
|
||||
Console.log(s);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class HttpsTest {
|
||||
|
||||
/**
|
||||
* 测试单例的SSLSocketFactory是否有线程安全问题
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest() {
|
||||
final AtomicInteger count = new AtomicInteger();
|
||||
for(int i =0; i < 100; i++){
|
||||
ThreadUtil.execute(()->{
|
||||
final String s = HttpUtil.get("https://www.baidu.com/");
|
||||
Console.log(count.incrementAndGet());
|
||||
});
|
||||
}
|
||||
ThreadUtil.sync(this);
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class HttpsTest {
|
||||
|
||||
/**
|
||||
* 测试单例的SSLSocketFactory是否有线程安全问题
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void getTest() {
|
||||
final AtomicInteger count = new AtomicInteger();
|
||||
for(int i =0; i < 100; i++){
|
||||
ThreadUtil.execute(()->{
|
||||
final String s = HttpUtil.get("https://www.baidu.com/");
|
||||
Console.log(count.incrementAndGet());
|
||||
});
|
||||
}
|
||||
ThreadUtil.sync(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,58 +1,58 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Rest类型请求单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class RestTest {
|
||||
|
||||
@Test
|
||||
public void contentTypeTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
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()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest2() {
|
||||
String result = HttpUtil.post("http://localhost:8090/rest/restTest/", JSONUtil.createObj()//
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWithBodyTest() {
|
||||
HttpRequest request = HttpRequest.get("http://localhost:8888/restTest")//
|
||||
.header(Header.CONTENT_TYPE, "application/json")
|
||||
.body(JSONUtil.createObj()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Rest类型请求单元测试
|
||||
*
|
||||
* @author looly
|
||||
*
|
||||
*/
|
||||
public class RestTest {
|
||||
|
||||
@Test
|
||||
public void contentTypeTest() {
|
||||
HttpRequest request = HttpRequest.post("http://localhost:8090/rest/restTest/")//
|
||||
.body(JSONUtil.createObj()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
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()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void postTest2() {
|
||||
String result = HttpUtil.post("http://localhost:8090/rest/restTest/", JSONUtil.createObj()//
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
Console.log(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void getWithBodyTest() {
|
||||
HttpRequest request = HttpRequest.get("http://localhost:8888/restTest")//
|
||||
.header(Header.CONTENT_TYPE, "application/json")
|
||||
.body(JSONUtil.createObj()
|
||||
.set("aaa", "aaaValue")
|
||||
.set("键2", "值2").toString());
|
||||
Console.log(request.execute().body());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,71 +1,71 @@
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传单元测试
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class UploadTest {
|
||||
|
||||
/**
|
||||
* 多文件上传测试
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void uploadFilesTest() {
|
||||
File file = FileUtil.file("d:\\图片1.JPG");
|
||||
File file2 = FileUtil.file("d:\\图片3.png");
|
||||
|
||||
// 方法一:自定义构建表单
|
||||
HttpRequest request = HttpRequest//
|
||||
.post("http://localhost:8888/file")//
|
||||
.form("file", file2, file)//
|
||||
.form("fileType", "图片");
|
||||
HttpResponse response = request.execute();
|
||||
Console.log(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);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void uploadTest() {
|
||||
//客户端
|
||||
String url = "http://localhost:8888/file";
|
||||
Path file = Paths.get("D:\\testBigData_upload.xlsx");
|
||||
Map<String, String> headers = new HashMap<>(16);
|
||||
headers.put("md5", "aaaaaaaa");
|
||||
|
||||
Map<String, Object> params = new HashMap<>(16);
|
||||
params.put("fileName", file.toFile().getName());
|
||||
params.put("file", file.toFile());
|
||||
HttpRequest httpRequest = HttpRequest.post(url)
|
||||
.setChunkedStreamingMode(1024 * 1024)
|
||||
.headerMap(headers, false)
|
||||
.form(params);
|
||||
HttpResponse httpResponse = httpRequest.execute();
|
||||
Console.log(httpResponse);
|
||||
}
|
||||
}
|
||||
package cn.hutool.http;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传单元测试
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class UploadTest {
|
||||
|
||||
/**
|
||||
* 多文件上传测试
|
||||
*/
|
||||
@Test
|
||||
@Ignore
|
||||
public void uploadFilesTest() {
|
||||
File file = FileUtil.file("d:\\图片1.JPG");
|
||||
File file2 = FileUtil.file("d:\\图片3.png");
|
||||
|
||||
// 方法一:自定义构建表单
|
||||
HttpRequest request = HttpRequest//
|
||||
.post("http://localhost:8888/file")//
|
||||
.form("file", file2, file)//
|
||||
.form("fileType", "图片");
|
||||
HttpResponse response = request.execute();
|
||||
Console.log(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);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void uploadTest() {
|
||||
//客户端
|
||||
String url = "http://localhost:8888/file";
|
||||
Path file = Paths.get("D:\\testBigData_upload.xlsx");
|
||||
Map<String, String> headers = new HashMap<>(16);
|
||||
headers.put("md5", "aaaaaaaa");
|
||||
|
||||
Map<String, Object> params = new HashMap<>(16);
|
||||
params.put("fileName", file.toFile().getName());
|
||||
params.put("file", file.toFile());
|
||||
HttpRequest httpRequest = HttpRequest.post(url)
|
||||
.setChunkedStreamingMode(1024 * 1024)
|
||||
.headerMap(headers, false)
|
||||
.form(params);
|
||||
HttpResponse httpResponse = httpRequest.execute();
|
||||
Console.log(httpResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.swing.DesktopUtil;
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
public class BlankServerTest {
|
||||
public static void main(String[] args) {
|
||||
HttpUtil.createServer(8888)
|
||||
.addAction("/", (req, res)-> res.write("Hello Hutool Server", ContentType.JSON.getValue()))
|
||||
.start();
|
||||
|
||||
DesktopUtil.browse("http://localhost:8888/");
|
||||
}
|
||||
}
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.swing.DesktopUtil;
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
public class BlankServerTest {
|
||||
public static void main(String[] args) {
|
||||
HttpUtil.createServer(8888)
|
||||
.addAction("/", (req, res)-> res.write("Hello Hutool Server", ContentType.JSON.getValue()))
|
||||
.start();
|
||||
|
||||
DesktopUtil.browse("http://localhost:8888/");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.swing.DesktopUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
public class DocServerTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HttpUtil.createServer(80)
|
||||
// 设置默认根目录,
|
||||
.setRoot("D:\\workspace\\site\\hutool-site")
|
||||
.start();
|
||||
|
||||
DesktopUtil.browse("http://localhost/");
|
||||
}
|
||||
}
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.swing.DesktopUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
|
||||
public class DocServerTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
HttpUtil.createServer(80)
|
||||
// 设置默认根目录,
|
||||
.setRoot("D:\\workspace\\site\\hutool-site")
|
||||
.start();
|
||||
|
||||
DesktopUtil.browse("http://localhost/");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.net.multipart.UploadFile;
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
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())
|
||||
.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();
|
||||
}
|
||||
}
|
||||
package cn.hutool.http.server;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.net.multipart.UploadFile;
|
||||
import cn.hutool.http.ContentType;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
|
||||
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())
|
||||
.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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,233 +1,233 @@
|
||||
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 parseHuaweiPhoneWithNativeBrowserTest() {
|
||||
String uaString = "Mozilla/5.0 (Linux; Android 10; EML-AL00 Build/HUAWEIEML-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("Android Browser", ua.getBrowser().toString());
|
||||
Assert.assertEquals("4.0", 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 parseSamsungPhoneWithNativeBrowserTest() {
|
||||
String uaString = "Dalvik/2.1.0 (Linux; U; Android 9; SM-G950U Build/PPR1.180610.011)";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("Android Browser", ua.getBrowser().toString());
|
||||
Assert.assertNull(ua.getVersion());
|
||||
Assert.assertEquals("Unknown", ua.getEngine().toString());
|
||||
Assert.assertNull(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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseEdgeTest() {
|
||||
String uaStr = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.69 Safari/537.36 Edg/81.0.416.34";
|
||||
UserAgent ua = UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSEdge", ua.getBrowser().toString());
|
||||
Assert.assertEquals("81.0.416.34", 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());
|
||||
}
|
||||
|
||||
/**
|
||||
* https://github.com/looly/hutool/issues/1177
|
||||
*/
|
||||
@Test
|
||||
public void parseMicroMessengerTest() {
|
||||
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A372 MicroMessenger/7.0.17(0x17001127) NetType/WIFI Language/zh_CN";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("MicroMessenger", ua.getBrowser().toString());
|
||||
Assert.assertEquals("7.0.17", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("604.1.38", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWorkWxTest() {
|
||||
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 wxwork/3.0.31 MicroMessenger/7.0.1 Language/zh";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("wxwork", ua.getBrowser().toString());
|
||||
Assert.assertEquals("3.0.31", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("605.1.15", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseDingTalkTest() {
|
||||
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/18A373 AliApp(DingTalk/5.1.33) com.laiwang.DingTalk/13976299 Channel/201200 language/zh-Hans-CN WK";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("DingTalk", ua.getBrowser().toString());
|
||||
Assert.assertEquals("5.1.33", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("605.1.15", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
}
|
||||
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 parseHuaweiPhoneWithNativeBrowserTest() {
|
||||
String uaString = "Mozilla/5.0 (Linux; Android 10; EML-AL00 Build/HUAWEIEML-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("Android Browser", ua.getBrowser().toString());
|
||||
Assert.assertEquals("4.0", 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 parseSamsungPhoneWithNativeBrowserTest() {
|
||||
String uaString = "Dalvik/2.1.0 (Linux; U; Android 9; SM-G950U Build/PPR1.180610.011)";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("Android Browser", ua.getBrowser().toString());
|
||||
Assert.assertNull(ua.getVersion());
|
||||
Assert.assertEquals("Unknown", ua.getEngine().toString());
|
||||
Assert.assertNull(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());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseEdgeTest() {
|
||||
String uaStr = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.69 Safari/537.36 Edg/81.0.416.34";
|
||||
UserAgent ua = UserAgentUtil.parse(uaStr);
|
||||
Assert.assertEquals("MSEdge", ua.getBrowser().toString());
|
||||
Assert.assertEquals("81.0.416.34", 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());
|
||||
}
|
||||
|
||||
/**
|
||||
* https://github.com/looly/hutool/issues/1177
|
||||
*/
|
||||
@Test
|
||||
public void parseMicroMessengerTest() {
|
||||
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Mobile/15A372 MicroMessenger/7.0.17(0x17001127) NetType/WIFI Language/zh_CN";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("MicroMessenger", ua.getBrowser().toString());
|
||||
Assert.assertEquals("7.0.17", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("604.1.38", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseWorkWxTest() {
|
||||
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 wxwork/3.0.31 MicroMessenger/7.0.1 Language/zh";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("wxwork", ua.getBrowser().toString());
|
||||
Assert.assertEquals("3.0.31", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("605.1.15", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseDingTalkTest() {
|
||||
String uaString = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/18A373 AliApp(DingTalk/5.1.33) com.laiwang.DingTalk/13976299 Channel/201200 language/zh-Hans-CN WK";
|
||||
UserAgent ua = UserAgentUtil.parse(uaString);
|
||||
Assert.assertEquals("DingTalk", ua.getBrowser().toString());
|
||||
Assert.assertEquals("5.1.33", ua.getVersion());
|
||||
Assert.assertEquals("Webkit", ua.getEngine().toString());
|
||||
Assert.assertEquals("605.1.15", ua.getEngineVersion());
|
||||
Assert.assertEquals("iPhone", ua.getOs().toString());
|
||||
Assert.assertEquals("iPhone", ua.getPlatform().toString());
|
||||
Assert.assertTrue(ua.isMobile());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +1,42 @@
|
||||
package cn.hutool.http.webservice;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.soap.SOAPException;
|
||||
import javax.xml.soap.SOAPMessage;
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
package cn.hutool.http.webservice;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.xml.soap.SOAPException;
|
||||
import javax.xml.soap.SOAPMessage;
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>文件上传表单提交</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>文件上传测试</h1>
|
||||
<form action="file" method="post" enctype="multipart/form-data">
|
||||
文件:<input type="file" name="file"><br>
|
||||
<input type="submit" />
|
||||
</form>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>文件上传表单提交</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>文件上传测试</h1>
|
||||
<form action="file" method="post" enctype="multipart/form-data">
|
||||
文件:<input type="file" name="file"><br>
|
||||
<input type="submit" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,7 +1,7 @@
|
||||
<!DOCTYPE html>
|
||||
<html class="no-js">
|
||||
<body>
|
||||
<h1>Hutool</h1>
|
||||
<h3>Simple Server of Hutool</h3>
|
||||
</body>
|
||||
<!DOCTYPE html>
|
||||
<html class="no-js">
|
||||
<body>
|
||||
<h1>Hutool</h1>
|
||||
<h3>Simple Server of Hutool</h3>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user