2019-01-05 17:37:34 +00:00
|
|
|
//
|
|
|
|
// TestURLSessionDelegate.swift
|
|
|
|
// MasKitTests
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 1/5/19.
|
|
|
|
// Copyright © 2019 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
/// Delegate for network requests initiated from tests.
|
|
|
|
class TestURLSessionDelegate: NSObject, URLSessionDelegate {
|
2021-03-22 05:25:18 +00:00
|
|
|
func urlSession(
|
|
|
|
_: URLSession,
|
|
|
|
didReceive challenge: URLAuthenticationChallenge,
|
|
|
|
completionHandler: (
|
|
|
|
URLSession.AuthChallengeDisposition,
|
|
|
|
URLCredential?
|
|
|
|
) -> Void
|
|
|
|
) {
|
2019-01-05 17:37:34 +00:00
|
|
|
// For example, you may want to override this to accept some self-signed certs here.
|
2021-03-22 05:25:18 +00:00
|
|
|
let methodIsServerTrust = challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
|
|
|
|
if methodIsServerTrust, Constants.selfSignedHosts.contains(challenge.protectionSpace.host) {
|
2019-01-05 17:37:34 +00:00
|
|
|
// Allow the self-signed cert.
|
|
|
|
let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
|
|
|
|
completionHandler(.useCredential, credential)
|
|
|
|
} else {
|
|
|
|
// You *have* to call completionHandler, so call
|
|
|
|
// it to do the default action.
|
|
|
|
completionHandler(.performDefaultHandling, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 05:46:17 +00:00
|
|
|
enum Constants {
|
2019-01-05 17:37:34 +00:00
|
|
|
// A list of hosts you allow self-signed certificates on.
|
|
|
|
// You'd likely have your dev/test servers here.
|
|
|
|
// Please don't put your production server here!
|
|
|
|
static let selfSignedHosts: Set<String> =
|
|
|
|
["dev.example.com", "test.example.com"]
|
|
|
|
}
|
|
|
|
}
|