Signal-iOS/SignalServiceKit/tests/Network/HTMLMetadataTests.swift
Evan Hahn 370ff654e7
Change license to AGPL
Change license to AGPL

This commit:

- Updates the `LICENSE` file

- Start every file with something like:

      // Copyright YEAR_FIRST_PUBLISHED Signal Messenger, LLC
      // SPDX-License-Identifier: AGPL-3.0-only

---

First, I removed existing license headers with this Ruby 3.1.2 script:

    require 'set'

    EXTENSIONS_TO_CHECK = Set['.h', '.hpp', '.cpp', '.m', '.mm', '.pch', '.swift']

    same = 0
    different = 0

    all_files = `git ls-files`.lines.map { |line| line.strip }
    all_files.each do |relative_path|
      if relative_path == 'Pods'
        next
      end

      unless EXTENSIONS_TO_CHECK.include? File.extname(relative_path)
        next
      end

      path = File.expand_path(relative_path)

      contents = File.read(path)
      new_contents = contents.sub(/\/\/\n\/\/  Copyright .*\n\/\/\n\n/, '')

      if contents == new_contents
        same += 1
      else
        different += 1
      end

      File.write(path, new_contents)
    end

    puts "updated #{different} file(s), left #{same} untouched"

I'm sure this script could be improved, but it worked well enough.

Then, I created `Scripts/lint/lint-license-headers` and ran it to auto-
fix a lot of files. This changed the mode of some files, but I think
that's actually desirable. For example,
`SignalServiceKit/src/Util/AppContext.m` previously had a mode of
`0755/-rwxr-xr-x`, and it's now `0644/-rw-r--r--`.

Then I fixed some stragglers and updated the precommit script.

See [a similar change in the Desktop app][0].

[0]: 8bfaf598af
2022-10-13 08:25:37 -05:00

146 lines
5.7 KiB
Swift

//
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
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="article:published_time" content="ArticlePublishedDate" />
<meta property="og:modified_time" content="ModifiedDate" />
<meta property="article:modified_time" content="ArticleModifiedDate" />
"""
let testMetadata = HTMLMetadata.construct(parsing: testHTML)
XCTAssertEqual(testMetadata, HTMLMetadata(
titleTag: "TitleString",
faviconUrlString: "FaviconString",
description: "DescriptionString",
ogTitle: "OpengraphTitle",
ogDescription: "OpengraphDescription",
ogImageUrlString: "ImageURL",
ogPublishDateString: "PublishedDate",
articlePublishDateString: "ArticlePublishedDate",
ogModifiedDateString: "ModifiedDate",
articleModifiedDateString: "ArticleModifiedDate"
))
}
func testParseFallbackImage() {
let testHTML = """
<title>TitleString</title>
<link rel="icon" href="FaviconString" />
<meta name="description" content="DescriptionString" />
<meta property="og:image:url" content="FallbackImageURL" />
"""
let testMetadata = HTMLMetadata.construct(parsing: testHTML)
XCTAssertEqual(testMetadata, HTMLMetadata(
titleTag: "TitleString",
faviconUrlString: "FaviconString",
description: "DescriptionString",
ogImageUrlString: "FallbackImageURL"
))
}
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")
}
}