mirror of
https://github.com/readrops/Readrops.git
synced 2025-02-03 03:57:36 +01:00
Always parse feed and items in the same time to detect malformed feeds
This commit is contained in:
parent
101753a1a7
commit
63497bd049
@ -51,7 +51,7 @@ class LocalRSSDataSourceTest {
|
|||||||
.addHeader(LibUtils.LAST_MODIFIED_HEADER, "Last-Modified")
|
.addHeader(LibUtils.LAST_MODIFIED_HEADER, "Last-Modified")
|
||||||
.setBody(Buffer().readFrom(stream)))
|
.setBody(Buffer().readFrom(stream)))
|
||||||
|
|
||||||
val pair = localRSSDataSource.queryRSSResource(url.toString(), null, true)
|
val pair = localRSSDataSource.queryRSSResource(url.toString(), null)
|
||||||
val feed = pair?.first!!
|
val feed = pair?.first!!
|
||||||
|
|
||||||
assertEquals(feed.name, "Hacker News")
|
assertEquals(feed.name, "Hacker News")
|
||||||
@ -74,7 +74,7 @@ class LocalRSSDataSourceTest {
|
|||||||
.setBody(Buffer().readFrom(stream)))
|
.setBody(Buffer().readFrom(stream)))
|
||||||
|
|
||||||
val headers = Headers.headersOf(LibUtils.ETAG_HEADER, "ETag", LibUtils.LAST_MODIFIED_HEADER, "Last-Modified")
|
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()
|
val request = mockServer.takeRequest()
|
||||||
|
|
||||||
@ -90,7 +90,7 @@ class LocalRSSDataSourceTest {
|
|||||||
.addHeader(LibUtils.CONTENT_TYPE_HEADER, "application/feed+json")
|
.addHeader(LibUtils.CONTENT_TYPE_HEADER, "application/feed+json")
|
||||||
.setBody(Buffer().readFrom(stream)))
|
.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.first.name, "News from Flying Meat")
|
||||||
assertEquals(pair.second.size, 10)
|
assertEquals(pair.second.size, 10)
|
||||||
@ -100,7 +100,7 @@ class LocalRSSDataSourceTest {
|
|||||||
fun response304Test() {
|
fun response304Test() {
|
||||||
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_MODIFIED))
|
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)
|
assertNull(pair)
|
||||||
}
|
}
|
||||||
@ -109,14 +109,14 @@ class LocalRSSDataSourceTest {
|
|||||||
fun response404Test() {
|
fun response404Test() {
|
||||||
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND))
|
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_NOT_FOUND))
|
||||||
|
|
||||||
localRSSDataSource.queryRSSResource(url.toString(), null, false)
|
localRSSDataSource.queryRSSResource(url.toString(), null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ParseException::class)
|
@Test(expected = ParseException::class)
|
||||||
fun noContentTypeTest() {
|
fun noContentTypeTest() {
|
||||||
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK))
|
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK))
|
||||||
|
|
||||||
localRSSDataSource.queryRSSResource(url.toString(), null, false)
|
localRSSDataSource.queryRSSResource(url.toString(), null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = ParseException::class)
|
@Test(expected = ParseException::class)
|
||||||
@ -124,7 +124,7 @@ class LocalRSSDataSourceTest {
|
|||||||
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
|
mockServer.enqueue(MockResponse().setResponseCode(HttpURLConnection.HTTP_OK)
|
||||||
.addHeader("Content-Type", ""))
|
.addHeader("Content-Type", ""))
|
||||||
|
|
||||||
localRSSDataSource.queryRSSResource(url.toString(), null, false)
|
localRSSDataSource.queryRSSResource(url.toString(), null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = UnknownFormatException::class)
|
@Test(expected = UnknownFormatException::class)
|
||||||
@ -133,7 +133,7 @@ class LocalRSSDataSourceTest {
|
|||||||
.addHeader("Content-Type", "application/xml")
|
.addHeader("Content-Type", "application/xml")
|
||||||
.setBody("<html> </html>"))
|
.setBody("<html> </html>"))
|
||||||
|
|
||||||
localRSSDataSource.queryRSSResource(url.toString(), null, false)
|
localRSSDataSource.queryRSSResource(url.toString(), null)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -27,12 +27,11 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) {
|
|||||||
* Query RSS url
|
* Query RSS url
|
||||||
* @param url url to query
|
* @param url url to query
|
||||||
* @param headers request headers
|
* @param headers request headers
|
||||||
* @param withItems parse items with their feed
|
* @return a Feed object with its items
|
||||||
* @return a Feed object with its items if specified by [withItems]
|
|
||||||
*/
|
*/
|
||||||
@Throws(ParseException::class, UnknownFormatException::class, NetworkErrorException::class, IOException::class)
|
@Throws(ParseException::class, UnknownFormatException::class, NetworkErrorException::class, IOException::class)
|
||||||
@WorkerThread
|
@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)
|
val response = queryUrl(url, headers)
|
||||||
|
|
||||||
return when {
|
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")
|
if (type == LocalRSSHelper.RSSType.UNKNOWN) throw UnknownFormatException("Unable to guess $url RSS type")
|
||||||
|
|
||||||
val feed = parseFeed(ByteArrayInputStream(bodyArray), type, response)
|
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()
|
response.body?.close()
|
||||||
Pair(feed, items)
|
Pair(feed, items)
|
||||||
|
@ -81,7 +81,7 @@ public class LocalFeedRepository extends ARepository<Void> {
|
|||||||
headers.add(LibUtils.IF_MODIFIED_HEADER, feed.getLastModified());
|
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) {
|
if (pair != null) {
|
||||||
insertNewItems(feed, pair.getSecond());
|
insertNewItems(feed, pair.getSecond());
|
||||||
@ -105,7 +105,7 @@ public class LocalFeedRepository extends ARepository<Void> {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Pair<Feed, List<Item>> pair = dataSource.queryRSSResource(parsingResult.getUrl(),
|
Pair<Feed, List<Item>> pair = dataSource.queryRSSResource(parsingResult.getUrl(),
|
||||||
null, false);
|
null);
|
||||||
Feed feed = insertFeed(pair.getFirst(), parsingResult);
|
Feed feed = insertFeed(pair.getFirst(), parsingResult);
|
||||||
|
|
||||||
if (feed != null) {
|
if (feed != null) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user