Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/FoundationEssentials/URL/URL_Swift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,9 @@ internal final class _SwiftURL: Sendable, Hashable, Equatable {
}

internal func appending<S: StringProtocol>(path: S, directoryHint: URL.DirectoryHint, encodingSlashes: Bool, compatibility: Bool = false) -> URL? {
guard !path.isEmpty || !_parseInfo.path.isEmpty || _parseInfo.netLocationRange?.isEmpty == false else {
return nil
}
#if os(Windows)
var pathToAppend = path.replacing(._backslash, with: ._slash)
#else
Expand Down
15 changes: 14 additions & 1 deletion Tests/FoundationEssentialsTests/URLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,21 @@ final class URLTests : XCTestCase {
XCTAssertEqual(example.path(), "/foo")
XCTAssertEqual(example.absoluteString, "https://example.com/foo")

// Maintain old behavior, where appending an empty path
// to an empty host does not add a slash, but appending
// an empty path to a non-empty host does
example = try XCTUnwrap(URL(string: "https://example.com"))
example.append(path: "")
XCTAssertEqual(example.host(), "example.com")
XCTAssertEqual(example.path(), "/")
XCTAssertEqual(example.absoluteString, "https://example.com/")

var emptyHost = try XCTUnwrap(URL(string: "scheme://"))
XCTAssertTrue(emptyHost.host()?.isEmpty ?? true)
XCTAssertNil(emptyHost.host())
XCTAssertTrue(emptyHost.path().isEmpty)

emptyHost.append(path: "")
XCTAssertNil(emptyHost.host())
XCTAssertTrue(emptyHost.path().isEmpty)

emptyHost.append(path: "foo")
Expand Down