Private
Public Access
1
0

[android] backend: normalize base url

This commit is contained in:
2026-04-12 11:26:38 -07:00
parent fd3660858e
commit 7056a7f836
5 changed files with 32 additions and 7 deletions

View File

@@ -104,8 +104,10 @@ class APIClientFactory {
return InvalidConfigurationAPIClient(InvalidConfigurationAPIClient.Issue.NOT_CONFIGURED)
}
val normalizedServerString = serverString.ensureTrailingSlash()
// Try to parse server string
val serverURL = HttpUrl.parse(serverString)
val serverURL = HttpUrl.parse(normalizedServerString)
?: return InvalidConfigurationAPIClient(InvalidConfigurationAPIClient.Issue.INVALID_HOST_URL)
return RetrofitAPIClient(serverURL.url(), authentication)
@@ -113,6 +115,10 @@ class APIClientFactory {
}
}
private fun String.ensureTrailingSlash(): String {
return if (endsWith("/")) this else "$this/"
}
// TODO: Is this a dumb idea?
class InvalidConfigurationAPIClient(val issue: Issue): APIClient {
enum class Issue {
@@ -215,4 +221,4 @@ fun URL.authenticatedWebSocketURL(serverPath: String, params: Map<String, String
}
return URL(requestURL.build().toString())
}
}

View File

@@ -11,6 +11,7 @@ import net.buzzert.kordophone.backend.db.CachedChatDatabase
import net.buzzert.kordophone.backend.model.Message
import net.buzzert.kordophone.backend.model.OutgoingMessage
import net.buzzert.kordophone.backend.server.APIClient
import net.buzzert.kordophone.backend.server.APIClientFactory
import net.buzzert.kordophone.backend.server.APIInterface
import net.buzzert.kordophone.backend.server.Authentication
import net.buzzert.kordophone.backend.server.ChatRepository
@@ -38,6 +39,16 @@ class BackendTests {
return Pair(repository, mockServer)
}
@Test
fun testCreateClientAcceptsBaseUrlWithoutTrailingSlash() {
val client = APIClientFactory.createClient(
"https://example.com/api",
Authentication("test", "test")
)
assertTrue(client.isConfigured)
}
@Test
fun testGetVersion() = runBlocking {
val (repository, mockServer) = mockRepository()
@@ -342,4 +353,4 @@ class BackendTests {
assertEquals(messagesToGenerate, allMessages.count())
}
}
}
}