URL path removes trailing slash

Originator:jbrayton
Number:rdar://28357201 Date Originated:2015-09-18
Status:Open Resolved:
Product:iOS SDK Product Version:Xcode 8.0 (8A218a), iOS 10.0.1 (14A403)
Classification:Other Bug Reproducible:Always
 
Description: 

If I instantiate a URL with the string "https://www.goldenhillsoftware.com/feed-hawk/", the path returned from the URL excludes the trailing slash ("/feed-hawk" instead of "/feed-hawk/".


Steps to Reproduce:

1.  Run this Swift 3 unit test:

    func testUrlPath() {
        guard let url = URL(string: "https://www.goldenhillsoftware.com/feed-hawk/") else {
            XCTFail()
            return
        }
        guard let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
            XCTFail()
            return
        }
        XCTAssertEqual(urlComponents.path, "/feed-hawk/")
        XCTAssertEqual(url.path, "/feed-hawk/")
        XCTAssertEqual(url.path, urlComponents.path)
    }


Expected Results:

I expect the unit test to pass. I expect url.path to be "/feed-hawk/". I also expect url.path to be consistent with urlComponents.path.


Actual Results:

The last two calls to XCTAssertEqual(...) fail because url.path is "/feed-hawk" instead of "/feed-hawk/". Interestingly, urlComponents.path is "/feed-hawk/" as I would expect.


Configuration:  

iOS and macOS, on device and in simulator.


Version & Build:  

Xcode 8.0 (8A218a), iOS 10.0.1 (14A403)


Additional Notes:

I initially encountered this in Xcode 8 for iOS 10. I see this problem on macOS and iOS. I see it on my iPhone 5S and in the simulator. I see the same behavior with an analogous Objective-C unit test. I also see the same behavior with an analogous Swift 2.2 unit test running under Xcode 7.3.1 in the iOS 9.3 simulator.

Comments

Still occurs on macOS Catalina with Xcode 11.2.1

This is still an issue, even 4 years later. I had to add NSURLComponents as an optional element to my app, to resolve URL paths, because there are web servers that actually care about the trailing slash, including on audio streams, and without it, will try to redirect to an index web page.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!