Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Threema/ThreemaFrameworkTests/   (Threema ©)  Datei vom 26.9.2026 mit Größe 1 kB image not shown  

Impressum URLProtocolMocks.swift   Sprache: JAVA

 

import Foundation
@testable import ThreemaFramework

final class URLProtocolMock: URLProtocol {
    
    enum URLProtocolMockError: Error {
        case noURL
        case noMockResponse
    }
    
    typealias MockURLResponse = (
        error: Error?,
        data: Data?,
        response: HTTPURLResponse?,
        challenges: [URLAuthenticationChallenge]?
    )
    typealias RequestCompletedCallback = () -> Void
    
    static var requests = [URL: URLRequest]()
    static var mockResponses = [URL: (MockURLResponse, RequestCompletedCallback?)]()
    
    // MARK: - URLProtocol
    
    override class func canInit(with request: URLRequest) -> Bool {
        true
    }

    override class func canonicalRequest(for request: URLRequest) -> URLRequest {
        request
    }

    override func startLoading() {
        guard let url = request.url else {
            client?.urlProtocol(self, didFailWithError: URLProtocolMockError.noURL)
            return
        }
        
        URLProtocolMock.requests[url] = request
        
        guard let ((error, data, response, challenges), completion) = URLProtocolMock.mockResponses[url] else {
            client?.urlProtocol(self, didFailWithError: URLProtocolMockError.noMockResponse)
            return
        }

        if let errorStrong = error {
            client?.urlProtocol(self, didFailWithError: errorStrong)
        }
        
        if let responseStrong = response {
            client?.urlProtocol(self, didReceive: responseStrong, cacheStoragePolicy: .notAllowed)
        }
        
        if let dataStrong = data {
            client?.urlProtocol(self, didLoad: dataStrong)
        }

        if let challenges {
            for challenge in challenges {
                client?.urlProtocol(self, didReceive: challenge)
            }
        }

        client?.urlProtocolDidFinishLoading(self)
        completion?()
    }

    override func stopLoading() {
        // Required by the superclass.
    }
}

Messung
CHZG
82928987

¤ Dauer der Verarbeitung: 0.3 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*© Formatika GbR, Deutschland