Skip to content

Commit

Permalink
Fix baseUrl must end in / crash
Browse files Browse the repository at this point in the history
  • Loading branch information
lneugebauer committed Oct 13, 2023
1 parent 47b3b80 commit 33933a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.lukasneugebauer.nextcloudcookbook.core.util

fun String.addSuffix(suffix: String, ignoreEmpty: Boolean = true): String {
if ((this.isEmpty() && ignoreEmpty) || this.endsWith(suffix)) {
return this
}
return "$this$suffix"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import de.lukasneugebauer.nextcloudcookbook.core.data.api.NcCookbookApi
import de.lukasneugebauer.nextcloudcookbook.core.data.remote.BasicAuthInterceptor
import de.lukasneugebauer.nextcloudcookbook.core.data.remote.NetworkInterceptor
import de.lukasneugebauer.nextcloudcookbook.core.domain.model.NcAccount
import de.lukasneugebauer.nextcloudcookbook.core.util.addSuffix
import de.lukasneugebauer.nextcloudcookbook.recipe.data.dto.NutritionDto
import de.lukasneugebauer.nextcloudcookbook.recipe.data.remote.deserializer.NutritionDeserializer
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -69,7 +70,7 @@ class ApiProvider @Inject constructor(
.build()

val retrofit = Retrofit.Builder()
.baseUrl(ncAccount.url)
.baseUrl(ncAccount.url.addSuffix("/"))
.client(client)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(NetworkResponseAdapterFactory())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.lukasneugebauer.nextcloudcookbook

import de.lukasneugebauer.nextcloudcookbook.core.util.addSuffix
import org.junit.Assert.assertEquals
import org.junit.Test

class StringAddSuffixUnitTest {
@Test
fun string_AddSlashAsSuffix_ReturnsString() {
val string = "https://cloud.example.tld".addSuffix("/")
assertEquals("https://cloud.example.tld/", string)
}

@Test
fun string_AddNoSuffix_ReturnsString() {
val string = "https://cloud.example.tld/".addSuffix("/")
assertEquals("https://cloud.example.tld/", string)
}
}

0 comments on commit 33933a4

Please sign in to comment.