Files

49 lines
2.2 KiB
Swift
Raw Permalink Normal View History

import Foundation
import XCTest
@testable import QueueCubeShareCore
final class SharedVideoTests: XCTestCase {
func testAcceptsHTTPAndHTTPSURLsWithoutChangingTheirContents() throws {
for value in ["https://video.example/watch?v=one&t=12", "http://video.example/a%20b"] {
let url = try XCTUnwrap(URL(string: value))
XCTAssertEqual(try SharedVideo.validate(url), url)
}
}
func testExtractsLinkFromSharedTitleAndText() throws {
let url = try SharedVideo.extract(from: "A video to watch: https://video.example/watch?v=one\nSent from my phone")
XCTAssertEqual(url.absoluteString, "https://video.example/watch?v=one")
}
func testRepeatedURLInTextAndAttachmentsIsOneLink() throws {
let url = try XCTUnwrap(URL(string: "https://video.example/watch?v=one"))
XCTAssertEqual(try SharedVideo.extract(from: "\(url)\nWatch it: \(url)"), url)
XCTAssertEqual(try SharedVideo.single([url, url]), url)
}
func testRejectsMultipleDistinctLinks() throws {
XCTAssertThrowsError(try SharedVideo.extract(from: "https://video.example/one https://video.example/two")) {
guard case SharedVideo.Error.multipleLinks = $0 else { return XCTFail("Unexpected error: \($0)") }
}
XCTAssertThrowsError(try SharedVideo.single([
XCTUnwrap(URL(string: "https://video.example/one")),
XCTUnwrap(URL(string: "https://video.example/two"))
]))
}
func testRejectsFilesOtherSchemesCredentialsAndMissingHosts() throws {
for value in [
"file:///tmp/video.mp4", "ftp://video.example/video.mp4", "javascript:alert(1)",
"https://user:password@video.example/video", "https://user@video.example/video",
"https:///video", "/relative/video"
] {
let url = try XCTUnwrap(URL(string: value))
XCTAssertThrowsError(try SharedVideo.validate(url), "Accepted \(value)") {
guard case SharedVideo.Error.noLink = $0 else { return XCTFail("Unexpected error: \($0)") }
}
}
XCTAssertThrowsError(try SharedVideo.extract(from: "This title contains no video link"))
XCTAssertThrowsError(try SharedVideo.single([]))
}
}