From 829bc38981cb710bad39e29367146055f8aaab03 Mon Sep 17 00:00:00 2001
From: Alessandro Boron <aboron@duckduckgo.com>
Date: Tue, 10 Dec 2024 10:28:59 +0100
Subject: [PATCH] Clean up

---
 .../MaliciousSiteProtectionManager.swift                  | 3 ++-
 .../SpecialErrorPageNavigationHandler.swift               | 8 +++++---
 .../MaliciousSiteProtectionNavigationHandlerTests.swift   | 8 +++++++-
 3 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/DuckDuckGo/MaliciousSiteProtection/MaliciousSiteProtectionManager.swift b/DuckDuckGo/MaliciousSiteProtection/MaliciousSiteProtectionManager.swift
index 321c243f54..46dab041d0 100644
--- a/DuckDuckGo/MaliciousSiteProtection/MaliciousSiteProtectionManager.swift
+++ b/DuckDuckGo/MaliciousSiteProtection/MaliciousSiteProtectionManager.swift
@@ -20,7 +20,7 @@
 import Foundation
 import MaliciousSiteProtection
 
-class MaliciousSiteProtectionManager: MaliciousSiteDetecting {
+final class MaliciousSiteProtectionManager: MaliciousSiteDetecting {
 
     func evaluate(_ url: URL) async -> ThreatKind? {
         try? await Task.sleep(interval: 0.3)
@@ -34,4 +34,5 @@ class MaliciousSiteProtectionManager: MaliciousSiteDetecting {
             return .none
         }
     }
+
 }
diff --git a/DuckDuckGo/SpecialErrorPage/SpecialErrorPageNavigationHandler.swift b/DuckDuckGo/SpecialErrorPage/SpecialErrorPageNavigationHandler.swift
index 4b6ff7185b..c5d596ec77 100644
--- a/DuckDuckGo/SpecialErrorPage/SpecialErrorPageNavigationHandler.swift
+++ b/DuckDuckGo/SpecialErrorPage/SpecialErrorPageNavigationHandler.swift
@@ -75,6 +75,8 @@ extension SpecialErrorPageNavigationHandler: WebViewNavigationHandling {
 
         switch result {
         case let .navigationHandled(.mainFrame(response)):
+            // Re-use the same request to avoid that the new sideload request is intercepted and cancelled
+            // due to parameters added to the header.
             var request = response.navigationAction.request
             request.url = response.errorData.url
             isSpecialErrorPageRequest = true
@@ -132,11 +134,11 @@ extension SpecialErrorPageNavigationHandler: SpecialErrorPageUserScriptDelegate
             if webView?.canGoBack == true {
                 _ = webView?.goBack()
             } else {
-                forceCloseTab()
+                closeTab()
             }
         }
 
-        func forceCloseTab() {
+        func closeTab() {
             delegate?.closeSpecialErrorPageTab()
         }
 
@@ -148,7 +150,7 @@ extension SpecialErrorPageNavigationHandler: SpecialErrorPageUserScriptDelegate
             navigateBackIfPossible()
         case .maliciousSite:
             maliciousSiteProtectionNavigationHandler.leaveSite()
-            forceCloseTab()
+            closeTab()
         }
     }
 
diff --git a/DuckDuckGoTests/SpecialErrorPage/MaliciousSiteProtectionNavigationHandlerTests.swift b/DuckDuckGoTests/SpecialErrorPage/MaliciousSiteProtectionNavigationHandlerTests.swift
index b55b855f43..96b5a06854 100644
--- a/DuckDuckGoTests/SpecialErrorPage/MaliciousSiteProtectionNavigationHandlerTests.swift
+++ b/DuckDuckGoTests/SpecialErrorPage/MaliciousSiteProtectionNavigationHandlerTests.swift
@@ -73,7 +73,13 @@ struct MaliciousSiteProtectionNavigationHandlerTests {
     }
 
     @MainActor
-    @Test("Bypassed Malicious Site does not create a Malicious Detection Task", arguments: [ThreatKind.phishing, .malware])
+    @Test(
+        "Bypassed Malicious Site does not create a Malicious Detection Task",
+        arguments: [
+            ThreatKind.phishing,
+            .malware
+        ]
+    )
     func whenBypassedMaliciousSiteThreatKindIsSetThenReturnNavigationNotHandled(threat: ThreatKind) throws {
         // GIVEN
         let url = try #require(URL(string: "https://www.example.com"))