<commit_before>//
//  Created by <NAME> on 09/02/2017.
//

import XCTest
import Alamofire
@testable import BSWFoundation


/// Full-suite tests are courtesy of our good friends of HTTPBin

private struct HTTPBIN: Environment {
    fileprivate var baseURL: URL {
        return URL(string: "https://httpbin.org/")!
    }
}

private enum HTTPBINAPI: Endpoint {
    case ip
    case upload

    var path: String {
        switch self {
        case .upload:
            return "post"
        case .ip:
            return "ip"
        }
    }

    var method: BSWFoundation.HTTPMethod {
        switch self {
        case .upload:
            return .POST
        default:
            return .GET
        }
    }
}

class DroskyTests: XCTestCase {

    var sut: Drosky!

    override func setUp() {
        super.setUp()
        sut = Drosky(environment: HTTPBIN())
    }

    func testGET() {
        get(shouldCancel: false)
    }

    func testGETCancel() {
        get(shouldCancel: true)
    }

    func testUpload() {
        upload(shouldCancel: false)
    }

    func testUploadCancel() {
        upload(shouldCancel: true)
    }

    // MARK: - Private

    private func get(shouldCancel: Bool) {
        let exp = expectation(description: "ip")
        let getTask = sut.performAndValidateRequest(forEndpoint: HTTPBINAPI.ip)
        getTask.upon(.main) { result in
            guard shouldCancel else {
                exp.fulfill()
                return
            }

            if let nsError = result.error as NSError?, nsError.domain == NSURLErrorDomain, nsError.code == NSURLErrorCancelled {
                exp.fulfill()
            } else {
                XCTFail()
            }
        }
        if shouldCancel {
            getTask.cancel()
        }
        waitForExpectations(timeout: 10, handler: nil)
    }

    private func upload(shouldCancel: Bool) {
        guard let url = Bundle(for: type(of: self)).url(forResource: "cannavaro", withExtension: "jpg") else {
            XCTFail()
            return
        }

        let exp = expectation(description: "uploadTask")
        let uploadTask = sut.performMultipartUpload(
            forEndpoint: HTTPBINAPI.upload,
            multipartParams: [
                MultipartParameter(parameterKey: "key", parameterValue: .url(url), fileName: "cannavaro.jpg", mimeType: .imageJPEG)
                ]
        )

        uploadTask.upon(.main) { result in
            guard shouldCancel else {
                exp.fulfill()
                return
            }

            if let nsError = result.error as NSError?, nsError.domain == NSURLErrorDomain, nsError.code == -999 {
                exp.fulfill()
            } else {
                XCTFail()
            }
        }

        if shouldCancel {
            uploadTask.cancel()
        }

        waitForExpectations(timeout: 600, handler: nil)
    }
}<commit_msg>Use correct error code<commit_after>//
//  Created by <NAME> on 09/02/2017.
//

import XCTest
import Alamofire
@testable import BSWFoundation


/// Full-suite tests are courtesy of our good friends of HTTPBin

private struct HTTPBIN: Environment {
    fileprivate var baseURL: URL {
        return URL(string: "https://httpbin.org/")!
    }
}

private enum HTTPBINAPI: Endpoint {
    case ip
    case upload

    var path: String {
        switch self {
        case .upload:
            return "post"
        case .ip:
            return "ip"
        }
    }

    var method: BSWFoundation.HTTPMethod {
        switch self {
        case .upload:
            return .POST
        default:
            return .GET
        }
    }
}

class DroskyTests: XCTestCase {

    var sut: Drosky!

    override func setUp() {
        super.setUp()
        sut = Drosky(environment: HTTPBIN())
    }

    func testGET() {
        get(shouldCancel: false)
    }

    func testGETCancel() {
        get(shouldCancel: true)
    }

    func testUpload() {
        upload(shouldCancel: false)
    }

    func testUploadCancel() {
        upload(shouldCancel: true)
    }

    // MARK: - Private

    private func get(shouldCancel: Bool) {
        let exp = expectation(description: "ip")
        let getTask = sut.performAndValidateRequest(forEndpoint: HTTPBINAPI.ip)
        getTask.upon(.main) { result in
            guard shouldCancel else {
                exp.fulfill()
                return
            }

            if let nsError = result.error as NSError?, nsError.domain == NSURLErrorDomain, nsError.code == NSURLErrorCancelled {
                exp.fulfill()
            } else {
                XCTFail()
            }
        }
        if shouldCancel {
            getTask.cancel()
        }
        waitForExpectations(timeout: 10, handler: nil)
    }

    private func upload(shouldCancel: Bool) {
        guard let url = Bundle(for: type(of: self)).url(forResource: "cannavaro", withExtension: "jpg") else {
            XCTFail()
            return
        }

        let exp = expectation(description: "uploadTask")
        let uploadTask = sut.performMultipartUpload(
            forEndpoint: HTTPBINAPI.upload,
            multipartParams: [
                MultipartParameter(parameterKey: "key", parameterValue: .url(url), fileName: "cannavaro.jpg", mimeType: .imageJPEG)
                ]
        )

        uploadTask.upon(.main) { result in
            guard shouldCancel else {
                exp.fulfill()
                return
            }

            if let nsError = result.error as NSError?, nsError.domain == NSURLErrorDomain, nsError.code == NSURLErrorCancelled {
                exp.fulfill()
            } else {
                XCTFail()
            }
        }

        if shouldCancel {
            uploadTask.cancel()
        }

        waitForExpectations(timeout: 600, handler: nil)
    }
}
{'fraction_non_alphanumeric': 0.0936426116838488, 'fraction_numerical': 0.004982817869415807, 'mean_word_length': 1.6447069513857338, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5604261', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1605, 'n_words': 403}