2019-05-05 22:41:20 +02:00
|
|
|
//
|
|
|
|
// TestTransport.swift
|
|
|
|
// AccountTests
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 5/4/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSWeb
|
2019-10-16 00:37:25 +02:00
|
|
|
import XCTest
|
2019-05-05 22:41:20 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
protocol TestTransportMockResponseProviding: class {
|
|
|
|
func mockResponseFileUrl(for components: URLComponents) -> URL?
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:41:20 +02:00
|
|
|
final class TestTransport: Transport {
|
|
|
|
|
|
|
|
enum TestTransportError: String, Error {
|
|
|
|
case invalidState = "The test wasn't set up correctly."
|
|
|
|
}
|
|
|
|
|
|
|
|
var testFiles = [String: String]()
|
2019-09-30 01:45:13 +02:00
|
|
|
var testStatusCodes = [String: Int]()
|
2019-05-05 22:41:20 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
weak var mockResponseFileUrlProvider: TestTransportMockResponseProviding?
|
2019-10-03 13:23:49 +02:00
|
|
|
|
2019-09-30 01:45:13 +02:00
|
|
|
private func httpResponse(for request: URLRequest, statusCode: Int = 200) -> HTTPURLResponse {
|
|
|
|
guard let url = request.url else {
|
|
|
|
fatalError("Attempting to mock a http response for a request without a URL \(request).")
|
|
|
|
}
|
|
|
|
return HTTPURLResponse(url: url, statusCode: statusCode, httpVersion: "HTTP/1.1", headerFields: nil)!
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:24:21 +01:00
|
|
|
func cancelAll() { }
|
|
|
|
|
2019-09-30 01:45:13 +02:00
|
|
|
func send(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
|
2019-05-05 22:41:20 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let url = request.url, let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
|
2019-10-03 13:23:49 +02:00
|
|
|
completion(.failure(TestTransportError.invalidState))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
let urlString = url.absoluteString
|
|
|
|
let response = httpResponse(for: request, statusCode: testStatusCodes[urlString] ?? 200)
|
|
|
|
let testFileURL: URL
|
2019-10-03 13:23:49 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
if let provider = mockResponseFileUrlProvider {
|
|
|
|
guard let providerUrl = provider.mockResponseFileUrl(for: components) else {
|
|
|
|
XCTFail("Test behaviour undefined. Mock provider failed to provide non-nil URL for \(components).")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
testFileURL = providerUrl
|
|
|
|
|
|
|
|
} else if let testKeyAndFileName = testFiles.first(where: { urlString.contains($0.key) }) {
|
|
|
|
testFileURL = Bundle(for: TestTransport.self).resourceURL!.appendingPathComponent(testKeyAndFileName.value)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// XCTFail("Missing mock response for: \(urlString)")
|
|
|
|
print("***\nWARNING: \(self) missing mock response for:\n\(urlString)\n***")
|
|
|
|
DispatchQueue.global(qos: .background).async {
|
|
|
|
completion(.success((response, nil)))
|
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
do {
|
|
|
|
let data = try Data(contentsOf: testFileURL)
|
2019-05-07 17:51:41 +02:00
|
|
|
DispatchQueue.global(qos: .background).async {
|
2019-09-30 01:45:13 +02:00
|
|
|
completion(.success((response, data)))
|
2019-05-07 17:51:41 +02:00
|
|
|
}
|
2019-10-18 23:21:02 +02:00
|
|
|
} catch {
|
|
|
|
XCTFail("Unable to read file at \(testFileURL) because \(error).")
|
2019-05-07 17:51:41 +02:00
|
|
|
DispatchQueue.global(qos: .background).async {
|
2019-10-18 23:21:02 +02:00
|
|
|
completion(.failure(error))
|
2019-05-07 17:51:41 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 01:45:13 +02:00
|
|
|
func send(request: URLRequest, method: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
|
|
|
fatalError("Unimplemented.")
|
2019-05-06 17:53:20 +02:00
|
|
|
}
|
|
|
|
|
2019-09-30 01:45:13 +02:00
|
|
|
func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
|
|
|
|
fatalError("Unimplemented.")
|
|
|
|
}
|
2019-05-05 22:41:20 +02:00
|
|
|
}
|