Use UnknownFormatException instead of ParseException for some cases

This commit is contained in:
Shinokuni 2020-09-12 12:02:03 +02:00
parent 8089d9ae6c
commit 4bf56b8f7f
4 changed files with 8 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.readrops.api.utils.HttpManager
import com.readrops.api.utils.ParseException
import com.readrops.api.utils.UnknownFormatException
import junit.framework.TestCase.*
import okhttp3.HttpUrl
import okhttp3.mockwebserver.MockResponse
@ -81,7 +82,7 @@ class LocalRSSDataSourceTest {
localRSSDataSource.queryRSSResource(url.toString(), null, false)
}
@Test(expected = ParseException::class)
@Test(expected = UnknownFormatException::class)
fun badContentTest() {
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader("Content-Type", "application/xml")

View File

@ -42,7 +42,7 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) {
if (type == LocalRSSHelper.RSSType.UNKNOWN)
type = LocalRSSHelper.getRSSContentType(response.body?.byteStream()!!)
// if we can't guess type even with the content, we are unable to go further
if (type == LocalRSSHelper.RSSType.UNKNOWN) throw ParseException("Unable to guess $url RSS type")
if (type == LocalRSSHelper.RSSType.UNKNOWN) throw UnknownFormatException("Unable to guess $url RSS type")
val feed = parseFeed(response, type)
val items = if (withItems) parseItems(response.body?.byteStream()!!, type) else listOf()

View File

@ -1,6 +1,6 @@
package com.readrops.api.localfeed
import com.readrops.api.utils.ParseException
import com.readrops.api.utils.UnknownFormatException
import java.io.InputStream
import java.util.regex.Pattern
@ -12,7 +12,7 @@ object LocalRSSHelper {
private const val ATOM_CONTENT_TYPE = "application/atom+xml"
private const val JSON_CONTENT_TYPE = "application/json"
private const val HTML_CONTENT_TYPE = "text/html"
private const val RSS_2_REGEX = "rss.*version=\"2.0\""
private const val ATOM_REGEX = "<feed.* xmlns=\"http://www.w3.org/2005/Atom\""
@ -26,7 +26,7 @@ object LocalRSSHelper {
ATOM_CONTENT_TYPE -> RSSType.ATOM
JSON_CONTENT_TYPE -> RSSType.JSONFEED
RSS_TEXT_CONTENT_TYPE, RSS_APPLICATION_CONTENT_TYPE, HTML_CONTENT_TYPE -> RSSType.UNKNOWN
else -> throw ParseException("Unknown content type")
else -> throw UnknownFormatException("Unknown content type : $contentType")
}
}

View File

@ -1,6 +1,6 @@
package com.readrops.api.localfeed
import com.readrops.api.utils.ParseException
import com.readrops.api.utils.UnknownFormatException
import junit.framework.TestCase.assertEquals
import org.junit.Test
import java.io.ByteArrayInputStream
@ -27,7 +27,7 @@ class LocalRSSHelperTest {
LocalRSSHelper.RSSType.UNKNOWN)
}
@Test(expected = ParseException::class)
@Test(expected = UnknownFormatException::class)
fun nonSupportedContentTypeTest() {
LocalRSSHelper.getRSSType("image/jpeg")
}