Skip to content

Commit

Permalink
Validate that repeating named param always has the same value (#5)
Browse files Browse the repository at this point in the history
* Ensure equality of similarly named regex paths

* fun interface Action
  • Loading branch information
kingsleyadio committed Jan 24, 2022
1 parent 352de4b commit 8c94208
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion deeplink/src/main/java/com/kingsleyadio/deeplink/Action.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.kingsleyadio.deeplink

interface Action<out T> {
fun interface Action<out T> {

fun run(uri: DeepLinkUri, params: Map<String, String>, env: Environment): T
}
5 changes: 3 additions & 2 deletions deeplink/src/main/java/com/kingsleyadio/deeplink/BaseRoute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ abstract class BaseRoute<out T>(private vararg val routes: String) : Action<T> {
when {
routePart.startsWith(":") -> {
val (key, value) = resolveParameterizedPath(routePart, inPart) ?: return@forEach
val parameterKey = key.takeUnless { it.isEmpty() } ?: return@zip
params[parameterKey] = value
key.ifEmpty { return@zip }
if (key in params && params[key] != value) return@forEach
else params[key] = value
}
routePart == "*" -> return@zip
routePart != inPart -> return@forEach
Expand Down
17 changes: 17 additions & 0 deletions deeplink/src/test/java/com/kingsleyadio/deeplink/BaseRouteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ class BaseRouteTest {
assertFalse(RegexPathRoute.matchWith(uri).isMatch)
}

@Test
fun matchWith_regexPathResolution_equalNamedPaths() {
var uri = DeepLinkUri.parse("http://www.example.com/recipes/1234/1234")
var res = EqualNamedPathRoute.matchWith(uri)
assertTrue(res.isMatch)
assertEquals(1, res.params.size)

uri = DeepLinkUri.parse("http://www.example.com/recipes/1234/5678")
res = EqualNamedPathRoute.matchWith(uri)
assertFalse(res.isMatch)
}

@Test
fun matchWith_regexPathResolutionUnnamed() {
val uri = DeepLinkUri.parse("http://www.example.com/recipe/abc-1234")
Expand Down Expand Up @@ -189,4 +201,9 @@ class BaseRouteTest {

override fun run(uri: DeepLinkUri, params: Map<String, String>, env: Environment) = Unit
}

object EqualNamedPathRoute : BaseRoute<Unit>("recipes/:id(\\w{4})/:id") {

override fun run(uri: DeepLinkUri, params: Map<String, String>, env: Environment) = Unit
}
}

0 comments on commit 8c94208

Please sign in to comment.