Printing description of error: ▿ AFError ▿ serverTrustEvaluationFailed : 1 element ▿ reason : ServerTrustFailureReason ▿ noRequiredEvaluator : 1 element - host : "***" (lldb) po error.debugDescription "Server trust evaluation failed due to reason: A ServerTrustEvaluating value is required for host *** but none was found."
原因是配置了白名单
1 2 3 4 5 6 7 8 9 10 11
var evaluators: [String: ServerTrustEvaluating] = [:]
let evaluators: [String: ServerTrustEvaluating] = [ "*.yourdomain.com": PinnedCertificatesTrustEvaluator() ] // 白名单 allHostsMustBeEvaluated: true let serverTrust =ServerTrustManager(allHostsMustBeEvaluated: true, evaluators: evaluators)
// 从给定的文件名创建证书:SecCertificate对象。 staticfunccertificate(filename: String) -> SecCertificate { let filePath =Bundle.test.path(forResource: filename, ofType: "cer")! let data =try!Data(contentsOf: URL(fileURLWithPath: filePath)) let certificate =SecCertificateCreateWithData(nil, data asCFData)!
return certificate } }
functestThatExpiredCertificateRequestFailsWhenPinningLeafPublicKeyWithCertificateChainValidation() { // Given // 这里直接从证书提取公钥 let keys = [TestCertificates.leaf].af.publicKeys let evaluators = [expiredHost: PublicKeysTrustEvaluator(keys: keys)]
let manager =Session(configuration: configuration, serverTrustManager: ServerTrustManager(evaluators: evaluators))
let expectation = expectation(description: "\(expiredURLString)") var error: AFError?
// When manager.request(expiredURLString) .response { resp in error = resp.error expectation.fulfill() }
waitForExpectations(timeout: timeout)
// Then XCTAssertNotNil(error, "error should not be nil") XCTAssertEqual(error?.isServerTrustEvaluationError, true)
ifcaselet .serverTrustEvaluationFailed(reason)?= error { if#available(iOS12, macOS10.14, tvOS12, watchOS5, *) { XCTAssertTrue(reason.isTrustEvaluationFailed, "should be .trustEvaluationFailed") } else { XCTAssertTrue(reason.isDefaultEvaluationFailed, "should be .defaultEvaluationFailed") } } else { XCTFail("error should be .serverTrustEvaluationFailed") } }