Skip to content

Commit 60f9e32

Browse files
Merge pull request #2 from jmoennich/master
Minimally invasive approach for skipping query parameters in UriTemplate
2 parents 92b2c2d + 32fe546 commit 60f9e32

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/main/kotlin/com/github/mduesterhoeft/router/UriTemplate.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class UriTemplate private constructor(private val template: String) {
1717
}
1818

1919
companion object {
20-
private val URI_TEMPLATE_FORMAT = "\\{([^}]+?)(?::([^}]+))?\\}".toRegex()
21-
fun from(template: String) = UriTemplate(template.trimSlashes())
20+
private val URI_TEMPLATE_FORMAT = "\\{([^}]+?)(?::([^}]+))?}".toRegex()
21+
fun from(template: String) = UriTemplate(template.split('?')[0].trimSlashes())
2222

2323
fun String.trimSlashes() = "^(/)?(.*?)(/)?$".toRegex().replace(this) { result -> result.groupValues[2] }
2424
}

src/test/kotlin/com/github/mduesterhoeft/router/UriTemplateTest.kt

+12
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,16 @@ class UriTemplateTest {
3434
.isEqualTo(mapOf("first" to "first-value", "second" to "second-value"))
3535
then(UriTemplate.from("/some").extract("/some")).isEmpty()
3636
}
37+
38+
@Test
39+
fun `should match with query parameter`() {
40+
then(UriTemplate.from("/some?a=1").matches("/some")).isTrue()
41+
then(UriTemplate.from("/some?a=1&b=2").matches("/some")).isTrue()
42+
}
43+
44+
@Test
45+
fun `should match with path parameter and query parameter`() {
46+
then(UriTemplate.from("/some/{id}?a=1").matches("/some/${UUID.randomUUID()}")).isTrue()
47+
then(UriTemplate.from("/some/{id}/other?a=1&b=2").matches("/some/${UUID.randomUUID()}/other")).isTrue()
48+
}
3749
}

0 commit comments

Comments
 (0)