From f79ff509bdeef496bb46d0b8c119e65223dd9c38 Mon Sep 17 00:00:00 2001 From: Javi Rueda Date: Fri, 2 Oct 2020 13:34:38 +0200 Subject: [PATCH] Support some lbry hosts to deep links. Add unit tests (#1008) * Support lbry.tv and other supported hosts for deep links. Add unit tests for LbryUri parse() method * add unit test for lbry protocol schema * Add unit test for lbry.tv deep-link with only channel --- app/src/main/AndroidManifest.xml | 4 + .../java/io/lbry/browser/utils/LbryUri.java | 13 +- .../io/lbry/browser/utils/LbryUriTest.java | 130 ++++++++++++++++++ 3 files changed, 143 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/io/lbry/browser/utils/LbryUriTest.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 145b4940..306751c9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -68,6 +68,10 @@ + + + + diff --git a/app/src/main/java/io/lbry/browser/utils/LbryUri.java b/app/src/main/java/io/lbry/browser/utils/LbryUri.java index 94458cfa..7b543bb6 100644 --- a/app/src/main/java/io/lbry/browser/utils/LbryUri.java +++ b/app/src/main/java/io/lbry/browser/utils/LbryUri.java @@ -18,7 +18,7 @@ public class LbryUri { public static final int CLAIM_ID_MAX_LENGTH = 40; private static final String REGEX_PART_PROTOCOL = "^((?:lbry://|https://)?)"; - private static final String REGEX_PART_HOST = "((?:open.lbry.com/)?)"; + private static final String REGEX_PART_HOST = "((?:open.lbry.com/|lbry.tv/|lbry.lat/|lbry.fr/|lbry.in/)?)"; private static final String REGEX_PART_STREAM_OR_CHANNEL_NAME = "([^:$#/]*)"; private static final String REGEX_PART_MODIFIER_SEPARATOR = "([:$#]?)([^/]*)"; private static final String QUERY_STRING_BREAKER = "^([\\S]+)([?][\\S]*)"; @@ -128,9 +128,14 @@ public class LbryUri { boolean isChannel = includesChannel && Helper.isNullOrEmpty(possibleStreamName); String channelName = includesChannel && streamOrChannelName.length() > 1 ? streamOrChannelName.substring(1) : null; - // It would have thrown already on the RegEx parser if protocol value was incorrect - // open.lbry.com uses ':' as ModSeparators while lbry:// expects '#' - if (components.get(1).equals("open.lbry.com/")) { + /* + * It would have thrown already on the RegEx parser if protocol value was incorrect + * or HTTPS host was unknown to us. + * + * [https://] hosts use ':' as ModSeparators while [lbry://] protocol expects '#' + */ + + if (!components.get(1).isEmpty()) { if (primaryModSeparator.equals(":")) primaryModSeparator = "#"; if (secondaryModSeparator.equals(":")) diff --git a/app/src/test/java/io/lbry/browser/utils/LbryUriTest.java b/app/src/test/java/io/lbry/browser/utils/LbryUriTest.java new file mode 100644 index 00000000..36ce5b49 --- /dev/null +++ b/app/src/test/java/io/lbry/browser/utils/LbryUriTest.java @@ -0,0 +1,130 @@ +package io.lbry.browser.utils; + +import org.jetbrains.annotations.NotNull; +import org.junit.Before; +import org.junit.Test; + +import io.lbry.browser.exceptions.LbryUriException; + +import static org.junit.Assert.assertEquals; + +public class LbryUriTest { + private LbryUri expected; + + /* + * Create an LbryUri object and assign fields manually using class methods. This object will be + * compared with LbryUri.parse() returned object on each test. + */ + @Before + public void createExpected() { + expected = new LbryUri(); + expected.setChannelName("@lbry"); + expected.setStreamName("lbryturns4"); + + try { + LbryUri.UriModifier primaryMod = LbryUri.UriModifier.parse("#", "3f"); + LbryUri.UriModifier secondaryMod = LbryUri.UriModifier.parse("#", "6"); + expected.setChannelClaimId(primaryMod.getClaimId()); + expected.setStreamClaimId(secondaryMod.getClaimId()); + } catch (LbryUriException e) { + e.printStackTrace(); + } + } + + @Test + public void parseOpenLbryComWithChannel() { + LbryUri obtained = new LbryUri(); + + try { + obtained = LbryUri.parse("https://open.lbry.com/@lbry:3f/lbryturns4:6",false); + } catch (LbryUriException e) { + e.printStackTrace(); + } + + assertEquals(expected, obtained); + } + + @Test + public void parseLbryTvWithChannel() { + LbryUri obtained = new LbryUri(); + + try { + obtained = LbryUri.parse("https://lbry.tv/@lbry:3f/lbryturns4:6",false); + } catch (LbryUriException e) { + e.printStackTrace(); + } + + assertEquals(expected, obtained); + } + + @Test + public void parseLbryAlterWithChannel() { + LbryUri obtained = new LbryUri(); + + try { + obtained = LbryUri.parse("https://lbry.lat/@lbry:3f/lbryturns4:6",false); + } catch (LbryUriException e) { + e.printStackTrace(); + } + + assertEquals(expected, obtained); + } + + @Test + public void parseLbryProtocolWithChannel() { + LbryUri obtained = new LbryUri(); + + try { + obtained = LbryUri.parse("lbry://@lbry#3f/lbryturns4#6",false); + } catch (LbryUriException e) { + e.printStackTrace(); + } + + assertEquals(expected, obtained); + } + + @Test + public void parseLbryProtocolOnlyChannel() { + LbryUri expectedForChannel = sinthesizeExpected(); + + LbryUri obtained = new LbryUri(); + + try { + obtained = LbryUri.parse("lbry://@UCBerkeley#d",false); + } catch (LbryUriException e) { + e.printStackTrace(); + } + + assertEquals(expectedForChannel, obtained); + } + + @Test + public void parseLbryTvProtocolOnlyChannel() { + LbryUri expectedForChannel = sinthesizeExpected(); + + LbryUri obtained = new LbryUri(); + + try { + obtained = LbryUri.parse("https://lbry.tv/@UCBerkeley:d",false); + } catch (LbryUriException e) { + e.printStackTrace(); + } + + assertEquals(expectedForChannel, obtained); + } + + @NotNull + private LbryUri sinthesizeExpected() { + LbryUri expectedForChannel = new LbryUri(); + expectedForChannel.setChannelName("@UCBerkeley"); + expectedForChannel.setChannel(true); + + try { + LbryUri.UriModifier primaryMod = LbryUri.UriModifier.parse("#", "d"); + expectedForChannel.setChannelClaimId(primaryMod.getClaimId()); + } catch (LbryUriException e) { + e.printStackTrace(); + } + return expectedForChannel; + } +} \ No newline at end of file