@@ -2,14 +2,130 @@ package io.homeassistant.companion.android.util
22
33import java.net.URL
44import kotlinx.coroutines.test.runTest
5+ import org.junit.jupiter.api.Assertions.assertEquals
56import org.junit.jupiter.api.Assertions.assertFalse
7+ import org.junit.jupiter.api.Assertions.assertNotNull
8+ import org.junit.jupiter.api.Assertions.assertNull
69import org.junit.jupiter.api.Assertions.assertTrue
10+ import org.junit.jupiter.api.BeforeEach
711import org.junit.jupiter.api.Test
812import org.junit.jupiter.params.ParameterizedTest
13+ import org.junit.jupiter.params.provider.CsvSource
914import org.junit.jupiter.params.provider.ValueSource
1015
1116class UrlUtilTest {
1217
18+ private lateinit var baseUrl: URL
19+
20+ @BeforeEach
21+ fun setUp () {
22+ baseUrl = URL (" https://example.com:8123/" )
23+ }
24+
25+ @ParameterizedTest
26+ @CsvSource(
27+ value = [
28+ " http://another.com/test,http://another.com/test" ,
29+ " https://secure.com/path,https://secure.com/path" ,
30+ " http://another.com:9000/path,http://another.com:9000/path" ,
31+ ],
32+ )
33+ fun `Given absolute URL when calling handle then returns parsed URL` (input : String , expected : String ) {
34+ val result = UrlUtil .handle(baseUrl, input)
35+
36+ assertNotNull(result)
37+ assertEquals(expected, result.toString())
38+ }
39+
40+ @ParameterizedTest
41+ @CsvSource(
42+ value = [
43+ " lovelace/default,https://example.com:8123/lovelace/default" ,
44+ " /lovelace/default,https://example.com:8123/lovelace/default" ,
45+ " lovelace/default?edit=1,https://example.com:8123/lovelace/default?edit=1" ,
46+ " lovelace/default#section,https://example.com:8123/lovelace/default#section" ,
47+ " lovelace/default?edit=1#section,https://example.com:8123/lovelace/default?edit=1#section" ,
48+ " api/states/light.living_room,https://example.com:8123/api/states/light.living_room" ,
49+ " lovelace?key=value&other=test,https://example.com:8123/lovelace?key=value&other=test" ,
50+ " path/with%20encoded%20spaces,https://example.com:8123/path/with%20encoded%20spaces" ,
51+ ],
52+ )
53+ fun `Given relative path when calling handle then returns URL resolved against base` (input : String , expected : String ) {
54+ val result = UrlUtil .handle(baseUrl, input)
55+
56+ assertNotNull(result)
57+ assertEquals(expected, result.toString())
58+ }
59+
60+ @Test
61+ fun `Given input with homeassistant navigate prefix and absolute URL when calling handle then treats as relative path without taking care of second host and protocol` () {
62+ val input = " homeassistant://navigate/https://example2.com/path/subpath"
63+
64+ val result = UrlUtil .handle(baseUrl, input)
65+
66+ assertNotNull(result)
67+ assertEquals(" https://example.com:8123/path/subpath" , result.toString())
68+ }
69+
70+ @Test
71+ fun `Given input with homeassistant navigate prefix and relative path when calling handle then returns resolved URL` () {
72+ val input = " homeassistant://navigate/lovelace/default"
73+
74+ val result = UrlUtil .handle(baseUrl, input)
75+
76+ assertNotNull(result)
77+ assertEquals(" https://example.com:8123/lovelace/default" , result.toString())
78+ }
79+
80+ @ParameterizedTest
81+ @ValueSource(
82+ strings = [
83+ " " ,
84+ " " ,
85+ ],
86+ )
87+ fun `Given empty or whitespace input when calling handle then returns base URL` (input : String ) {
88+ val result = UrlUtil .handle(baseUrl, input)
89+
90+ assertNotNull(result)
91+ assertEquals(" https://example.com:8123/" , result.toString())
92+ }
93+
94+ @Test
95+ fun `Given invalid URI input when calling handle then returns base URL` () {
96+ val input = " not a valid uri with spaces and bad chars <>"
97+
98+ val result = UrlUtil .handle(baseUrl, input)
99+
100+ assertEquals(baseUrl, result)
101+ }
102+
103+ @Test
104+ fun `Given null base and relative path when calling handle then returns null` () {
105+ val input = " lovelace/default"
106+
107+ val result = UrlUtil .handle(null , input)
108+
109+ assertNull(result)
110+ }
111+
112+ @Test
113+ fun `Given valid base url and invalid input URI that cannot be parsed into URL when calling handle then returns null` () {
114+ val input = " http://h:8123None"
115+
116+ assertNull(UrlUtil .handle(baseUrl, input))
117+ }
118+
119+ @Test
120+ fun `Given null base and absolute URL when calling handle then returns parsed URL` () {
121+ val input = " https://example.com/test"
122+
123+ val result = UrlUtil .handle(null , input)
124+
125+ assertNotNull(result)
126+ assertEquals(" https://example.com/test" , result.toString())
127+ }
128+
13129 @ParameterizedTest
14130 @ValueSource(
15131 strings = [
0 commit comments