Always parse feed and items in the same time to detect malformed feeds

This commit is contained in:
Shinokuni 2020-10-04 22:17:52 +02:00
parent 101753a1a7
commit 63497bd049
3 changed files with 13 additions and 14 deletions

View File

@ -51,7 +51,7 @@ class LocalRSSDataSourceTest {
.addHeader(LibUtils.LAST_MODIFIED_HEADER, "Last-Modified")
.setBody(Buffer().readFrom(stream)))
val pair = localRSSDataSource.queryRSSResource(url.toString(), null, true)
val pair = localRSSDataSource.queryRSSResource(url.toString(), null)
val feed = pair?.first!!
assertEquals(feed.name, "Hacker News")
@ -74,7 +74,7 @@ class LocalRSSDataSourceTest {
.setBody(Buffer().readFrom(stream)))
val headers = Headers.headersOf(LibUtils.ETAG_HEADER, "ETag", LibUtils.LAST_MODIFIED_HEADER, "Last-Modified")
localRSSDataSource.queryRSSResource(url.toString(), headers, false)
localRSSDataSource.queryRSSResource(url.toString(), headers)
val request = mockServer.takeRequest()
@ -90,7 +90,7 @@ class LocalRSSDataSourceTest {
.addHeader(LibUtils.CONTENT_TYPE_HEADER, "application/feed+json")
.setBody(Buffer().readFrom(stream)))
val pair = localRSSDataSource.queryRSSResource(url.toString(), null, true)!!
val pair = localRSSDataSource.queryRSSResource(url.toString(), null)!!
assertEquals(pair.first.name, "News from Flying Meat")
assertEquals(pair.second.size, 10)
@ -100,7 +100,7 @@ class LocalRSSDataSourceTest {
fun response304Test() {
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED))
val pair = localRSSDataSource.queryRSSResource(url.toString(), null, false)
val pair = localRSSDataSource.queryRSSResource(url.toString(), null)
assertNull(pair)
}
@ -109,14 +109,14 @@ class LocalRSSDataSourceTest {
fun response404Test() {
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND))
localRSSDataSource.queryRSSResource(url.toString(), null, false)
localRSSDataSource.queryRSSResource(url.toString(), null)
}
@Test(expected = ParseException::class)
fun noContentTypeTest() {
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK))
localRSSDataSource.queryRSSResource(url.toString(), null, false)
localRSSDataSource.queryRSSResource(url.toString(), null)
}
@Test(expected = ParseException::class)
@ -124,7 +124,7 @@ class LocalRSSDataSourceTest {
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
.addHeader("Content-Type", ""))
localRSSDataSource.queryRSSResource(url.toString(), null, false)
localRSSDataSource.queryRSSResource(url.toString(), null)
}
@Test(expected = UnknownFormatException::class)
@ -133,7 +133,7 @@ class LocalRSSDataSourceTest {
.addHeader("Content-Type", "application/xml")
.setBody("<html> </html>"))
localRSSDataSource.queryRSSResource(url.toString(), null, false)
localRSSDataSource.queryRSSResource(url.toString(), null)
}
@Test

View File

@ -27,12 +27,11 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) {
* Query RSS url
* @param url url to query
* @param headers request headers
* @param withItems parse items with their feed
* @return a Feed object with its items if specified by [withItems]
* @return a Feed object with its items
*/
@Throws(ParseException::class, UnknownFormatException::class, NetworkErrorException::class, IOException::class)
@WorkerThread
fun queryRSSResource(url: String, headers: Headers?, withItems: Boolean): Pair<Feed, List<Item>>? {
fun queryRSSResource(url: String, headers: Headers?): Pair<Feed, List<Item>>? {
val response = queryUrl(url, headers)
return when {
@ -54,7 +53,7 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) {
if (type == LocalRSSHelper.RSSType.UNKNOWN) throw UnknownFormatException("Unable to guess $url RSS type")
val feed = parseFeed(ByteArrayInputStream(bodyArray), type, response)
val items = if (withItems) parseItems(ByteArrayInputStream(bodyArray), type) else listOf()
val items = parseItems(ByteArrayInputStream(bodyArray), type)
response.body?.close()
Pair(feed, items)

View File

@ -81,7 +81,7 @@ public class LocalFeedRepository extends ARepository<Void> {
headers.add(LibUtils.IF_MODIFIED_HEADER, feed.getLastModified());
}
Pair<Feed, List<Item>> pair = dataSource.queryRSSResource(feed.getUrl(), headers.build(), true);
Pair<Feed, List<Item>> pair = dataSource.queryRSSResource(feed.getUrl(), headers.build());
if (pair != null) {
insertNewItems(feed, pair.getSecond());
@ -105,7 +105,7 @@ public class LocalFeedRepository extends ARepository<Void> {
try {
Pair<Feed, List<Item>> pair = dataSource.queryRSSResource(parsingResult.getUrl(),
null, false);
null);
Feed feed = insertFeed(pair.getFirst(), parsingResult);
if (feed != null) {