Make getCharset to support null parameter.

master
Joe Zhou 2024-06-04 00:57:28 +08:00
parent 7d2d2244b3
commit d2aebc60a7
2 changed files with 10 additions and 0 deletions

View File

@ -116,6 +116,10 @@ public class UrlUtils {
private static final Pattern patternForCharset = Pattern.compile("charset\\s*=\\s*['\"]*([^\\s;'\"]*)", Pattern.CASE_INSENSITIVE);
public static String getCharset(String contentType) {
if (contentType == null) {
return null;
}
Matcher matcher = patternForCharset.matcher(contentType);
if (matcher.find()) {
String charset = matcher.group(1);

View File

@ -1,5 +1,7 @@
package us.codecraft.webmagic.utils;
import static org.junit.Assert.assertNull;
import org.junit.Assert;
import org.junit.Test;
@ -43,5 +45,9 @@ public class UrlUtilsTest {
Assert.assertEquals("www.dianping.com",UrlUtils.getDomain(url));
}
@Test
public void testGetCharset() {
assertNull(UrlUtils.getCharset(null));
}
}