Replace RSS2 atom:link element value by response url, as atom:link element is unreliable

This commit is contained in:
Shinokuni 2020-09-29 23:30:51 +02:00
parent 7eecbc8e8b
commit 872a894db0
2 changed files with 6 additions and 3 deletions

View File

@ -32,7 +32,7 @@ class LocalRSSDataSourceTest {
@Before
fun before() {
mockServer.start()
mockServer.start(8080)
url = mockServer.url("/rss")
}
@ -55,6 +55,7 @@ class LocalRSSDataSourceTest {
val feed = pair?.first!!
assertEquals(feed.name, "Hacker News")
assertEquals(feed.url, "http://localhost:8080/rss")
assertEquals(feed.siteUrl, "https://news.ycombinator.com/")
assertEquals(feed.description, "Links for the intellectually curious, ranked by readers.")

View File

@ -98,7 +98,7 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) {
val adapter = Moshi.Builder()
.add(JSONFeedAdapter())
.build()
.adapter<Feed>(Feed::class.java)
.adapter(Feed::class.java)
adapter.fromJson(Buffer().readFrom(stream))!!
}
@ -129,7 +129,9 @@ class LocalRSSDataSource(private val httpClient: OkHttpClient) {
private fun handleSpecialCases(feed: Feed, type: LocalRSSHelper.RSSType, response: Response) {
with(feed) {
if (type == LocalRSSHelper.RSSType.RSS_2) {
if (url == null) url = response.request.url.toString()
// if an atom:link element was parsed, we still replace its value as it is unreliable,
// otherwise we just add the rss url
url = response.request.url.toString()
} else if (type == LocalRSSHelper.RSSType.ATOM) {
if (url == null) url = response.request.url.toString()
if (siteUrl == null) siteUrl = response.request.url.scheme + "://" + response.request.url.host