2
0

BaseTestCase.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // BaseTestCase.swift
  3. //
  4. // Copyright (c) 2014-2018 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import Alamofire
  25. import Foundation
  26. import XCTest
  27. class BaseTestCase: XCTestCase {
  28. enum SkipVersion {
  29. case twenty
  30. case none
  31. var shouldSkip: Bool {
  32. switch self {
  33. case .twenty:
  34. if #available(macOS 11, iOS 14, tvOS 14, watchOS 7, *) {
  35. false
  36. } else {
  37. true
  38. }
  39. case .none:
  40. false
  41. }
  42. }
  43. var reason: String {
  44. switch self {
  45. case .twenty:
  46. "Skipped due to being iOS 13 or below."
  47. case .none:
  48. "This should never skip."
  49. }
  50. }
  51. }
  52. let timeout: TimeInterval = 10
  53. var skipVersion: SkipVersion { .none }
  54. var testDirectoryURL: URL {
  55. FileManager.temporaryDirectoryURL.appendingPathComponent("org.alamofire.tests")
  56. }
  57. var temporaryFileURL: URL {
  58. testDirectoryURL.appendingPathComponent(UUID().uuidString)
  59. }
  60. private var session: Session?
  61. override func setUp() {
  62. FileManager.createDirectory(at: testDirectoryURL)
  63. super.setUp()
  64. }
  65. override func setUpWithError() throws {
  66. try XCTSkipIf(skipVersion.shouldSkip, skipVersion.reason)
  67. try super.setUpWithError()
  68. }
  69. override func tearDown() {
  70. session = nil
  71. FileManager.removeAllItemsInsideDirectory(at: testDirectoryURL)
  72. clearCredentials()
  73. clearCookies()
  74. super.tearDown()
  75. }
  76. func clearCookies(for storage: HTTPCookieStorage = .shared) {
  77. storage.cookies?.forEach { storage.deleteCookie($0) }
  78. }
  79. func clearCredentials(for storage: URLCredentialStorage = .shared) {
  80. for (protectionSpace, credentials) in storage.allCredentials {
  81. for (_, credential) in credentials {
  82. storage.remove(credential, for: protectionSpace)
  83. }
  84. }
  85. }
  86. func url(forResource fileName: String, withExtension ext: String) -> URL {
  87. Bundle.test.url(forResource: fileName, withExtension: ext)!
  88. }
  89. func stored(_ session: Session) -> Session {
  90. self.session = session
  91. return session
  92. }
  93. /// Runs assertions on a particular `DispatchQueue`.
  94. ///
  95. /// - Parameters:
  96. /// - queue: The `DispatchQueue` on which to run the assertions.
  97. /// - assertions: Closure containing assertions to run
  98. @MainActor
  99. func assert(on queue: DispatchQueue, assertions: @escaping @Sendable () -> Void) {
  100. let expect = expectation(description: "all assertions are complete")
  101. queue.async {
  102. assertions()
  103. expect.fulfill()
  104. }
  105. waitForExpectations(timeout: timeout)
  106. }
  107. }