Signal-iOS/SignalServiceKit/tests/Network/HTMLMetadataTests.swift
Michelle Linington e63b16a7e0 IOS-822: Support for parsing additional HTML content
This branch makes improvements to how HTML metadata is parsed. Now,
we'll parse other kinds of opengraph tags as well as title, favicon, and
meta description.

This branch also modifies how URL validation is performed. Before,
international URLs were not supported due to NSURL/NSDataDetectors
handling of punycode. Now, there are mechanisms to detect non-ASCII URLs
that have been parsed and validate that they are comprised of the
correct characters.
2020-09-02 14:50:12 -07:00

151 lines
6.1 KiB
Swift

//
// Copyright (c) 2020 Open Whisper Systems. All rights reserved.
//
import Foundation
import XCTest
@testable import SignalServiceKit
class HTMLMetadataTests: SSKBaseTestSwift {
func testEmptyBody() {
let empty = HTMLMetadata.construct(parsing: "")
XCTAssertEqual(empty, HTMLMetadata())
}
func testParseTitleTags() {
let testSet: [String: String] = [
"<title>Simple</title>": "Simple",
"\nhello\n\t<title>Two Lines</title>\n\nblahhhh": "Two Lines",
"<title>Title1</title><title>Title2</title>": "Title1",
" \n\t < title \n>Oddly spaced< /title >": "Oddly spaced",
"<title>&quotEntities&quot</title>": "\"Entities\""
]
testSet.forEach { test, expectedResult in
XCTAssertEqual(
HTMLMetadata.construct(parsing: test),
HTMLMetadata(titleTag: expectedResult)
)
}
}
func testParseFaviconUrlString() {
let testSet: [String: String?] = [
"<link rel=\"icon\" href=\"test.ico\"></link>": "test.ico",
"< link rel=\" shortcut icon \" href=\"spacedddd\" />": "spacedddd",
"<link rel=\"apple-touch-icon\" href=\"incorrect tag\">": nil,
"""
<link rel=\"icon\" href=\"first\">
<link rel=\"icon\" href=\"second\">
<link rel=\"icon\" href=\"third\">
""": "first",
"<link href=\"href first\" rel=\"icon\">": "href first"
]
testSet.forEach { test, expectedResult in
XCTAssertEqual(
HTMLMetadata.construct(parsing: test),
HTMLMetadata(faviconUrlString: expectedResult)
)
}
}
func testParseMetaDescription() {
let testSet: [String: String?] = [
"<meta name=\"description\" content=\"DescriptionText\" />": "DescriptionText",
"< meta name = \"description\" \n\n\n content=\"Spaced Description\" />": "Spaced Description",
"<meta name=\"description\" content=\"DescriptionText\" /><meta name=\"description\" content=\"Repeat\" />": "DescriptionText",
"<meta property=\"description\" content=\"DescriptionText\" />": nil
]
testSet.forEach { test, expectedResult in
XCTAssertEqual(
HTMLMetadata.construct(parsing: test),
HTMLMetadata(description: expectedResult)
)
}
}
func testParseOpengraphTitle() {
let testSet: [String: String?] = [
"<meta property=\"og:title\" content=\"TestTitle\">": "TestTitle",
"<meta content=\"FlippedTitle\" property=\"og:title\">": "FlippedTitle",
"<meta garbage1\t=\ngarbage property=\"og:title\" garbage2 = garbage content=\"TitleWithGarbage\" garbage3=garbage kafdjadk>": "TitleWithGarbage",
"<meta property=\"og:title\" content=\"Title\"><meta property=\"og:title\" content=\"Repeat\">": "Title"
]
testSet.forEach { test, expectedResult in
XCTAssertEqual(
HTMLMetadata.construct(parsing: test),
HTMLMetadata(ogTitle: expectedResult)
)
}
}
func testParseOneOfEach_Simple() {
let testHTML = """
<title>TitleString</title>
<link rel="icon" href="FaviconString" />
<meta name="description" content="DescriptionString" />
<meta property="og:title" content="OpengraphTitle" />
<meta property="og:description" content="OpengraphDescription" />
<meta property="og:image" content="ImageURL" />
<meta property="og:image:url" content="FallbackImageURL" />
<meta property="og:published_time" content="PublishedDate" />
<meta property="og:article:published_time" content="FallbackPublishedDate" />
<meta property="og:modified_time" content="ModifiedDate" />
<meta property="og:article:modified_time" content="FallbackModifiedDate" />
"""
let testMetadata = HTMLMetadata.construct(parsing: testHTML)
XCTAssertEqual(testMetadata, HTMLMetadata(
titleTag: "TitleString",
faviconUrlString: "FaviconString",
description: "DescriptionString",
ogTitle: "OpengraphTitle",
ogDescription: "OpengraphDescription",
ogImageUrlString: "ImageURL",
ogPublishDateString: "PublishedDate",
ogModifiedDateString: "ModifiedDate"
))
}
func testParseOneOfEach_Fallback() {
let testHTML = """
<title>TitleString</title>
<link rel="icon" href="FaviconString" />
<meta name="description" content="DescriptionString" />
<meta property="og:title" content="OpengraphTitle" />
<meta property="og:description" content="OpengraphDescription" />
<meta property="og:image:url" content="FallbackImageURL" />
<meta property="og:article:published_time" content="FallbackPublishedDate" />
<meta property="og:article:modified_time" content="FallbackModifiedDate" />
"""
let testMetadata = HTMLMetadata.construct(parsing: testHTML)
XCTAssertEqual(testMetadata, HTMLMetadata(
titleTag: "TitleString",
faviconUrlString: "FaviconString",
description: "DescriptionString",
ogTitle: "OpengraphTitle",
ogDescription: "OpengraphDescription",
ogImageUrlString: "FallbackImageURL",
ogPublishDateString: "FallbackPublishedDate",
ogModifiedDateString: "FallbackModifiedDate"
))
}
func testLinkDataParsing() {
let linkText = ("<meta property=\"og:title\" content=\"Randomness is Random - Numberphile\">" +
"<meta property=\"og:image\" content=\"https://i.ytimg.com/vi/tP-Ipsat90c/maxresdefault.jpg\">")
let content = HTMLMetadata.construct(parsing: linkText)
XCTAssertNotNil(content)
XCTAssertEqual(content.ogTitle, "Randomness is Random - Numberphile")
XCTAssertEqual(content.ogImageUrlString, "https://i.ytimg.com/vi/tP-Ipsat90c/maxresdefault.jpg")
}
}