Skip to content

Commit

Permalink
URLCredential NSCoding test
Browse files Browse the repository at this point in the history
URLCredential NSCoding implementation

URLCredential isEqual override

status update

status
  • Loading branch information
Sergey Minakov committed Nov 8, 2016
1 parent af83051 commit d963095
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Docs/Status.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ There is no _Complete_ status for test coverage because there are always additio
|------------------------------|-----------------|---------------|--------------------------------------------------------------------------------------------------------------------|
| `URLAuthenticationChallenge` | Unimplemented | None | |
| `URLCache` | Unimplemented | None | |
| `URLCredential` | Mostly Complete | Incomplete | `NSCoding` and `NSCopying` remain unimplemented |
| `URLCredential` | Mostly Complete | Incomplete | `NSCopying` remains unimplemented |
| `URLCredentialStorage` | Unimplemented | None | |
| `NSURLError*` | Complete | N/A | |
| `URLProtectionSpace` | Unimplemented | None | |
Expand Down
39 changes: 37 additions & 2 deletions Foundation/NSURLCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,32 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
*/

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
guard aDecoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

func bridgeString(_ value: NSString) -> String? {
return String._unconditionallyBridgeFromObjectiveC(value)
}

let encodedUser = aDecoder.decodeObject(forKey: "NS._user") as! NSString
self._user = bridgeString(encodedUser)!

let encodedPassword = aDecoder.decodeObject(forKey: "NS._password") as! NSString
self._password = bridgeString(encodedPassword)!

let encodedPersistence = aDecoder.decodeObject(forKey: "NS._persistence") as! NSNumber
self._persistence = Persistence(rawValue: encodedPersistence.uintValue)!
}

open func encode(with aCoder: NSCoder) {
NSUnimplemented()
guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}

aCoder.encode(self._user._bridgeToObjectiveC(), forKey: "NS._user")
aCoder.encode(self._password._bridgeToObjectiveC(), forKey: "NS._password")
aCoder.encode(self._persistence.rawValue._bridgeToObjectiveC(), forKey: "NS._persistence")
}

static public var supportsSecureCoding: Bool {
Expand All @@ -84,6 +105,20 @@ open class URLCredential : NSObject, NSSecureCoding, NSCopying {
return self
}

open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? URLCredential else {
return false
}

guard other !== self else {
return true
}

return other._user == self._user
&& other._password == self._password
&& other._persistence == self._persistence
}

/*!
@method persistence
@abstract Determine whether this credential is or should be stored persistently
Expand Down
9 changes: 8 additions & 1 deletion TestFoundation/TestNSURLCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class TestNSURLCredential : XCTestCase {
static var allTests: [(String, (TestNSURLCredential) -> () throws -> Void)] {
return [
("test_construction", test_construction),
("test_copy", test_copy)
("test_copy", test_copy),
("test_NSCoding", test_NSCoding)
]
}

Expand All @@ -39,4 +40,10 @@ class TestNSURLCredential : XCTestCase {
let copy = credential.copy() as! URLCredential
XCTAssertTrue(copy.isEqual(credential))
}

func test_NSCoding() {
let credentialA = URLCredential(user: "swiftUser", password: "swiftPassword", persistence: .forSession)
let credentialB = NSKeyedUnarchiver.unarchiveObject(with: NSKeyedArchiver.archivedData(withRootObject: credentialA)) as! URLCredential
XCTAssertEqual(credentialA, credentialB, "Archived then unarchived url credential must be equal.")
}
}

0 comments on commit d963095

Please sign in to comment.