Skip to content

Commit d2aebc6

Browse files
committed
Make getCharset to support null parameter.
1 parent 7d2d224 commit d2aebc6

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/utils/UrlUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public static List<String> convertToUrls(Collection<Request> requests) {
116116
private static final Pattern patternForCharset = Pattern.compile("charset\\s*=\\s*['\"]*([^\\s;'\"]*)", Pattern.CASE_INSENSITIVE);
117117

118118
public static String getCharset(String contentType) {
119+
if (contentType == null) {
120+
return null;
121+
}
122+
119123
Matcher matcher = patternForCharset.matcher(contentType);
120124
if (matcher.find()) {
121125
String charset = matcher.group(1);

webmagic-core/src/test/java/us/codecraft/webmagic/utils/UrlUtilsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package us.codecraft.webmagic.utils;
22

3+
import static org.junit.Assert.assertNull;
4+
35
import org.junit.Assert;
46
import org.junit.Test;
57

@@ -43,5 +45,9 @@ public void testGetDomain(){
4345
Assert.assertEquals("www.dianping.com",UrlUtils.getDomain(url));
4446
}
4547

48+
@Test
49+
public void testGetCharset() {
50+
assertNull(UrlUtils.getCharset(null));
51+
}
4652

4753
}

0 commit comments

Comments
 (0)