diff --git a/Cara/Cara.xcodeproj/project.pbxproj b/Cara/Cara.xcodeproj/project.pbxproj index e8efe76..12218a1 100644 --- a/Cara/Cara.xcodeproj/project.pbxproj +++ b/Cara/Cara.xcodeproj/project.pbxproj @@ -269,6 +269,7 @@ TargetAttributes = { 9D551C702273749400DDD584 = { CreatedOnToolsVersion = 10.2.1; + LastSwiftMigration = 1130; }; 9D551C87227374FF00DDD584 = { CreatedOnToolsVersion = 10.2.1; @@ -281,6 +282,7 @@ hasScannedForEncodings = 0; knownRegions = ( en, + Base, ); mainGroup = 9D551C672273749400DDD584; productRefGroup = 9D551C722273749400DDD584 /* Products */; @@ -508,7 +510,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.icapps.cara.Cara; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -535,7 +537,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.icapps.cara.Cara; PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 4.2; + SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/Cara/Cara.xcodeproj/xcshareddata/xcschemes/Cara Tv.xcscheme b/Cara/Cara.xcodeproj/xcshareddata/xcschemes/Cara Tv.xcscheme index b25eeb4..3479922 100644 --- a/Cara/Cara.xcodeproj/xcshareddata/xcschemes/Cara Tv.xcscheme +++ b/Cara/Cara.xcodeproj/xcshareddata/xcschemes/Cara Tv.xcscheme @@ -1,6 +1,6 @@ - - - - - - - - + onlyGenerateCoverageForSpecifiedTargets = "YES"> + + + + - - - - - - - - RequestHeaders? { + return mockedHeaders + } } diff --git a/Example/Tests/Specs/Service/ServiceSpec.swift b/Example/Tests/Specs/Service/ServiceSpec.swift index 82b6ec9..08e9c54 100644 --- a/Example/Tests/Specs/Service/ServiceSpec.swift +++ b/Example/Tests/Specs/Service/ServiceSpec.swift @@ -93,7 +93,7 @@ class ServiceSpec: QuickSpec { let interceptor = MockedInterceptor() interceptor.interceptHandle = { error, retry in - configuration.headers = ["Some": "Header"] + configuration.mockedHeaders = ["Some": "Header"] self.stub(http(.get, uri: "https://relative.com/request"), http(200)) return false } @@ -114,7 +114,7 @@ class ServiceSpec: QuickSpec { let interceptor = MockedInterceptor() interceptor.interceptHandle = { error, retry in - configuration.headers = ["Some": "Header"] + configuration.mockedHeaders = ["Some": "Header"] self.stub(http(.get, uri: "https://relative.com/request"), http(200)) DispatchQueue.global().asyncAfter(deadline: .now() + 0.1, execute: retry) return true @@ -138,7 +138,7 @@ class ServiceSpec: QuickSpec { var count: Int = 0 interceptor.interceptHandle = { error, retry in count += 1 - configuration.headers = ["Some": "Header"] + configuration.mockedHeaders = ["Some": "Header"] if count > 1 { // Make sure the second try also fails. self.stub(http(.get, uri: "https://relative.com/request"), http(200)) diff --git a/Sources/Configuration/Configuration.swift b/Sources/Configuration/Configuration.swift index c5df4ee..cc64fa9 100644 --- a/Sources/Configuration/Configuration.swift +++ b/Sources/Configuration/Configuration.swift @@ -14,10 +14,15 @@ public typealias PublicKeys = [String: String] public protocol Configuration { /// The base url that is appended to the `Request`'s relative url. var baseURL: URL? { get } - /// Set the headers that will be used for all the requests. - var headers: RequestHeaders? { get } /// Set the public keys for the hosts. var publicKeys: PublicKeys? { get } /// Set the loggers when you want to receive more information on the requests. var loggers: [Logger]? { get } + /// Set the headers that will be used for all the requests. The current request is passed + /// so that some additional logic can be applied before returning the headers. + /// + /// - parameters request: The current request. + /// + /// - return: The request headers. + func headers(for request: Request) -> RequestHeaders? } diff --git a/Sources/Request/Request+URLRequest.swift b/Sources/Request/Request+URLRequest.swift index c81cf28..d3af35f 100644 --- a/Sources/Request/Request+URLRequest.swift +++ b/Sources/Request/Request+URLRequest.swift @@ -52,7 +52,9 @@ extension Request { private func makeHeaders(with configuration: Configuration) -> RequestHeaders? { var requestHeaders: RequestHeaders? // When headers are available in the configuration we use them. - if let headers = configuration.headers { requestHeaders = headers } + if let headers = configuration.headers(for: self) { + requestHeaders = headers + } // When headers are available in this request we use them. if let headers = headers { requestHeaders = requestHeaders ?? RequestHeaders() diff --git a/Sources/Service/PublicKeyPinningService.swift b/Sources/Service/PublicKeyPinningService.swift index c63167f..c690fa8 100644 --- a/Sources/Service/PublicKeyPinningService.swift +++ b/Sources/Service/PublicKeyPinningService.swift @@ -54,7 +54,7 @@ class PublicKeyPinningService { var error: Unmanaged? if let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, &error) { - var keyWithHeader = Data(bytes: rsa2048Asn1Header) + var keyWithHeader = Data(rsa2048Asn1Header) keyWithHeader.append(publicKeyData as Data) let sha256 = keyWithHeader.sha256 // When the public keys don't match continue to the next certificate. @@ -71,7 +71,8 @@ class PublicKeyPinningService { private extension Data { var sha256: Data { var hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH)) - withUnsafeBytes { _ = CC_SHA256($0, CC_LONG(self.count), &hash) } - return Data(bytes: hash) + withUnsafeBytes { _ = CC_SHA256($0.baseAddress, CC_LONG(self.count), &hash) } + return Data(hash) + } }