Skip to content

Commit 071de50

Browse files
committed
Merge branch 'pr/302'
2 parents c47a74e + 6ace5b5 commit 071de50

File tree

4 files changed

+81
-30
lines changed

4 files changed

+81
-30
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
EADE0BCB1BD15E0000C49C64 /* NSXMLNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B8D1BD15DFF00C49C64 /* NSXMLNode.swift */; };
340340
EADE0BCC1BD15E0000C49C64 /* NSXMLNodeOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B8E1BD15DFF00C49C64 /* NSXMLNodeOptions.swift */; };
341341
EADE0BCD1BD15E0000C49C64 /* NSXMLParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B8F1BD15DFF00C49C64 /* NSXMLParser.swift */; };
342+
F9E0BB371CA70B8000F7FF3C /* TestNSURLCredential.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9E0BB361CA70B8000F7FF3C /* TestNSURLCredential.swift */; };
342343
/* End PBXBuildFile section */
343344

344345
/* Begin PBXContainerItemProxy section */
@@ -736,6 +737,7 @@
736737
EADE0B8D1BD15DFF00C49C64 /* NSXMLNode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSXMLNode.swift; sourceTree = "<group>"; };
737738
EADE0B8E1BD15DFF00C49C64 /* NSXMLNodeOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSXMLNodeOptions.swift; sourceTree = "<group>"; };
738739
EADE0B8F1BD15DFF00C49C64 /* NSXMLParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSXMLParser.swift; sourceTree = "<group>"; };
740+
F9E0BB361CA70B8000F7FF3C /* TestNSURLCredential.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLCredential.swift; sourceTree = "<group>"; };
739741
/* End PBXFileReference section */
740742

741743
/* Begin PBXFrameworksBuildPhase section */
@@ -1201,6 +1203,7 @@
12011203
61D6C9EE1C1DFE9500DEF583 /* TestNSTimer.swift */,
12021204
84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */,
12031205
EA66F6431BF1619600136161 /* TestNSURL.swift */,
1206+
F9E0BB361CA70B8000F7FF3C /* TestNSURLCredential.swift */,
12041207
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */,
12051208
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */,
12061209
555683BC1C1250E70041D4C6 /* TestNSUserDefaults.swift */,
@@ -1976,6 +1979,7 @@
19761979
5B13B32F1C582D4C00651CE2 /* TestNSGeometry.swift in Sources */,
19771980
5B13B3351C582D4C00651CE2 /* TestNSKeyedUnarchiver.swift in Sources */,
19781981
5B13B33D1C582D4C00651CE2 /* TestNSPipe.swift in Sources */,
1982+
F9E0BB371CA70B8000F7FF3C /* TestNSURLCredential.swift in Sources */,
19791983
5B13B3341C582D4C00651CE2 /* TestNSKeyedArchiver.swift in Sources */,
19801984
5B13B3441C582D4C00651CE2 /* TestNSSet.swift in Sources */,
19811985
5B13B3321C582D4C00651CE2 /* TestNSIndexSet.swift in Sources */,

Foundation/NSURLCredential.swift

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ public enum NSURLCredentialPersistence : UInt {
3131
@discussion This class is an immutable object representing an authentication credential. The actual type of the credential is determined by the constructor called in the categories declared below.
3232
*/
3333
public class NSURLCredential : NSObject, NSSecureCoding, NSCopying {
34+
internal var _user : String
35+
internal var _password : String
36+
internal var _persistence : NSURLCredentialPersistence
37+
38+
/*!
39+
@method initWithUser:password:persistence:
40+
@abstract Initialize a NSURLCredential with a user and password
41+
@param user the username
42+
@param password the password
43+
@param persistence enum that says to store per session, permanently or not at all
44+
@result The initialized NSURLCredential
45+
*/
46+
public init(user: String, password: String, persistence: NSURLCredentialPersistence) {
47+
guard persistence != .Permanent && persistence != .Synchronizable else {
48+
NSUnimplemented()
49+
}
50+
_user = user
51+
_password = password
52+
_persistence = persistence
53+
super.init()
54+
}
55+
56+
/*!
57+
@method credentialWithUser:password:persistence:
58+
@abstract Create a new NSURLCredential with a user and password
59+
@param user the username
60+
@param password the password
61+
@param persistence enum that says to store per session, permanently or not at all
62+
@result The new autoreleased NSURLCredential
63+
*/
3464

3565
public required init?(coder aDecoder: NSCoder) {
3666
NSUnimplemented()
@@ -57,36 +87,14 @@ public class NSURLCredential : NSObject, NSSecureCoding, NSCopying {
5787
@abstract Determine whether this credential is or should be stored persistently
5888
@result A value indicating whether this credential is stored permanently, per session or not at all.
5989
*/
60-
public var persistence: NSURLCredentialPersistence { NSUnimplemented() }
61-
}
62-
63-
extension NSURLCredential {
64-
65-
/*!
66-
@method initWithUser:password:persistence:
67-
@abstract Initialize a NSURLCredential with a user and password
68-
@param user the username
69-
@param password the password
70-
@param persistence enum that says to store per session, permanently or not at all
71-
@result The initialized NSURLCredential
72-
*/
73-
public convenience init(user: String, password: String, persistence: NSURLCredentialPersistence) { NSUnimplemented() }
74-
75-
/*!
76-
@method credentialWithUser:password:persistence:
77-
@abstract Create a new NSURLCredential with a user and password
78-
@param user the username
79-
@param password the password
80-
@param persistence enum that says to store per session, permanently or not at all
81-
@result The new autoreleased NSURLCredential
82-
*/
90+
public var persistence: NSURLCredentialPersistence { return _persistence }
8391

8492
/*!
8593
@method user
8694
@abstract Get the username
8795
@result The user string
88-
*/
89-
public var user: String? { NSUnimplemented() }
96+
*/
97+
public var user: String? { return _user }
9098

9199
/*!
92100
@method password
@@ -95,9 +103,9 @@ extension NSURLCredential {
95103
@discussion This method might actually attempt to retrieve the
96104
password from an external store, possible resulting in prompting,
97105
so do not call it unless needed.
98-
*/
99-
public var password: String? { NSUnimplemented() }
100-
106+
*/
107+
public var password: String? { return _password }
108+
101109
/*!
102110
@method hasPassword
103111
@abstract Find out if this credential has a password, without trying to get it
@@ -106,8 +114,11 @@ extension NSURLCredential {
106114
external store, the password method may return nil even if this
107115
method returns YES, since getting the password may fail, or the
108116
user may refuse access.
109-
*/
110-
public var hasPassword: Bool { NSUnimplemented() }
117+
*/
118+
public var hasPassword: Bool {
119+
// Currently no support for SecTrust/SecIdentity, always return true
120+
return true
121+
}
111122
}
112123

113124
// TODO: We have no implementation for Security.framework primitive types SecIdentity and SecTrust yet
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
12+
import Foundation
13+
import XCTest
14+
#else
15+
import SwiftFoundation
16+
import SwiftXCTest
17+
#endif
18+
19+
class TestNSURLCredential : XCTestCase {
20+
21+
static var allTests: [(String, TestNSURLCredential -> () throws -> Void)] {
22+
return [
23+
("test_construction", test_construction)
24+
]
25+
}
26+
27+
func test_construction() {
28+
let credential = NSURLCredential(user: "swiftUser", password: "swiftPassword", persistence: .ForSession)
29+
XCTAssertNotNil(credential)
30+
XCTAssertEqual(credential.user, "swiftUser")
31+
XCTAssertEqual(credential.password, "swiftPassword")
32+
XCTAssertEqual(credential.persistence, NSURLCredentialPersistence.ForSession)
33+
XCTAssertEqual(credential.hasPassword, true)
34+
}
35+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ XCTMain([
6363
testCase(TestNSTimeZone.allTests),
6464
testCase(TestNSURL.allTests),
6565
testCase(TestNSURLComponents.allTests),
66+
testCase(TestNSURLCredential.allTests),
6667
testCase(TestNSURLRequest.allTests),
6768
testCase(TestNSURLResponse.allTests),
6869
testCase(TestNSHTTPURLResponse.allTests),

0 commit comments

Comments
 (0)