Skip to content

Commit eb28f8f

Browse files
authored
(150389961) Don't add a slash when appending an empty string to an empty path and host (#1282)
1 parent f057ef4 commit eb28f8f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

Sources/FoundationEssentials/URL/URL_Swift.swift

+3
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,9 @@ internal final class _SwiftURL: Sendable, Hashable, Equatable {
682682
}
683683

684684
internal func appending<S: StringProtocol>(path: S, directoryHint: URL.DirectoryHint, encodingSlashes: Bool, compatibility: Bool = false) -> URL? {
685+
guard !path.isEmpty || !_parseInfo.path.isEmpty || _parseInfo.netLocationRange?.isEmpty == false else {
686+
return nil
687+
}
685688
#if os(Windows)
686689
var pathToAppend = path.replacing(._backslash, with: ._slash)
687690
#else

Tests/FoundationEssentialsTests/URLTests.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,21 @@ final class URLTests : XCTestCase {
966966
XCTAssertEqual(example.path(), "/foo")
967967
XCTAssertEqual(example.absoluteString, "https://example.com/foo")
968968

969+
// Maintain old behavior, where appending an empty path
970+
// to an empty host does not add a slash, but appending
971+
// an empty path to a non-empty host does
972+
example = try XCTUnwrap(URL(string: "https://example.com"))
973+
example.append(path: "")
974+
XCTAssertEqual(example.host(), "example.com")
975+
XCTAssertEqual(example.path(), "/")
976+
XCTAssertEqual(example.absoluteString, "https://example.com/")
977+
969978
var emptyHost = try XCTUnwrap(URL(string: "scheme://"))
970-
XCTAssertTrue(emptyHost.host()?.isEmpty ?? true)
979+
XCTAssertNil(emptyHost.host())
980+
XCTAssertTrue(emptyHost.path().isEmpty)
981+
982+
emptyHost.append(path: "")
983+
XCTAssertNil(emptyHost.host())
971984
XCTAssertTrue(emptyHost.path().isEmpty)
972985

973986
emptyHost.append(path: "foo")

0 commit comments

Comments
 (0)