mirror of
https://github.com/mas-cli/mas
synced 2024-11-22 03:23:08 +00:00
Merge pull request #361 from mas-cli/clean
♻️ Remove unused test helpers
This commit is contained in:
commit
c88a98892e
5 changed files with 1 additions and 124 deletions
|
@ -19,21 +19,6 @@ class NetworkSessionMock: NetworkSession {
|
|||
var data: Data?
|
||||
var error: Error?
|
||||
|
||||
/// Creates a mock data task
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - url: unused
|
||||
/// - completionHandler: Closure which is delivered both data and error properties (only one should be non-nil)
|
||||
/// - Returns: Mock data task
|
||||
func dataTask(with _: URL, completionHandler: @escaping CompletionHandler) -> URLSessionDataTask {
|
||||
let data = self.data
|
||||
let error = self.error
|
||||
|
||||
return URLSessionDataTaskMock {
|
||||
completionHandler(data, nil, error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Immediately passes data and error to completion handler.
|
||||
///
|
||||
/// - Parameters:
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
//
|
||||
// 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 {
|
||||
func urlSession(
|
||||
_: URLSession,
|
||||
didReceive challenge: URLAuthenticationChallenge,
|
||||
completionHandler: (
|
||||
URLSession.AuthChallengeDisposition,
|
||||
URLCredential?
|
||||
) -> Void
|
||||
) {
|
||||
// For example, you may want to override this to accept some self-signed certs here.
|
||||
let methodIsServerTrust = challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust
|
||||
if methodIsServerTrust, Constants.selfSignedHosts.contains(challenge.protectionSpace.host) {
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
enum Constants {
|
||||
// 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"]
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
//
|
||||
// URLSessionConfiguration+Test.swift
|
||||
// MasKitTests
|
||||
//
|
||||
// Created by Ben Chatelain on 1/5/19.
|
||||
// Copyright © 2019 mas-cli. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Configuration for network requests initiated from tests.
|
||||
extension URLSessionConfiguration {
|
||||
/// Just like defaultSessionConfiguration, returns a
|
||||
/// newly created session configuration object, customised
|
||||
/// from the default to your requirements.
|
||||
class func testSessionConfiguration() -> URLSessionConfiguration {
|
||||
let config = `default`
|
||||
|
||||
// Eg we think 60s is too long a timeout time.
|
||||
config.timeoutIntervalForRequest = 20
|
||||
|
||||
// Some headers that are common to all reqeuests.
|
||||
// Eg my backend needs to be explicitly asked for JSON.
|
||||
config.httpAdditionalHeaders = ["MyResponseType": "JSON"]
|
||||
|
||||
// Eg we want to use pipelining.
|
||||
config.httpShouldUsePipelining = true
|
||||
|
||||
return config
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
//
|
||||
// URLSessionDataTaskMock .swift
|
||||
// MasKitTests
|
||||
//
|
||||
// Created by Ben Chatelain on 1/5/19.
|
||||
// Copyright © 2019 mas-cli. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
// Partial mock subclassing the original class
|
||||
class URLSessionDataTaskMock: URLSessionDataTask {
|
||||
private let closure: () -> Void
|
||||
|
||||
init(closure: @escaping () -> Void) {
|
||||
self.closure = closure
|
||||
}
|
||||
|
||||
// We override the 'resume' method and simply call our closure
|
||||
// instead of actually resuming any task.
|
||||
override func resume() {
|
||||
closure()
|
||||
}
|
||||
}
|
|
@ -8,19 +8,7 @@
|
|||
|
||||
// https://medium.com/@merowing_/stop-weak-strong-dance-in-swift-3aec6d3563d4
|
||||
|
||||
func strongify<Context: AnyObject, Arguments>(
|
||||
_ context: Context?,
|
||||
closure: @escaping (Context, Arguments) -> Void
|
||||
) -> (Arguments) -> Void {
|
||||
let strongified = { [weak context] (arguments: Arguments) in
|
||||
guard let strongContext = context else { return }
|
||||
closure(strongContext, arguments)
|
||||
}
|
||||
|
||||
return strongified
|
||||
}
|
||||
|
||||
func strongify<Context: AnyObject>(_ context: Context?, closure: @escaping (Context) -> Void) {
|
||||
func strongify<Context: AnyObject>(_ context: Context?, closure: (Context) -> Void) {
|
||||
guard let strongContext = context else { return }
|
||||
closure(strongContext)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue