<commit_before>//
//  ServicePassthroughDelegateTests.swift
//  ELWebService
//
//  Created by <NAME> on 2/9/16.
//  Copyright © 2016 WalmartLabs. All rights reserved.
//

import XCTest
@testable import ELWebService

class ServicePassthroughDelegateTests: XCTestCase {
    func test_passthroughDelegate_receivesEventsWhenResponseIsValid() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        passthroughSpy.requestSentExpectation = expectationWithDescription("requestSent passthrough called")
        passthroughSpy.responseReceivedExpectation = expectationWithDescription("responseReceived passthrough called")
        passthroughSpy.updateUIBeginExpectation = expectationWithDescription("updateUIBegin passthrough called")
        passthroughSpy.updateUIEndExpectation = expectationWithDescription("updateUIEnd passthrough called")
        let service = WebService(baseURLString: "http://httpbin.org/", passthroughDelegate: passthroughSpy)
        
        let _ = service
            .GET("/get")
            .response { data, response in
                return .Value(true)
            }
            .updateUI { value in
            }
            .resume()
        
        
        waitForExpectationsWithTimeout(2, handler: nil)
    }
    
    func test_passthroughDelegate_receivesEventsWhenResponseHandlerReturnsEmpty() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        passthroughSpy.requestSentExpectation = expectationWithDescription("requestSent passthrough called")
        passthroughSpy.responseReceivedExpectation = expectationWithDescription("responseReceived passthrough called")
        passthroughSpy.updateUIBeginExpectation = expectationWithDescription("updateUIBegin passthrough called")
        passthroughSpy.updateUIEndExpectation = expectationWithDescription("updateUIEnd passthrough called")
        let service = WebService(baseURLString: "http://httpbin.org/", passthroughDelegate: passthroughSpy)
        
        let _ = service
            .GET("/get")
            .response { data, response in
                return .Empty
            }
            .updateUI { value in
            }
            .resume()
        
        
        waitForExpectationsWithTimeout(2, handler: nil)
    }
    
    func test_passthroughDelegate_receivesFailureEventWhenResponseHandlerReturnsFailure() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        passthroughSpy.serviceResultFailureExpecation = expectationWithDescription("serviceResultFailure passthrough called")
        let service = WebService(baseURLString: "http://httpbin.org/", passthroughDelegate: passthroughSpy)
        
        let _ = service
            .GET("/get")
            .response { data, response in
                return .Failure(MockError.SomethingWentHorriblyWrong)
            }
            .resume()
        
        waitForExpectationsWithTimeout(2, handler: nil)
    }
}

// MARK - Mocks

enum MockError: ErrorType {
    case SomethingWentHorriblyWrong
}

class ServicePassthroughDelegateSpy: ServicePassthroughDelegate {
    var requestSentExpectation: XCTestExpectation?
    var responseReceivedExpectation: XCTestExpectation?
    var updateUIBeginExpectation: XCTestExpectation?
    var updateUIEndExpectation: XCTestExpectation?
    var serviceResultFailureExpecation: XCTestExpectation?
    
    func requestSent(request: NSURLRequest) {
        requestSentExpectation?.fulfill()
    }
    
    func responseReceived(response: NSURLResponse?, data: NSData?, request: NSURLRequest?, error: NSError?) {
        responseReceivedExpectation?.fulfill()
    }
    
    func updateUIBegin(response: NSURLResponse?) {
        updateUIBeginExpectation?.fulfill()
    }<commit_msg>add unit test for modifiedRequest()<commit_after>//
//  ServicePassthroughDelegateTests.swift
//  ELWebService
//
//  Created by <NAME> on 2/9/16.
//  Copyright © 2016 WalmartLabs. All rights reserved.
//

import XCTest
@testable import ELWebService

class ServicePassthroughDelegateTests: XCTestCase {
    func test_passthroughDelegate_receivesEventsWhenResponseIsValid() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        passthroughSpy.requestSentExpectation = expectationWithDescription("requestSent passthrough called")
        passthroughSpy.responseReceivedExpectation = expectationWithDescription("responseReceived passthrough called")
        passthroughSpy.updateUIBeginExpectation = expectationWithDescription("updateUIBegin passthrough called")
        passthroughSpy.updateUIEndExpectation = expectationWithDescription("updateUIEnd passthrough called")
        let service = WebService(baseURLString: "http://httpbin.org/", passthroughDelegate: passthroughSpy)
        
        let _ = service
            .GET("/get")
            .response { data, response in
                return .Value(true)
            }
            .updateUI { value in
            }
            .resume()
        
        
        waitForExpectationsWithTimeout(2, handler: nil)
    }
    
    func test_passthroughDelegate_receivesEventsWhenResponseHandlerReturnsEmpty() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        passthroughSpy.requestSentExpectation = expectationWithDescription("requestSent passthrough called")
        passthroughSpy.responseReceivedExpectation = expectationWithDescription("responseReceived passthrough called")
        passthroughSpy.updateUIBeginExpectation = expectationWithDescription("updateUIBegin passthrough called")
        passthroughSpy.updateUIEndExpectation = expectationWithDescription("updateUIEnd passthrough called")
        let service = WebService(baseURLString: "http://httpbin.org/", passthroughDelegate: passthroughSpy)
        
        let _ = service
            .GET("/get")
            .response { data, response in
                return .Empty
            }
            .updateUI { value in
            }
            .resume()
        
        
        waitForExpectationsWithTimeout(2, handler: nil)
    }
    
    func test_passthroughDelegate_receivesFailureEventWhenResponseHandlerReturnsFailure() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        passthroughSpy.serviceResultFailureExpecation = expectationWithDescription("serviceResultFailure passthrough called")
        let service = WebService(baseURLString: "http://httpbin.org/", passthroughDelegate: passthroughSpy)
        
        let _ = service
            .GET("/get")
            .response { data, response in
                return .Failure(MockError.SomethingWentHorriblyWrong)
            }
            .resume()
        
        waitForExpectationsWithTimeout(2, handler: nil)
    }
    
    func test_passthroughDelegate_modifiedRequestReturnsNil() {
        let passthroughSpy = ServicePassthroughDelegateSpy()
        
        let result = passthroughSpy.modifiedRequest(NSURLRequest())
        
        XCTAssertNil(result)
    }
}

// MARK - Mocks

enum MockError: ErrorType {
    case SomethingWentHorriblyWrong
}

class ServicePassthroughDelegateSpy: ServicePassthroughDelegate {
    var requestSentExpectation: XCTestExpectation?
    var responseReceivedExpectation: XCTestExpectation?
    var updateUIBeginExpectation: XCTestExpectation?
    var updateUIEndExpectation: XCTestExpectation?
    var serviceResultFailureExpecation: XCTestExpectation?
    
    func requestSent(request: NSURLRequest) {
        requestSentExpectation?.fulfill()
    }
    
    func responseReceived(response: NSURLResponse?, data: NSData?, request: NSURLRequest?, error: NSError?) {
        responseReceivedExpectation?.fulfill()
    }
    
    func updateUIBegin(response: NSURLResponse?) {
        updateUIBeginExpectation?.fulfill()
    }
{'fraction_non_alphanumeric': 0.07140090380890897, 'fraction_numerical': 0.002840542285345384, 'mean_word_length': 3.0897571277719114, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2977151', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2102, 'n_words': 373}