|
| 1 | +// |
| 2 | +// testURL+.swift |
| 3 | +// swiftui-loop-videoplayer |
| 4 | +// |
| 5 | +// Created by Igor Shelopaev on 20.08.25. |
| 6 | +// |
| 7 | + |
| 8 | +import XCTest |
| 9 | +@testable import swiftui_loop_videoplayer |
| 10 | + |
| 11 | +final class testURL: XCTestCase { |
| 12 | + |
| 13 | + // MARK: - Positive cases (should pass) |
| 14 | + |
| 15 | + func testSampleVideoURLsPass() { |
| 16 | + // Given: four sample URLs from the sandbox dictionary |
| 17 | + let urls = [ |
| 18 | + "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8", |
| 19 | + "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", |
| 20 | + "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4", |
| 21 | + "https://devstreaming-cdn.apple.com/videos/streaming/examples/adv_dv_atmos/main.m3u8" |
| 22 | + ] |
| 23 | + |
| 24 | + // When/Then |
| 25 | + for raw in urls { |
| 26 | + let url = URL.validURLFromString(from: raw) |
| 27 | + XCTAssertNotNil(url, "Expected to parse: \(raw)") |
| 28 | + XCTAssertEqual(url?.scheme?.lowercased(), "https") |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + func testAddsHTTPSIfMissing() { |
| 33 | + // Given |
| 34 | + let raw = "example.com/path?x=1#y" |
| 35 | + |
| 36 | + // When |
| 37 | + let url = URL.validURLFromString(from: raw) |
| 38 | + |
| 39 | + // Then |
| 40 | + XCTAssertNotNil(url) |
| 41 | + XCTAssertEqual(url?.scheme, "https") |
| 42 | + XCTAssertEqual(url?.host, "example.com") |
| 43 | + XCTAssertEqual(url?.path, "/path") |
| 44 | + } |
| 45 | + |
| 46 | + func testTrimsWhitespace() { |
| 47 | + let raw = " https://example.com/video.m3u8 " |
| 48 | + let url = URL.validURLFromString(from: raw) |
| 49 | + XCTAssertNotNil(url) |
| 50 | + XCTAssertEqual(url?.host, "example.com") |
| 51 | + XCTAssertEqual(url?.path, "/video.m3u8") |
| 52 | + } |
| 53 | + |
| 54 | + func testIPv6AndLocalHosts() { |
| 55 | + // IPv6 loopback |
| 56 | + XCTAssertNotNil(URL.validURLFromString(from: "https://[::1]")) |
| 57 | + // localhost |
| 58 | + XCTAssertNotNil(URL.validURLFromString(from: "http://localhost")) |
| 59 | + // IPv4 with port and query/fragment |
| 60 | + XCTAssertNotNil(URL.validURLFromString(from: "http://127.0.0.1:8080/path?a=1#x")) |
| 61 | + } |
| 62 | + |
| 63 | + func testIDNUnicodeHost() { |
| 64 | + // Unicode host (IDN). URLComponents should handle this. |
| 65 | + let url = URL.validURLFromString(from: "https://bücher.de") |
| 66 | + XCTAssertNotNil(url) |
| 67 | + XCTAssertEqual(url?.scheme, "https") |
| 68 | + XCTAssertNotNil(url?.host) |
| 69 | + } |
| 70 | + |
| 71 | + // MARK: - Negative cases (should fail) |
| 72 | + |
| 73 | + func testRejectsNonHTTP() { |
| 74 | + XCTAssertNil(URL.validURLFromString(from: "ftp://example.com/file.mp4")) |
| 75 | + XCTAssertNil(URL.validURLFromString(from: "mailto:user@example.com")) |
| 76 | + XCTAssertNil(URL.validURLFromString(from: "file:///Users/me/movie.mp4")) |
| 77 | + } |
| 78 | + |
| 79 | + func testRejectsInvalidPort() { |
| 80 | + XCTAssertNil(URL.validURLFromString(from: "https://example.com:0")) |
| 81 | + XCTAssertNil(URL.validURLFromString(from: "https://example.com:65536")) |
| 82 | + XCTAssertNotNil(URL.validURLFromString(from: "https://example.com:65535")) |
| 83 | + } |
| 84 | + |
| 85 | + func testRejectsMissingHost() { |
| 86 | + XCTAssertNil(URL.validURLFromString(from: "https://")) |
| 87 | + XCTAssertNil(URL.validURLFromString(from: "https:///path-only")) |
| 88 | + } |
| 89 | + |
| 90 | + func testNoAutoSchemeOption() { |
| 91 | + // When auto-scheme is disabled, a bare host should fail. |
| 92 | + XCTAssertNil(URL.validURLFromString(from: "example.com", assumeHTTPSIfMissing: false)) |
| 93 | + } |
| 94 | +} |
0 commit comments