Repository Public License
1import Foundation2import NavigatorCore3import TestSupport4import Testing5import WebKit6@testable import WebRuntime78extension WebRuntimeTests {9 /// The three additions the shell and the download engine asked for: live navigation settings, leaving an10 /// interstitial without loading anything, and starting a download under a known identifier.11 @Suite struct ShellGaps {12 @Test func navigationSettingsApplyToALiveEngine() async throws {13 try await withHarness { harness in14 let engine = harness.makeEngine()15 try await harness.load(engine, "/", expectingTitle: "Fixture Home")16 #expect(!engine.settings.navigation.httpsOnly)1718 // HTTPS-only is switched on while the tab is open: the next navigation is upgraded.19 engine.updateNavigationSettings { $0.httpsOnly = true; $0.httpsOnlyExemptsLoopback = false }20 #expect(engine.settings.navigation.httpsOnly)21 engine.load(harness.server.url("/page/plain"))22 try await waitUntil("upgrade failure") { engine.httpsUpgradeFailure != nil }23 #expect(harness.server.requests(path: "/page/plain").isEmpty)2425 // Leaving the interstitial brings the committed page back without a request.26 let homeRequests = harness.server.requests(path: "/").count27 #expect(engine.dismissInterstitial())28 #expect(engine.httpsUpgradeFailure == nil && engine.loadError == nil)29 #expect(engine.url == harness.server.url("/") && engine.title == "Fixture Home")30 await settle()31 #expect(harness.server.requests(path: "/").count == homeRequests, "nothing was reloaded")32 #expect(harness.server.requests(path: "/page/plain").isEmpty)33 #expect(await engine.string("return document.title;") == "Fixture Home")34 #expect(!engine.isLoading)3536 // Switched off again: the same address loads over http.37 var navigation = engine.settings.navigation38 navigation.httpsOnly = false39 engine.updateNavigationSettings(navigation)40 try await harness.load(engine, "/page/plain", expectingTitle: "Fixture plain")41 #expect(engine.committedURL == harness.server.url("/page/plain"))42 }43 }4445 @Test func dismissingACertificateErrorReturnsToTheCommittedPage() async throws {46 try await withHarness { harness in47 let identity = try FixtureTLSIdentity.makeSelfSigned()48 let tls = FixtureServer(tls: identity.tlsOptions())49 try await tls.start()50 defer { tls.stop() }5152 let engine = harness.makeEngine()53 try await harness.load(engine, "/", expectingTitle: "Fixture Home")54 let commits = engine.commitCount55 #expect(commits >= 1 && engine.committedURL == harness.server.url("/"))56 let homeRequests = harness.server.requests(path: "/").count5758 engine.load(tls.url("/page/secret"))59 try await waitUntil("certificate error") { engine.certificateError != nil }60 #expect(engine.url == tls.url("/page/secret"))61 #expect(engine.committedURL == harness.server.url("/"), "a refused page never commits")6263 #expect(engine.dismissInterstitial())64 #expect(engine.certificateError == nil)65 #expect(engine.url == harness.server.url("/") && engine.title == "Fixture Home")66 await settle()67 #expect(engine.commitCount == commits && !engine.isLoading)68 #expect(harness.server.requests(path: "/").count == homeRequests, "nothing was reloaded")69 #expect(tls.requests.isEmpty)70 #expect(engine.profile.certificateExceptions.isEmpty)71 #expect(!engine.dismissInterstitial(), "nothing left to dismiss")7273 // The page is alive: it still navigates.74 try await engine.evaluate("document.getElementById('link-a').click();")75 try await waitUntil("link followed") { engine.title == "Fixture a" && !engine.isLoading }76 #expect(engine.commitCount == commits + 1)77 }78 }7980 @Test func dismissingOnATabWithoutAPageGoesBackToTheEmptyState() async throws {81 try await withHarness { harness in82 let identity = try FixtureTLSIdentity.makeSelfSigned()83 let tls = FixtureServer(tls: identity.tlsOptions())84 try await tls.start()85 defer { tls.stop() }8687 // Certificate error as the very first navigation of the tab.88 let engine = harness.makeEngine()89 engine.load(tls.url("/"))90 try await waitUntil("certificate error") { engine.certificateError != nil }91 #expect(engine.dismissInterstitial())92 #expect(engine.url == nil && engine.title.isEmpty && engine.certificateError == nil)93 #expect(engine.committedURL == nil && engine.commitCount == 0)94 await settle()95 #expect(tls.requests.isEmpty && !engine.isLoading)9697 // Network error (nothing listens on that port).98 let unreachable = try #require(URL(string: "http://127.0.0.1:65501/"))99 engine.load(unreachable)100 try await waitUntil("load error") { engine.loadError != nil }101 #expect(engine.dismissInterstitial())102 #expect(engine.url == nil && engine.loadError == nil)103104 // A refused address typed in an empty tab.105 #expect(engine.load(input: "javascript:alert(1)") == .navigationBlocked(.scriptScheme))106 #expect(engine.blockedNavigations.filter(\.isMainFrame).count == 1)107 #expect(engine.dismissInterstitial())108 #expect(engine.blockedNavigations.filter(\.isMainFrame).isEmpty)109 #expect(!engine.dismissInterstitial())110111 // The engine is still usable afterwards.112 try await harness.load(engine, "/", expectingTitle: "Fixture Home")113 }114 }115116 @Test func downloadStartsUnderTheRequestedIdentifier() async throws {117 try await withHarness { harness in118 let engine = harness.makeEngine()119 try await harness.load(engine, "/", expectingTitle: "Fixture Home")120 let id = DownloadID()121 #expect(await engine.startDownload(harness.server.url("/download/attachment"), id: id))122 try await waitUntilAsync("candidate") { await harness.downloads.candidates.count == 1 }123 let candidate = try #require(await harness.downloads.candidates.first)124 #expect(candidate.id == id)125 #expect(candidate.sourceURL == harness.server.url("/download/attachment"))126 #expect(candidate.originURL == harness.server.url("/"))127 try await waitUntilAsync("finished") { await harness.downloads.terminalEvent(id) != nil }128 let destination = try #require(await harness.downloads.destinations[id])129 #expect(await harness.downloads.terminalEvent(id) == .finished(fileURL: destination))130131 // The same identifier again (a restart) and an explicit origin; then a dormant tab, which gets132 // its web view back for the transfer.133 let origin = harness.server.url("/download/page")134 #expect(await engine.startDownload(harness.server.url("/download/attachment"), id: id, originURL: origin))135 try await waitUntilAsync("second candidate") { await harness.downloads.candidates.count == 2 }136 #expect(await harness.downloads.candidates.last?.id == id)137 #expect(await harness.downloads.candidates.last?.originURL == origin)138139 let profile = harness.makeProfile()140 let space = OrbitSpace(profileID: profile.id, name: "Test")141 let dormant = WebEngine(profile: harness.context(profile),142 restoring: Tab(windowID: WindowID(), spaceID: space.id))143 harness.adopt(dormant)144 #expect(dormant.webView == nil)145 let other = DownloadID()146 #expect(await dormant.startDownload(harness.server.url("/download/unknown"), id: other))147 try await waitUntilAsync("dormant candidate") { await harness.downloads.candidates.count == 3 }148 #expect(await harness.downloads.candidates.last?.id == other)149 try await waitUntilAsync("dormant finished") { await harness.downloads.terminalEvent(other) != nil }150151 // Addresses that cannot be downloaded are refused.152 let local = try #require(URL(string: "file:///etc/hosts"))153 let started = await engine.startDownload(local, id: DownloadID())154 #expect(!started)155 #expect(await harness.downloads.candidates.count == 3)156 }157 }158 }159}160