1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
class TestURLCredentialStorage : XCTestCase {
func test_storageStartsEmpty() {
let storage = URLCredentialStorage.shared
XCTAssertEqual(storage.allCredentials.count, 0)
}
func test_sessionCredentialGetsReturnedForTheRightProtectionSpace() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
expectChanges(storage.allCredentials.count, by: 1) {
storage.set(credential, for: space)
}
XCTAssertEqual(storage.credentials(for: space)?.count, 1)
guard let credentials = storage.credentials(for: space),
let recovered = credentials[try XCTUnwrap(credential.user)] else {
XCTFail("Credential not found in storage")
return
}
XCTAssertEqual(recovered, credential)
storage.remove(credential, for: space)
}
func test_sessionCredentialDoesNotGetReturnedForTheWrongProtectionSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let wrongSpace = URLProtectionSpace(host: "example2.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space)
XCTAssertNil(storage.credentials(for: wrongSpace))
storage.remove(credential, for: space)
}
func test_sessionCredentialBecomesDefaultForProtectionSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space)
XCTAssertEqual(storage.defaultCredential(for: space), credential)
storage.remove(credential, for: space)
}
func test_sessionCredentialGetsReturnedAsDefaultIfSetAsDefaultForSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.setDefaultCredential(credential, for: space)
XCTAssertEqual(storage.defaultCredential(for: space), credential)
storage.remove(credential, for: space)
}
func test_sessionCredentialGetsReturnedIfSetAsDefaultForSpace() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
expectChanges(storage.allCredentials.count, by: 1) {
storage.setDefaultCredential(credential, for: space)
}
XCTAssertEqual(storage.credentials(for: space)?.count, 1)
guard let credentials = storage.credentials(for: space),
let recovered = credentials[try XCTUnwrap(credential.user)] else {
XCTFail("Credential not found in storage")
return
}
XCTAssertEqual(recovered, credential)
storage.remove(credential, for: space)
}
func test_sessionCredentialDoesNotGetReturnedIfSetAsDefaultForOtherSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let wrongSpace = URLProtectionSpace(host: "example2.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.setDefaultCredential(credential, for: space)
XCTAssertNil(storage.defaultCredential(for: wrongSpace))
storage.remove(credential, for: space)
}
func test_sessionCredentialDoesNotGetReturnedWhenNotAddedAsDefault() {
let storage = URLCredentialStorage.shared
let credential1 = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let credential2 = URLCredential(user: "user2", password: "password2", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential2, for: space)
storage.setDefaultCredential(credential1, for: space)
XCTAssertNotEqual(storage.defaultCredential(for: space), credential2)
storage.remove(credential2, for: space)
storage.remove(credential1, for: space)
}
func test_sessionCredentialCanBeRemovedFromSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space)
expectChanges(storage.allCredentials.count, by: -1) {
storage.remove(credential, for: space)
}
XCTAssertNil(storage.credentials(for: space))
storage.remove(credential, for: space)
}
func test_sessionDefaultCredentialCanBeRemovedFromSpace() {
let storage = URLCredentialStorage.shared
let credential1 = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let credential2 = URLCredential(user: "user2", password: "password2", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.setDefaultCredential(credential1, for: space)
storage.set(credential2, for: space)
storage.remove(credential1, for: space)
XCTAssertNil(storage.defaultCredential(for: space))
XCTAssertEqual(storage.credentials(for: space)?.count, 1)
storage.remove(credential2, for: space)
}
#if NS_FOUNDATION_NETWORKING_URLCREDENTIALSTORAGE_SYNCHRONIZABLE_ALLOWED
/*
swift-corelibs-foundation does not support synchronizable credentials, refusing to save them much like Darwin when logged out of iCloud.
Thus, these tests cannot succeed — there is never a credential to remove.
If we ever implement synchronizable credentials, uncomment this.
*/
func test_synchronizableCredentialCanBeRemovedWithRightOptions() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .synchronizable)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space)
expectChanges(storage.allCredentials.count, by: -1) {
storage.remove(credential, for: space, options: [NSURLCredentialStorageRemoveSynchronizableCredentials: NSNumber(value: true)])
}
}
func test_synchronizableCredentialWillNotBeRemovedWithoutRightOptions() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .synchronizable)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space)
// No options, no removal of synchronizable credentials
storage.remove(credential, for: space)
XCTAssertEqual(storage.allCredentials.count, 1)
// Empty options, no removal of synchronizable credentials
storage.remove(credential, for: space, options: [:])
XCTAssertEqual(storage.allCredentials.count, 1)
// Invalid type, no removal of synchronizable credentials
storage.remove(credential, for: space, options: [NSURLCredentialStorageRemoveSynchronizableCredentials: "of course" as NSString])
XCTAssertEqual(storage.allCredentials.count, 1)
// False value, no removal of synchronizable credentials
storage.remove(credential, for: space, options: [NSURLCredentialStorageRemoveSynchronizableCredentials: NSNumber(value: false)])
XCTAssertEqual(storage.allCredentials.count, 1)
storage.remove(credential, for: space, options: [NSURLCredentialStorageRemoveSynchronizableCredentials: NSNumber(value: true)])
}
#endif
func test_storageCanRemoveArbitraryCredentialWithoutFailing() {
let storage = URLCredentialStorage.shared
let credential1 = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let credential2 = URLCredential(user: "user2", password: "password2", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential2, for: space)
XCTAssertNoThrow(storage.remove(credential1, for: space))
storage.remove(credential2, for: space)
}
func test_storageWillNotSaveCredentialsWithoutPersistence() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .none)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
expectNoChanges(storage.allCredentials.count) {
storage.set(credential, for: space)
}
storage.remove(credential, for: space)
}
// TODO: test that credentials without name/password are not saved. There's
// no support for other kind of credentials, so it cannot be tested right
// now.
// TODO: test that credentials without name/password can be set as default.
// There's no support for other kinds of credentials, so it cannot be tested
// right now.
func test_storageWillSendNotificationWhenAddingNewCredential() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let notificationReceived = expectation(description: "Notification Received")
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.set(credential, for: space)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential, for: space)
}
func test_storageWillSendNotificationWhenAddingExistingCredentialToDifferentSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space1 = URLProtectionSpace(host: "example1.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let space2 = URLProtectionSpace(host: "example2.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space1)
let notificationReceived = expectation(description: "Notification Received")
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.set(credential, for: space2)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential, for: space1)
storage.remove(credential, for: space2)
}
func test_storageWillNotSendNotificationWhenAddingExistingCredential() {
let storage = URLCredentialStorage.shared
let credential1 = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let credential2 = URLCredential(user: "user1", password: "password1", persistence: .forSession) // intentially equal
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential1, for: space)
let notificationReceived = expectation(description: "Notification Received")
notificationReceived.isInverted = true
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.set(credential2, for: space)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential1, for: space)
storage.remove(credential2, for: space)
}
func test_storageWillSendNotificationWhenAddingNewDefaultCredential() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let notificationReceived = expectation(description: "Notification Received")
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.setDefaultCredential(credential, for: space)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential, for: space)
}
func test_storageWillNotSendNotificationWhenAddingExistingDefaultCredential() {
let storage = URLCredentialStorage.shared
let credential1 = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let credential2 = URLCredential(user: "user1", password: "password1", persistence: .forSession) // intentionally equal
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.setDefaultCredential(credential1, for: space)
let notificationReceived = expectation(description: "Notification Received")
notificationReceived.isInverted = true
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.setDefaultCredential(credential2, for: space)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential1, for: space)
storage.remove(credential2, for: space)
}
func test_storageWillSendNotificationWhenAddingDifferentDefaultCredential() {
let storage = URLCredentialStorage.shared
let credential1 = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let credential2 = URLCredential(user: "user2", password: "password2", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.setDefaultCredential(credential1, for: space)
let notificationReceived = expectation(description: "Notification Received")
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.setDefaultCredential(credential2, for: space)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential1, for: space)
storage.remove(credential2, for: space)
}
func test_storageWillSendNotificationWhenRemovingExistingCredential() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space)
let notificationReceived = expectation(description: "Notification Received")
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.remove(credential, for: space)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential, for: space)
}
func test_storageWillNotSendNotificationWhenRemovingExistingCredentialInOtherSpace() {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space1 = URLProtectionSpace(host: "example1.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let space2 = URLProtectionSpace(host: "example2.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.set(credential, for: space1)
let notificationReceived = expectation(description: "Notification Received")
notificationReceived.isInverted = true
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
storage.remove(credential, for: space2)
waitForExpectations(timeout: 0)
NotificationCenter.default.removeObserver(observer)
storage.remove(credential, for: space1)
}
func test_storageWillSendNotificationWhenRemovingDefaultNotification() {
let storage = URLCredentialStorage.shared
// TODO: this test will be better is we can create credentials without
// user/password, but currently is not possible. At least we are testing
// that only one notification is fired.
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
storage.setDefaultCredential(credential, for: space)
let notificationReceived = expectation(description: "Notification Received")
let observer = NotificationCenter.default.addObserver(forName: .NSURLCredentialStorageChanged, object: storage, queue: nil) { _ in
notificationReceived.fulfill()
}
defer { NotificationCenter.default.removeObserver(observer) }
storage.remove(credential, for: space)
waitForExpectations(timeout: 0)
}
func test_taskBasedGetCredentialsReturnsCredentialsForSpace() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: try XCTUnwrap(URL(string: "http://example.com/")))
storage.set(credential, for: space)
let completionCallbackCalled = expectation(description: "Completion callback called")
storage.getCredentials(for: space, task: task) { credentials in
completionCallbackCalled.fulfill()
XCTAssertNotNil(credentials)
XCTAssertEqual(credentials?["user1"], credential)
}
waitForExpectations(timeout: 0)
storage.remove(credential, for: space)
}
func test_taskBasedSetCredentialStoresGivenCredentials() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: try XCTUnwrap(URL(string: "http://example.com/")))
storage.set(credential, for: space, task: task)
let expectation = self.expectation(description: "Done")
storage.getCredentials(for: space, task: task) { credentials in
guard let credentials = credentials,
let user = credential.user,
let recovered = credentials[user] else {
XCTFail("Credential not found in storage")
return
}
XCTAssertEqual(recovered, credential)
expectation.fulfill()
}
waitForExpectations(timeout: 10)
storage.remove(credential, for: space)
}
func test_taskBasedRemoveCredentialDeletesCredentialsFromSpace() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: try XCTUnwrap(URL(string: "http://example.com/")))
storage.set(credential, for: space)
expectChanges(storage.allCredentials.count, by: -1) {
storage.remove(credential, for: space, options: [:], task: task)
}
}
func test_taskBasedGetDefaultCredentialReturnsTheDefaultCredential() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: try XCTUnwrap(URL(string: "http://example.com/")))
storage.setDefaultCredential(credential, for: space)
let completionCallbackCalled = expectation(description: "Completion callback called")
storage.getDefaultCredential(for: space, task: task) {
completionCallbackCalled.fulfill()
XCTAssertEqual($0, credential)
}
waitForExpectations(timeout: 0)
storage.remove(credential, for: space)
}
func test_taskBasedSetDefaultCredentialStoresTheDefaultCredential() throws {
let storage = URLCredentialStorage.shared
let credential = URLCredential(user: "user1", password: "password1", persistence: .forSession)
let space = URLProtectionSpace(host: "example.com", port: 0, protocol: NSURLProtectionSpaceHTTP, realm: nil, authenticationMethod: NSURLAuthenticationMethodDefault)
let urlSession = URLSession.shared
let task = urlSession.dataTask(with: try XCTUnwrap(URL(string: "http://example.com/")))
expectChanges(storage.allCredentials.count, by: 1) {
storage.setDefaultCredential(credential, for: space, task: task)
}
XCTAssertEqual(storage.defaultCredential(for: space), credential)
storage.remove(credential, for: space)
}
}
|