// // HTMLLinkTests.swift // RSParser // // Created by Brent Simmons on 6/25/17. // Copyright © 2017 Ranchero Software, LLC. All rights reserved. // import Foundation import Testing import RSParser @Suite struct HTMLLinkTests { @Test func sixColorsLink() { let d = parserData("sixcolors", "html", "http://sixcolors.com/") let links = HTMLLinkParser.htmlLinks(with: d) let linkToFind = "https://www.theincomparable.com/theincomparable/290/index.php" let textToFind = "this week’s episode of The Incomparable" var found = false for oneLink in links { if let urlString = oneLink.urlString, let text = oneLink.text, urlString == linkToFind, text == textToFind { found = true } } #expect(found) #expect(links.count == 131) } // MARK: - Inline fixture tests @Test func linksInsideScriptAreIgnored() { // The scanner enters raw-text mode for real 2 """ let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) let links = HTMLLinkParser.htmlLinks(with: d) let urls = links.compactMap { $0.urlString } #expect(urls.contains("http://real.example.com/")) #expect(urls.contains("http://real2.example.com/")) #expect(!urls.contains("http://not-a-link.example.com/")) #expect(!urls.contains("http://also-not.example.com/")) } @Test func linksInsideStyleAreIgnored() { // real """ let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) let links = HTMLLinkParser.htmlLinks(with: d) let urls = links.compactMap { $0.urlString } #expect(urls == ["http://real.example.com/"]) } @Test func relativeURLsResolvedAgainstPageURL() { // The parser's second argument is the page URL; relative hrefs must // resolve against it so the caller gets absolute URLs back. let html = """ absolute path relative path already absolute """ let d = ParserData(url: "http://example.com/page/", data: Data(html.utf8)) let links = HTMLLinkParser.htmlLinks(with: d) let urls = links.compactMap { $0.urlString } #expect(urls.contains("http://example.com/foo")) #expect(urls.contains("http://example.com/page/bar.html")) #expect(urls.contains("http://other.example.com/x")) } @Test func unclosedTagsDoNotCrashAndStillFindLinks() { // Malformed markup: the scanner must not hang or lose later links. let html = """

Orphan bold italic first

Unclosed div second """ let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) let links = HTMLLinkParser.htmlLinks(with: d) let urls = links.compactMap { $0.urlString } #expect(urls.contains("http://example.com/first")) #expect(urls.contains("http://example.com/second")) } @Test func anchorWithoutHrefIgnored() { // `` without href is an anchor target, not a link. Must not appear. let html = """ real no href at all """ let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) let links = HTMLLinkParser.htmlLinks(with: d) let urls = links.compactMap { $0.urlString } #expect(urls == ["http://example.com/"]) } @Test func entitiesInHrefDecoded() { // href attribute values routinely contain `&` as a query-parameter // separator. After extraction the caller expects the decoded `&` (so // the URL they get is the real URL, not the HTML-escaped spelling). // A regression that stopped decoding entities in attribute values // would silently break every URL with a `?a=1&b=2`-style query. let html = """ two-param three-param """ let d = ParserData(url: "http://example.com/", data: Data(html.utf8)) let links = HTMLLinkParser.htmlLinks(with: d) let urls = links.compactMap { $0.urlString } #expect(urls.contains("http://example.com/search?q=foo&page=2")) #expect(urls.contains("http://example.com/a?x=1&y=2&z=3")) // No `&` should leak through. for u in urls { #expect(!u.contains("&"), "entity not decoded in href: \(u)") } } }