From 54f1c4806f0aeeeb16620202766b32067aa46945 Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 4 Dec 2018 20:54:22 +0800 Subject: [PATCH] GalleryDetailUrlParser supports non-strict url --- .../client/parser/GalleryDetailUrlParser.java | 15 +++- .../parser/GalleryDetailUrlParserTest.java | 73 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParserTest.java diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.java index 75c0175e6..f9380fed1 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParser.java @@ -29,16 +29,25 @@ import java.util.regex.Pattern; */ public final class GalleryDetailUrlParser { - public static final Pattern URL_PATTERN = Pattern.compile("https?://(?:" + - EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/(?:g|mpv)/(\\d+)/(\\w+)"); + private static final Pattern URL_STRICT_PATTERN = Pattern.compile( + "https?://(?:" + EhUrl.DOMAIN_EX + "|" + EhUrl.DOMAIN_E + "|" + EhUrl.DOMAIN_LOFI + ")/(?:g|mpv)/(\\d+)/([0-9a-f]{10})"); + + private static final Pattern URL_PATTERN = Pattern.compile( + "(\\d+)/([0-9a-f]{10})"); @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(1)); diff --git a/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParserTest.java b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParserTest.java new file mode 100644 index 000000000..187e7bf28 --- /dev/null +++ b/app/src/test/java/com/hippo/ehviewer/client/parser/GalleryDetailUrlParserTest.java @@ -0,0 +1,73 @@ +/* + * 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 GalleryDetailUrlParserTest { + + @Parameterized.Parameters + public static Collection data() { + return Arrays.asList(new Object[][] { + { "https://e-hentai.org/g/530350/8b3c7e4a21/", true, false, 530350, "8b3c7e4a21" }, + { "https://exhentai.org/g/530350/8b3c7e4a21/", true, false, 530350, "8b3c7e4a21" }, + { "https://lofi.e-hentai.org/g/530350/8b3c7e4a21/", true, false, 530350, "8b3c7e4a21" }, + { "https://e-hentai.org/mpv/530350/8b3c7e4a21/", true, false, 530350, "8b3c7e4a21" }, + { "https://exhentai.org/mpv/530350/8b3c7e4a21/", true, false, 530350, "8b3c7e4a21" }, + { "https://lofi.e-hentai.org/mpv/530350/8b3c7e4a21/", true, false, 530350, "8b3c7e4a21" }, + { "https://e-hentai.org/mpv/530350/8b3c7e4a21/#page1", true, false, 530350, "8b3c7e4a21" }, + { "530350/8b3c7e4a21/#page1", true, true, 0, null }, + + { "530350/8b3c7e4a21", false, false, 530350, "8b3c7e4a21" }, + { "g/530350/8b3c7e4a21", false, false, 530350, "8b3c7e4a21" }, + { "530350b/8b3c7e4a21", false, true, 0, null }, + }); + } + + private String url; + private boolean strict; + private boolean isNull; + private long gid; + private String token; + + public GalleryDetailUrlParserTest(String url, boolean strict, boolean isNull, long gid, String token) { + this.url = url; + this.strict = strict; + this.isNull = isNull; + this.gid = gid; + this.token = token; + } + + @Test + public void testParse() { + GalleryDetailUrlParser.Result result = GalleryDetailUrlParser.parse(url, strict); + if (isNull) { + assertNull(result); + } else { + assertEquals(gid, result.gid); + assertEquals(token, result.token); + } + } +}