Skip to content

Commit

Permalink
Fix matching issue with placeholders (#365)
Browse files Browse the repository at this point in the history
* Fix issue with url segments that are matching (from back) but longer than the text before the placeholder in the template.

* Fix issue with url segments that are matching (from back) but longer than the text before the placeholder in the template.

---------

Co-authored-by: Andreas Rossbacher <[email protected]>
  • Loading branch information
rossbacher and Andreas Rossbacher authored Jun 27, 2023
1 parent e23411a commit 22ac5cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,12 @@ private CompareResult compareComponentParam(
+ new String(placeholderValue), false);
} else return null;
}
if (byteArray[valueStartPos + j] != valueToMatch[k]) {
// We do the comparison from the back. If the value in the index (before the placeholder
// starts) is longer than valueToMatch we need to abort.
// e.g. template value: "{something}longer" and valueToMatch: "onger". As we compare
// from back all the chars match but the template value is longer so as soon as we reach
// the end of valueToMatch before we reach the placeholder end char we need to abort.
if (k < 0 || byteArray[valueStartPos + j] != valueToMatch[k]) {
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MatchIndexTests {
private val entryWithAllowedValues = DeepLinkEntry.MethodDeeplinkEntry("http{scheme(|s)}://www.example.{tld(de|fr)}/somePath1/differentPath2", MatchIndexTests::class.java.name, "someMethod10")
private val entryWithAllowedValueOnlyOneElement = DeepLinkEntry.MethodDeeplinkEntry("http{scheme(s)}://www.justtesting.com/somePath", MatchIndexTests::class.java.name, "someMethod10")
private val entryWithEmptyAllowedValue = DeepLinkEntry.MethodDeeplinkEntry("http{scheme()}://www.anothertest.com/somePath", MatchIndexTests::class.java.name, "someMethod10")
private val entryWithAllowedValueAndLongerValueThan = DeepLinkEntry.MethodDeeplinkEntry("scheme://{some_value(allowed|values)}somethinglonger/one/{param}/three", MatchIndexTests::class.java.name, "someMethod10")

private val allowedValuesPlaceholderNames = setOf("scheme", "tld")

Expand All @@ -23,9 +24,23 @@ class MatchIndexTests {
DeepLinkEntry.MethodDeeplinkEntry("http://example.com/path1/someOtherPathElement2", MatchIndexTests::class.java.name, "someMethod6"),
DeepLinkEntry.MethodDeeplinkEntry("http://example.com/", MatchIndexTests::class.java.name, "someMethod8"),
entryWithAllowedValues,
entryWithAllowedValueOnlyOneElement
entryWithAllowedValueOnlyOneElement,
entryWithAllowedValueAndLongerValueThan
).sortedBy { it.uriTemplate }

@Test
fun testMatchWithPlaceholderThatEndsInSegmentWithLongerMatch() {
// This is testing a condition where an URL segment (in this case the host) has a placeholder with allowed values
// in the template and is matching (from the back) to what is in the to be matched URL, where the entry in the template
// is matching but longer (and thus not matching) before the placeholder starts (from the back).
// In this case the host is it like this:
// template host: {some_value(allowed|values)}somethinglonger
// to match host: longer
val deepLinkUri = DeepLinkUri.parse("scheme://longer/one/param/three")
val matchEntry = testRegistry(testEntries).idxMatch(deepLinkUri, emptyMap())
assertThat(matchEntry).isNull()
}

@Test
fun testGetAllEntries() {
val allEntries = testRegistry(testEntries).getAllEntries().sortedBy { it.uriTemplate }
Expand Down

0 comments on commit 22ac5cd

Please sign in to comment.