<commit_before>//
//  CustomURLProtocol.swift
//  SessionStubber
//
//  Created by <NAME> on 18/09/2016.
//  Copyright © 2016 HarmeetSingh. All rights reserved.
//

import Foundation

open class SessionStubber: URLSession {
    
    // MARK: Properties
    
    fileprivate var mockDataTaskResponse: DataTaskResponse?
    fileprivate var mockUploadTaskResponse: UploadTaskResponse?
    fileprivate var mockDownloadTaskResponse: DownloadTaskResponse?
    
    fileprivate var dataTaskResponse: ((Data?, URLResponse?, NSError?) -> Void)?

    // MARK: Override functions
    
    override open class var shared: SessionStubber {
        return SessionStubber()
    }
}

// MARK: Data Tasks

extension SessionStubber {
    
    override open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {

        if let mockDataTaskResponse = mockDataTaskResponse {
            return DataTask(response: mockDataTaskResponse, completionHandler: completionHandler)
        }
        
        return super.dataTask(with: request, completionHandler: completionHandler)
    }
    
}

// MARK: Data Task Stubs

extension SessionStubber {
    
    public func stubDataTask(_withData data: Data?, response: URLResponse?, error: NSError?) {
        
        mockDataTaskResponse = (data: data, response: response, error: error)
    }

    public func stubDataTask(_withData data: Data?) {
        
        mockDataTaskResponse = (data: data, response: nil, error: nil)
    }<commit_msg>origin/feature/ExampleApp: updated to override data task with url function<commit_after>//
//  CustomURLProtocol.swift
//  SessionStubber
//
//  Created by <NAME> on 18/09/2016.
//  Copyright © 2016 HarmeetSingh. All rights reserved.
//

import Foundation

open class SessionStubber: URLSession {
    
    // MARK: Properties
    
    fileprivate var mockDataTaskResponse: DataTaskResponse?
    fileprivate var mockUploadTaskResponse: UploadTaskResponse?
    fileprivate var mockDownloadTaskResponse: DownloadTaskResponse?
    
    fileprivate var dataTaskResponse: ((Data?, URLResponse?, NSError?) -> Void)?

    // MARK: Override functions
    
    override open class var shared: SessionStubber {
        return SessionStubber()
    }
}

// MARK: Data Tasks

extension SessionStubber {
    
    override open func dataTask(with request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {

        if let mockDataTaskResponse = mockDataTaskResponse {
            
            return DataTask(response: mockDataTaskResponse, completionHandler: completionHandler)
        }
        
        return super.dataTask(with: request, completionHandler: completionHandler)
    }
    
    override open func dataTask(with url: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) -> URLSessionDataTask {
        
        if let mockDataTaskResponse = mockDataTaskResponse {
            
            return DataTask(response: mockDataTaskResponse, completionHandler: completionHandler)
        }
        
        return super.dataTask(with: url, completionHandler: completionHandler)
    }
}

// MARK: Data Task Stubs

extension SessionStubber {
    
    public func stubDataTask(_withData data: Data?, response: URLResponse?, error: NSError?) {
        
        mockDataTaskResponse = (data: data, response: response, error: error)
    }

    public func stubDataTask(_withData data: Data?) {
        
        mockDataTaskResponse = (data: data, response: nil, error: nil)
    }
{'fraction_non_alphanumeric': 0.08542014468558709, 'fraction_numerical': 0.00667779632721202, 'mean_word_length': 3.080590238365494, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3398505', 'n_tokens_mistral': 988, 'n_tokens_neox': 947, 'n_words': 273}