diff --git a/app/src/main/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImpl.kt b/app/src/main/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImpl.kt index e040d25..15c155d 100644 --- a/app/src/main/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImpl.kt +++ b/app/src/main/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImpl.kt @@ -12,7 +12,17 @@ class ShareRecipeRepoImpl @Inject constructor( override suspend fun saveRecipeByURL(url: CharSequence): String { logger.v { "saveRecipeByURL() called with: url = $url" } - val request = ParseRecipeURLInfo(url = url.toString(), includeTags = true) + + val urlStart = url.indexOf("http") + if (urlStart == -1) throw IllegalArgumentException("URL doesn't start with http") + + val startsWithUrl = url.subSequence(urlStart, url.length) + val urlEnd = startsWithUrl.indexOfFirst { it.isWhitespace() } + .takeUnless { it == -1 } + ?: startsWithUrl.length + + val urlString = startsWithUrl.substring(0, urlEnd) + val request = ParseRecipeURLInfo(url = urlString, includeTags = true) return parseRecipeDataSource.parseRecipeFromURL(request) } } \ No newline at end of file diff --git a/app/src/test/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImplTest.kt b/app/src/test/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImplTest.kt new file mode 100644 index 0000000..2a83c9b --- /dev/null +++ b/app/src/test/java/gq/kirmanak/mealient/data/share/ShareRecipeRepoImplTest.kt @@ -0,0 +1,70 @@ +package gq.kirmanak.mealient.data.share + +import gq.kirmanak.mealient.test.BaseUnitTest +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.impl.annotations.MockK +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest +import org.junit.Test + +@OptIn(ExperimentalCoroutinesApi::class) +class ShareRecipeRepoImplTest : BaseUnitTest() { + + + @MockK(relaxUnitFun = true) + lateinit var parseRecipeDataSource: ParseRecipeDataSource + + lateinit var subject: ShareRecipeRepo + + override fun setUp() { + super.setUp() + subject = ShareRecipeRepoImpl(logger, parseRecipeDataSource) + coEvery { parseRecipeDataSource.parseRecipeFromURL(any()) } returns "" + } + + @Test(expected = IllegalArgumentException::class) + fun `when url is empty expect saveRecipeByURL throws Exception`() = runTest { + subject.saveRecipeByURL("") + } + + @Test + fun `when url is correct expect saveRecipeByURL saves it`() = runTest { + subject.saveRecipeByURL("https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/") + val expected = ParseRecipeURLInfo( + url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/", + includeTags = true + ) + coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) } + } + + @Test + fun `when url has prefix expect saveRecipeByURL removes it`() = runTest { + subject.saveRecipeByURL("My favorite recipe: https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/") + val expected = ParseRecipeURLInfo( + url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/", + includeTags = true + ) + coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) } + } + + @Test + fun `when url has suffix expect saveRecipeByURL removes it`() = runTest { + subject.saveRecipeByURL("https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/ is my favorite recipe") + val expected = ParseRecipeURLInfo( + url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/", + includeTags = true + ) + coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) } + } + + @Test + fun `when url has prefix and suffix expect saveRecipeByURL removes them`() = runTest { + subject.saveRecipeByURL("Actually, https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/ is my favorite recipe") + val expected = ParseRecipeURLInfo( + url = "https://www.allrecipes.com/recipe/215447/dads-leftover-turkey-pot-pie/", + includeTags = true + ) + coVerify { parseRecipeDataSource.parseRecipeFromURL(eq(expected)) } + } +} \ No newline at end of file