From 64af84f71b92c345e48f49f8dc983ea4e03c5bf5 Mon Sep 17 00:00:00 2001 From: Steven Sherry Date: Mon, 9 May 2022 16:54:32 -0500 Subject: [PATCH 1/2] fix(ios): Creating a url from `URL(string:)` returns nil if a path contains spaces. `URL(fileURLWithPath:)` does not. chore(ios): Remove test checking for invalid path. File urls are less strict in the characters accepted and never returns nil. --- ios/Capacitor/Capacitor/Router.swift | 6 +++--- ios/Capacitor/CapacitorTests/RouterTests.swift | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/ios/Capacitor/Capacitor/Router.swift b/ios/Capacitor/Capacitor/Router.swift index 6ac417d31f..24d4f3a91b 100644 --- a/ios/Capacitor/Capacitor/Router.swift +++ b/ios/Capacitor/Capacitor/Router.swift @@ -15,10 +15,10 @@ public protocol Router { // swiftlint:disable:next type_name internal struct _Router: Router { func route(for path: String) -> String { - let pathUrl = URL(string: path) + let pathUrl = URL(fileURLWithPath: path) - // if the pathUrl is null, then it is an invalid url (meaning it is empty or just plain invalid) then we want to route to /index.html - if pathUrl?.pathExtension.isEmpty ?? true { + // If there's no path extension it also means the path is empty or a SPA route + if pathUrl.pathExtension.isEmpty { return "/index.html" } diff --git a/ios/Capacitor/CapacitorTests/RouterTests.swift b/ios/Capacitor/CapacitorTests/RouterTests.swift index f328c9c7ca..7f407bef32 100644 --- a/ios/Capacitor/CapacitorTests/RouterTests.swift +++ b/ios/Capacitor/CapacitorTests/RouterTests.swift @@ -12,10 +12,6 @@ import XCTest class RouterTests: XCTestCase { let router = _Router() - func testRouterReturnsIndexWhenProvidedInvalidPath() { - XCTAssertEqual(router.route(for: "/skull.💀"), "/index.html") - } - func testRouterReturnsIndexWhenProvidedEmptyPath() { XCTAssertEqual(router.route(for: ""), "/index.html") } From 8d94aa272e9a47bcf77855083c8e2f955af465f2 Mon Sep 17 00:00:00 2001 From: Steven Sherry Date: Mon, 9 May 2022 17:08:36 -0500 Subject: [PATCH 2/2] chore(ios): Add test case for file path with spaces --- ios/Capacitor/CapacitorTests/RouterTests.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ios/Capacitor/CapacitorTests/RouterTests.swift b/ios/Capacitor/CapacitorTests/RouterTests.swift index 7f407bef32..8a6e4d45db 100644 --- a/ios/Capacitor/CapacitorTests/RouterTests.swift +++ b/ios/Capacitor/CapacitorTests/RouterTests.swift @@ -23,4 +23,8 @@ class RouterTests: XCTestCase { func testRouterReturnsPathWhenProvidedValidPath() { XCTAssertEqual(router.route(for: "/a/valid/path.ext"), "/a/valid/path.ext") } + + func testRouterReturnsPathWhenProvidedValidPathWithExtensionAndSpaces() { + XCTAssertEqual(router.route(for: "/a/valid/file path.ext"), "/a/valid/file path.ext") + } }