Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class APIXMLParser: NSObject, XMLParserDelegate {
}
currentPost.artworkURL = url
case "enclosure":
guard let url = attributes?["url"] else {
guard let url = attributes?["url"],
attributes?["type"]?.hasPrefix("audio/") == true else {
return
}
currentPost.podcastURL = url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,70 @@ struct XMLParserTests {
}
}

@Test("Image enclosure from search feed is not treated as a podcast URL")
func parseSearchXMLImageEnclosureIsNotPodcastURL() async throws {
// Given
let xml = """
<?xml version="1.0"?>
<rss><channel><item>
<post-id>123</post-id>
<title>News item</title>
<link>https://macmagazine.com.br/post</link>
<enclosure url="https://macmagazine.com.br/image.jpg" length="12345" type="image/jpeg" />
</item></channel></rss>
"""
let data = Data(xml.utf8)

// When
let posts = try await withCheckedThrowingContinuation { continuation in
let parser = XMLParser(data: data)
let apiParser = APIXMLParser(
numberOfPosts: -1,
category: "",
parseFullContent: false,
continuation: continuation
)
parser.delegate = apiParser
parser.parse()
} as [XMLPost]

// Then
let post = try #require(posts.first)
#expect(post.podcastURL.isEmpty, "Image enclosure must not populate podcastURL")
}

@Test("Audio enclosure from search feed is treated as a podcast URL")
func parseSearchXMLAudioEnclosureIsPodcastURL() async throws {
// Given
let xml = """
<?xml version="1.0"?>
<rss><channel><item>
<post-id>456</post-id>
<title>Podcast item</title>
<link>https://macmagazine.com.br/podcast</link>
<enclosure url="https://feeds.soundcloud.com/stream/episode.mp3" length="67890" type="audio/mpeg" />
</item></channel></rss>
"""
let data = Data(xml.utf8)

// When
let posts = try await withCheckedThrowingContinuation { continuation in
let parser = XMLParser(data: data)
let apiParser = APIXMLParser(
numberOfPosts: -1,
category: "",
parseFullContent: false,
continuation: continuation
)
parser.delegate = apiParser
parser.parse()
} as [XMLPost]

// Then
let post = try #require(posts.first)
#expect(post.podcastURL == "https://feeds.soundcloud.com/stream/episode.mp3")
}

// MARK: - Error Handling Tests

@Test("Parse invalid XML throws error")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ private let filterKeyToCategory: [String: NewsCategory] = {
var map = [String: NewsCategory]()
for category in NewsCategory.allCases {
map[category.filterKey] = category
map[category.rawValue] = category
}
return map
}()

public extension Array where Element == String {
var toNewsCategory: [NewsCategory] {
compactMap { filterKeyToCategory[$0] }
var seen = Set<NewsCategory>()
return compactMap { filterKeyToCategory[$0] }.filter { seen.insert($0).inserted }
}
}
Loading