Skip to content

Commit e74984f

Browse files
committed
squash: Add tests, create its own map.
1 parent 698944b commit e74984f

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/main/kotlin/org/jitsi/utils/TemplatedUrl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import java.net.URI
1919

2020
class TemplatedUrl(
2121
private val template: String,
22-
private val map: MutableMap<String, String> = mutableMapOf(),
22+
inMap: Map<String, String> = emptyMap(),
2323
private val requiredKeys: Set<String> = emptySet(),
2424
) {
25+
private val map = inMap.toMutableMap()
2526

2627
/** Saves the given key:value pair in the map. */
2728
fun set(key: String, value: String) {

src/test/kotlin/org/jitsi/utils/TemplatedUrlTest.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class TemplatedUrlTest : ShouldSpec({
4949
templatedUrl.set("param", "value")
5050

5151
templatedUrl.resolve(
52-
mapOf("host" to "example.com", "path" to "api/resource", "param" to "value")
53-
) shouldBe URI("wss://example.com/api/resource?param=value")
52+
mapOf("host" to "example2.com", "path" to "api/resource2", "param" to "value2")
53+
) shouldBe URI("wss://example2.com/api/resource2?param=value2")
5454
}
5555

5656
should("resolve with a new key-value pair without saving it") {
@@ -75,13 +75,32 @@ class TemplatedUrlTest : ShouldSpec({
7575
templatedUrl.resolve()
7676
}
7777
}
78+
79+
should("fail to resolve when some requiredKeys are not set") {
80+
val template = "https://{{host}}/{{path}}?optional={{optional}}"
81+
val templatedUrl = TemplatedUrl(template, requiredKeys = setOf("host", "path"))
82+
83+
templatedUrl.set("host", "example.com")
84+
85+
shouldThrow<IllegalArgumentException> {
86+
templatedUrl.resolve()
87+
}
88+
}
89+
7890
should("throw an exception when the URI is invalid") {
7991
val templatedUrl = TemplatedUrl("https://example.com/{{}}")
8092

8193
shouldThrow<URISyntaxException> {
8294
templatedUrl.resolve()
8395
}
8496
}
97+
should("throw an exception when the values lead to an invalid URI ") {
98+
val templatedUrl = TemplatedUrl("https://example.com/{{path}}")
99+
100+
shouldThrow<URISyntaxException> {
101+
templatedUrl.resolve("path", "}")
102+
}
103+
}
85104

86105
should("handle keys that appear multiple times") {
87106
val template = "https://{{host}}/{{path}}/{{path}}"
@@ -91,5 +110,12 @@ class TemplatedUrlTest : ShouldSpec({
91110

92111
templatedUrl.resolve() shouldBe URI("https://example.com/resource/resource")
93112
}
113+
should("not modify the map passed as a constructor parameter") {
114+
val m = mutableMapOf("key1" to "value1", "key2" to "value2")
115+
val templatedUrl = TemplatedUrl("https://example.com/{{key1}}/{{key2}}", m)
116+
templatedUrl.set("key1", "newValue1")
117+
templatedUrl.set("key2", "newValue2")
118+
m shouldBe mapOf("key1" to "value1", "key2" to "value2")
119+
}
94120
}
95121
})

0 commit comments

Comments
 (0)