@@ -727,6 +727,66 @@ private struct URLTests {
727
727
#expect( schemeRelative. relativePath == " " )
728
728
}
729
729
730
+ @Test func deletingLastPathComponentWithBase( ) throws {
731
+ let basePath = " /Users/foo-bar/Test1 Test2? Test3/Test4 "
732
+ let baseURL = URL ( filePath: basePath, directoryHint: . isDirectory)
733
+ let fileURL = URL ( filePath: " ../Test5.txt " , directoryHint: . notDirectory, relativeTo: baseURL)
734
+ #expect( fileURL. path == " /Users/foo-bar/Test1 Test2? Test3/Test5.txt " )
735
+ #expect( fileURL. deletingLastPathComponent ( ) . path == " /Users/foo-bar/Test1 Test2? Test3 " )
736
+ #expect( baseURL. deletingLastPathComponent ( ) . path == " /Users/foo-bar/Test1 Test2? Test3 " )
737
+ }
738
+
739
+ @Test func encodedAbsoluteString( ) throws {
740
+ let base = URL ( string: " http://user name:pass word@😂😂😂.com/pa th/p?qu ery#frag ment " )
741
+ #expect
( base
? . absoluteString
== " http://user%20name:pass%[email protected] /pa%20th/p?qu%20ery#frag%20ment " )
742
+ var url = URL ( string: " relative " , relativeTo: base)
743
+ #expect
( url
? . absoluteString
== " http://user%20name:pass%[email protected] /pa%20th/relative " )
744
+ url = URL ( string: " rela tive " , relativeTo: base)
745
+ #expect
( url
? . absoluteString
== " http://user%20name:pass%[email protected] /pa%20th/rela%20tive " )
746
+ url = URL ( string: " relative?qu " , relativeTo: base)
747
+ #expect
( url
? . absoluteString
== " http://user%20name:pass%[email protected] /pa%20th/relative?qu " )
748
+ url = URL ( string: " rela tive?q u " , relativeTo: base)
749
+ #expect
( url
? . absoluteString
== " http://user%20name:pass%[email protected] /pa%20th/rela%20tive?q%20u " )
750
+
751
+ let fileBase = URL ( filePath: " /Users/foo bar/more spaces/ " )
752
+ #expect( fileBase. absoluteString == " file:///Users/foo%20bar/more%20spaces/ " )
753
+
754
+ url = URL ( string: " relative " , relativeTo: fileBase)
755
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/relative " )
756
+ #expect( url? . path == " /Users/foo bar/more spaces/relative " )
757
+
758
+ url = URL ( string: " rela tive " , relativeTo: fileBase)
759
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/rela%20tive " )
760
+ #expect( url? . path == " /Users/foo bar/more spaces/rela tive " )
761
+
762
+ // URL(string:) should count ? as the query delimiter
763
+ url = URL ( string: " relative?query " , relativeTo: fileBase)
764
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/relative?query " )
765
+ #expect( url? . path == " /Users/foo bar/more spaces/relative " )
766
+
767
+ url = URL ( string: " rela tive?qu ery " , relativeTo: fileBase)
768
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/rela%20tive?qu%20ery " )
769
+ #expect( url? . path == " /Users/foo bar/more spaces/rela tive " )
770
+
771
+ // URL(filePath:) should encode ? as part of the path
772
+ url = URL ( filePath: " relative?query " , relativeTo: fileBase)
773
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/relative%3Fquery " )
774
+ #expect( url? . path == " /Users/foo bar/more spaces/relative?query " )
775
+
776
+ url = URL ( filePath: " rela tive?qu ery " , relativeTo: fileBase)
777
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/rela%20tive%3Fqu%20ery " )
778
+ #expect( url? . path == " /Users/foo bar/more spaces/rela tive?qu ery " )
779
+
780
+ // URL(filePath:) should encode %3F as part of the path
781
+ url = URL ( filePath: " relative%3Fquery " , relativeTo: fileBase)
782
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/relative%253Fquery " )
783
+ #expect( url? . path == " /Users/foo bar/more spaces/relative%3Fquery " )
784
+
785
+ url = URL ( filePath: " rela tive%3Fqu ery " , relativeTo: fileBase)
786
+ #expect( url? . absoluteString == " file:///Users/foo%20bar/more%20spaces/rela%20tive%253Fqu%20ery " )
787
+ #expect( url? . path == " /Users/foo bar/more spaces/rela tive%3Fqu ery " )
788
+ }
789
+
730
790
@Test func filePathDropsTrailingSlashes( ) throws {
731
791
var url = URL ( filePath: " /path/slashes/// " )
732
792
#expect( url. path ( ) == " /path/slashes/// " )
0 commit comments