<commit_before>//
//  PCKClientTests.swift
//  PocketCastsKitTests
//
//  Created by <NAME> on 18.08.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import XCTest

enum TestErrors: Error {
    case injectedError
}

@testable import PocketCastsKit
class PCKClientTests: XCTestCase {
    private var mock: NetworkManagerMock!
    private var rest: RestClient!
    private var api: PCKClient!
    private var expec: XCTestExpectation!
    private var restClient: RestClient!
    
    override func setUp() {
        expec = expectation(description: "API Client")
    }
    
    func testAuthenticateCheckProperties() {
        mock = NetworkManagerMock()
        mock.injectTestCode { (url, options, method, handler) in
            XCTAssertEqual(url, URL(string: "http://localhost/users/sign_in")!)
            XCTAssertEqual(options.count, 1)
            XCTAssertEqual(method, .POST)
            
            handler(Result.success(Data()))
        }
        
        rest = try! RestClient(baseURLString: "http://localhost", manager: mock)
        api = PCKClient(client: rest)
        
        api.authenticate(username: "user", password: "<PASSWORD>") { (result) in
            self.expec.fulfill()
        }
        wait(for: [expec], timeout: 2)
    }
    
    func testAuthenticateResponseError() {
        mock = NetworkManagerMock()
        mock.injectTestCode { (url, options, method, handler) in
            handler(Result.error(TestErrors.injectedError))
        }
        
        rest = try! RestClient(baseURLString: "http://localhost", manager: mock)
        api = PCKClient(client: rest)
        
        api.authenticate(username: "user", password: "<PASSWORD>") { (result) in
            switch result {
            case .success(_):
                XCTFail()
            default:
                break
            }
            self.expec.fulfill()
        }
        wait(for: [expec], timeout: 2)
    }
}<commit_msg>Updated PCKClient test cases<commit_after>//
//  PCKClientTests.swift
//  PocketCastsKitTests
//
//  Created by <NAME> on 18.08.17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import XCTest

enum TestErrors: Error {
    case injectedError
}

@testable import PocketCastsKit
class PCKClientTests: PCKTestCase {
    var rest: RestClient!
    var api: PCKClient!
    
    override func setUp() {
        super.setUp()
        rest = try! RestClient(baseURLString: baseURLString, manager: manager)
        api = PCKClient(client: rest)
    }
}

// MARK: - Authentication Testing
extension PCKClientTests {
    
    func testAuthenticateCheckProperties() {
        api.authenticate(username: "user", password: "<PASSWORD>") { (result) in
            self.expec.fulfill()
        }
        let data = "[user]email=user&[user]password=<PASSWORD>"
            .addingPercentEncoding(withAllowedCharacters: .urlEncoded)!
            .data(using: .utf8)!
        let request = getRequest()!
        
        XCTAssertEqual(request.url, URL(string: "http://localhost/users/sign_in")!)
        XCTAssertEqual(request.httpBody, data)
        XCTAssertEqual(request.httpMethod, MethodType.POST.rawValue)
        
        wait()
    }
    
    func testAuthenticateWithErrorResponse() {
        mock = URLSessionMock(data: nil, response: nil, error: TestErrors.injectedError)
        manager = NetworkManager(session: mock)
        rest = try! RestClient(baseURLString: "http://localhost", manager: manager)
        api = PCKClient(client: rest)
        
        api.authenticate(username: "user", password: "<PASSWORD>") { (result) in
            switch result {
            case .success(_):
                XCTFail()
            default:
                break
            }
            self.expec.fulfill()
        }
        wait()
    }
}
{'fraction_non_alphanumeric': 0.1112873380914618, 'fraction_numerical': 0.006344171292624901, 'mean_word_length': 2.204064352243861, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '4732565', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1091, 'n_words': 249}