Skip to content

Commit

Permalink
fix(ios): Use URL(fileURLWithPath:) instead of URL(string:) (#5603)
Browse files Browse the repository at this point in the history
* 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.
* chore(ios): Add test case for file path with spaces
  • Loading branch information
Steven0351 committed May 10, 2022
1 parent 009064d commit 5fac1b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions ios/Capacitor/Capacitor/Router.swift
Expand Up @@ -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"
}

Expand Down
8 changes: 4 additions & 4 deletions ios/Capacitor/CapacitorTests/RouterTests.swift
Expand Up @@ -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")
}
Expand All @@ -27,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")
}
}

0 comments on commit 5fac1b2

Please sign in to comment.