 
 // MARK: - MockSession
 
+/// Implements the `Session` protocol and provides an API for adding mocked responses as stubs.
 public class MockSession: Session {
     typealias Stub = (MockableDataTaskResult, (URLRequestEncodable) -> (Bool))
     lazy var stubs = [Stub]()

    }
}

// MARK: - Mocking Errors

extension NSError: MockableDataTaskResult {
    public func dataTaskResult(request: URLRequestEncodable) -> (NSData?, NSURLResponse?, NSError?) {
        return (nil, nil, self)
    }

    
    // MARK: Stubbing API
    
    /**
     Add a stub that will be used as the response of a request.
    
     - parameter response: A mockable data task result that provides the mocked 
     response value.
     - parameter requestMatcher: A matcher closure that determines if the mocked 
     response is used as a response stub for a given request.
    */
    public func addStub(response: MockableDataTaskResult, requestMatcher: (URLRequestEncodable) -> (Bool)) {
        stubs.append((response, requestMatcher))
    }

    /**
     Add a stub that will be used as the response of any request. 
     The added stub will match any request.
     
     - parameter response: A mockable data task result that provides the mocked
     response value.
    */
    public func addStub(response: MockableDataTaskResult) {
        stubs.append((response, { _ in return true}))
    }
    
    /**
     Retrieve a response stub for a given request.
     
     - parameter request: The request that needs a stubbed response.
     */
    public func stubbedResponse(request request: URLRequestEncodable) -> (NSData?, NSURLResponse?, NSError?) {
        for (response, matcher) in stubs.reverse() where matcher(request) {
            return response.dataTaskResult(request)
