Make getCharset to support null parameter.
parent
7d2d2244b3
commit
d2aebc60a7
|
@ -116,6 +116,10 @@ public class UrlUtils {
|
||||||
private static final Pattern patternForCharset = Pattern.compile("charset\\s*=\\s*['\"]*([^\\s;'\"]*)", Pattern.CASE_INSENSITIVE);
|
private static final Pattern patternForCharset = Pattern.compile("charset\\s*=\\s*['\"]*([^\\s;'\"]*)", Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
public static String getCharset(String contentType) {
|
public static String getCharset(String contentType) {
|
||||||
|
if (contentType == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
Matcher matcher = patternForCharset.matcher(contentType);
|
Matcher matcher = patternForCharset.matcher(contentType);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
String charset = matcher.group(1);
|
String charset = matcher.group(1);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package us.codecraft.webmagic.utils;
|
package us.codecraft.webmagic.utils;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -43,5 +45,9 @@ public class UrlUtilsTest {
|
||||||
Assert.assertEquals("www.dianping.com",UrlUtils.getDomain(url));
|
Assert.assertEquals("www.dianping.com",UrlUtils.getDomain(url));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetCharset() {
|
||||||
|
assertNull(UrlUtils.getCharset(null));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue