Skip to content

Commit 4e22a07

Browse files
mbvreddyparkera
authored andcommitted
implementation for NSURLComponents.URLRelativeToURL API (#346)
remove semicolon
1 parent 7427877 commit 4e22a07

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

Foundation/NSURL.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,10 @@ public class NSURLComponents : NSObject, NSCopying {
647647

648648
// Returns a URL created from the NSURLComponents relative to a base URL. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.
649649
public func URLRelativeToURL(_ baseURL: NSURL?) -> NSURL? {
650-
NSUnimplemented()
650+
if let componentString = string {
651+
return NSURL(string: componentString, relativeToURL: baseURL)
652+
}
653+
return nil
651654
}
652655

653656
// Returns a URL string created from the NSURLComponents. If the NSURLComponents has an authority component (user, password, host or port) and a path component, then the path must either begin with "/" or be an empty string. If the NSURLComponents does not have an authority component (user, password, host or port) and has a path component, the path component must not start with "//". If those requirements are not met, nil is returned.

TestFoundation/TestNSURL.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ class TestNSURLComponents : XCTestCase {
424424
return [
425425
("test_string", test_string),
426426
("test_port", test_portSetter),
427+
("test_URLRelativeToURL", test_URLRelativeToURL),
427428
]
428429
}
429430

@@ -447,4 +448,46 @@ class TestNSURLComponents : XCTestCase {
447448
XCTAssertEqual(receivedString, expectedString, "expected \(expectedString) but received \(receivedString)")
448449
}
449450

451+
func test_URLRelativeToURL() {
452+
453+
let baseURL = NSURL(string: "https://www.example.com")
454+
455+
/* test NSURLComponents without authority */
456+
let compWithAuthority = NSURLComponents(string: "https://www.swift.org")
457+
compWithAuthority!.path = "/path/to/file with space.html"
458+
compWithAuthority!.query = "id=23&search=Foo Bar"
459+
var expectedString = "https://www.swift.org/path/to/file%20with%20space.html?id=23&search=Foo%20Bar"
460+
XCTAssertEqual(compWithAuthority!.string, expectedString, "expected \(expectedString) but received \(compWithAuthority!.string)")
461+
462+
var url = compWithAuthority!.URLRelativeToURL(baseURL)
463+
XCTAssertNotNil(url)
464+
XCTAssertNil(url!.baseURL)
465+
XCTAssertEqual(url!.absoluteString, expectedString, "expected \(expectedString) but received \(url!.absoluteString)")
466+
467+
compWithAuthority!.path = "path/to/file with space.html" //must start with /
468+
XCTAssertNil(compWithAuthority!.string) // must be nil
469+
470+
url = compWithAuthority!.URLRelativeToURL(baseURL)
471+
XCTAssertNil(url) //must be nil
472+
473+
474+
475+
/* test NSURLComponents without authority */
476+
let compWithoutAuthority = NSURLComponents()
477+
compWithoutAuthority.path = "path/to/file with space.html"
478+
compWithoutAuthority.query = "id=23&search=Foo Bar"
479+
expectedString = "path/to/file%20with%20space.html?id=23&search=Foo%20Bar"
480+
XCTAssertEqual(compWithoutAuthority.string, expectedString, "expected \(expectedString) but received \(compWithoutAuthority.string)")
481+
482+
url = compWithoutAuthority.URLRelativeToURL(baseURL)
483+
XCTAssertNotNil(url)
484+
expectedString = "https://www.example.com/path/to/file%20with%20space.html?id=23&search=Foo%20Bar"
485+
XCTAssertEqual(url!.absoluteString, expectedString, "expected \(expectedString) but received \(url!.absoluteString)")
486+
487+
compWithoutAuthority.path = "//path/to/file with space.html" //shouldn't start with //
488+
XCTAssertNil(compWithoutAuthority.string) // must be nil
489+
490+
url = compWithoutAuthority.URLRelativeToURL(baseURL)
491+
XCTAssertNil(url) //must be nil
492+
}
450493
}

0 commit comments

Comments
 (0)