Revision control
Copy as Markdown
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
import XCTest
@testable import SiteImageView
class FaviconURLFetcherTests: XCTestCase {
var subject: DefaultFaviconURLFetcher!
var networkMock: HTMLDataRequestMock!
override func setUp() {
super.setUp()
networkMock = HTMLDataRequestMock()
subject = DefaultFaviconURLFetcher(network: networkMock)
}
override func tearDown() {
super.tearDown()
networkMock = nil
subject = nil
}
func testGetFaviconWithExistingIcon() async {
networkMock.data = generateHTMLData(string: ImageURLTestHTML.mockHTMLWithIcon)
do {
let result = try await subject.fetchFaviconURL(siteURL: url)
} catch {
XCTFail("Failed to fetch favicon with existing icon")
}
}
func testGetFaviconWithNoIconFallback() async {
networkMock.data = generateHTMLData(string: ImageURLTestHTML.mockHTMLWithNoIcon)
do {
let result = try await subject.fetchFaviconURL(siteURL: url)
} catch {
XCTFail("Failed to fetch favicon with no icon using fallback ico")
}
}
// MARK: - Private helpers
private func generateHTMLData(string: String) -> Data? {
return string.data(using: .utf8)
}
}
// MARK: - Mock HTML Data
private enum ImageURLTestHTML {
static let mockHTMLWithIcon = """
<html>
<head>
<title>Firefox</title>
</head>
</html>
"""
static let mockHTMLWithNoIcon = """
<html>
<head>
<title>Firefox</title>
</head>
</html>
"""
}