From 7104fddded271b9b4d14167db11f4070226a83cd Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 4 Dec 2018 21:27:37 +0800 Subject: [PATCH] GalleryPageUrlParser supports non-strict url --- .../client/parser/GalleryPageUrlParser.java | 18 +++-- .../parser/GalleryPageUrlParserTest.java | 71 +++++++++++++++++++ 2 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParserTest.java diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParser.java index 41330f4ee..4f3ab78c0 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParser.java @@ -16,8 +16,8 @@ package com.hippo.ehviewer.client.parser; +import android.support.annotation.Nullable; import com.hippo.ehviewer.client.EhUrl; - import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -26,15 +26,25 @@ import java.util.regex.Pattern; */ public final class GalleryPageUrlParser { - public static final Pattern URL_PATTERN = Pattern.compile("https?://(?:" + - EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/s/(\\w+)/(\\d+)-(\\d+)"); + private static final Pattern URL_STRICT_PATTERN = Pattern.compile( + "https?://(?:" + EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/s/([0-9a-f]{10})/(\\d+)-(\\d+)"); + private static final Pattern URL_PATTERN = Pattern.compile( + "([0-9a-f]{10})/(\\d+)-(\\d+)"); + + @Nullable public static Result parse(String url) { + return parse(url, true); + } + + @Nullable + public static Result parse(String url, boolean strict) { if (url == null) { return null; } - Matcher m = URL_PATTERN.matcher(url); + Pattern pattern = strict ? URL_STRICT_PATTERN : URL_PATTERN; + Matcher m = pattern.matcher(url); if (m.find()) { Result result = new Result(); result.gid = Long.parseLong(m.group(2)); diff --git a/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParserTest.java b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParserTest.java new file mode 100644 index 000000000..e7e4c67c0 --- /dev/null +++ b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryPageUrlParserTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 2018 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.client.parser; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.Arrays; +import java.util.Collection; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class GalleryPageUrlParserTest { + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] { + { "https://e-hentai.org/s/7b87643838/530350-1", true, false, 530350, "7b87643838", 0}, + { "https://exhentai.org/s/7b87643838/530350-1", true, false, 530350, "7b87643838", 0}, + { "7b87643838/530350-1", true, true, 0, null, 0}, + + { "7b87643838/530350-1", false, false, 530350, "7b87643838", 0}, + { "s/7b87643838/530350-1", false, false, 530350, "7b87643838", 0}, + { "7b87643838/530350a-1", false, true, 0, null, 0}, + }); + } + + private String url; + private boolean strict; + private boolean isNull; + private long gid; + private String pToken; + private int page; + + public GalleryPageUrlParserTest(String url, boolean strict, boolean isNull, long gid, String pToken, int page) { + this.url = url; + this.strict = strict; + this.isNull = isNull; + this.gid = gid; + this.pToken = pToken; + this.page = page; + } + + @Test + public void testParse() { + GalleryPageUrlParser.Result result = GalleryPageUrlParser.parse(url, strict); + if (isNull) { + assertNull(result); + } else { + assertEquals(gid, result.gid); + assertEquals(pToken, result.pToken); + assertEquals(page, result.page); + } + } +}