<commit_before>//
//  MockHTTPServer.swift
//  Punos
//
//  Created by <NAME> on 9.2.16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation


public typealias MockResponseMatcher = (request: HTTPRequest) -> Bool


/// Data for a mocked HTTP server response.
///
public struct MockResponse {
    
    /// The HTTP status code
    public let statusCode: Int?
    
    /// The body data
    public let data: NSData?
    
    /// The HTTP headers
    public let headers: [String:String]?
}


private struct MockResponseConfiguration {
    let response: MockResponse
    let matcher: MockResponseMatcher?
    let onlyOnce: Bool
    let delay: NSTimeInterval
}


private class OurSwifterServer: HttpServerIO {
    
    var responder: (HttpRequest -> HttpResponse)?
    
    class func defaultResponse() -> HttpResponse {
        return HttpResponse(200, "OK", nil, { writer in writer.write([])})
    }
    
    override func dispatch(method: String, path: String) -> ([String : String], HttpRequest -> HttpResponse) {
        if let responder = responder {
            return ([:], responder)
        }
        return ([:], { req in
            return OurSwifterServer.defaultResponse()
        })
    }
}


/// A web server that runs on `localhost` and can be told how to respond
/// to incoming requests. Meant for automated tests.
///
public class MockHTTPServer {
    
    private let server = OurSwifterServer()
    
    public init() {
        server.responder = respondToRequest
    }
    
    // ------------------------------------
    // MARK: Starting and stopping
    
    /// Start the server on the given `port` (`8080` by default.)
    ///
    public func start(port: in_port_t? = nil) {
        do {
            let effectivePort = port ?? 8080
            try server.start(effectivePort)
            self.port = effectivePort
            isRunning = true
            debugPrint("\(self.dynamicType) started at port \(effectivePort)")
        } catch let error {
            fatalError("The mock server failed to start on port \(port). Error: \(error)")
        }
    }
    
    /// Stop the server.
    ///
    public func stop() {
        server.stop()
        port = 0
        isRunning = false
        debugPrint("\(self.dynamicType) stopped.")
    }
    
    /// Whether the server is currently running.
    ///
    public private(set) var isRunning: Bool = false
    
    /// The port the server is running on, or 0 if the server
    /// is not running.
    ///
    public private(set) var port: in_port_t = 0<commit_msg>Print server start/stop in release builds<commit_after>//
//  MockHTTPServer.swift
//  Punos
//
//  Created by <NAME> on 9.2.16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation


public typealias MockResponseMatcher = (request: HTTPRequest) -> Bool


/// Data for a mocked HTTP server response.
///
public struct MockResponse {
    
    /// The HTTP status code
    public let statusCode: Int?
    
    /// The body data
    public let data: NSData?
    
    /// The HTTP headers
    public let headers: [String:String]?
}


private struct MockResponseConfiguration {
    let response: MockResponse
    let matcher: MockResponseMatcher?
    let onlyOnce: Bool
    let delay: NSTimeInterval
}


private class OurSwifterServer: HttpServerIO {
    
    var responder: (HttpRequest -> HttpResponse)?
    
    class func defaultResponse() -> HttpResponse {
        return HttpResponse(200, "OK", nil, { writer in writer.write([])})
    }
    
    override func dispatch(method: String, path: String) -> ([String : String], HttpRequest -> HttpResponse) {
        if let responder = responder {
            return ([:], responder)
        }
        return ([:], { req in
            return OurSwifterServer.defaultResponse()
        })
    }
}


/// A web server that runs on `localhost` and can be told how to respond
/// to incoming requests. Meant for automated tests.
///
public class MockHTTPServer {
    
    private let server = OurSwifterServer()
    
    public init() {
        server.responder = respondToRequest
    }
    
    // ------------------------------------
    // MARK: Starting and stopping
    
    /// Start the server on the given `port` (`8080` by default.)
    ///
    public func start(port: in_port_t? = nil) {
        do {
            let effectivePort = port ?? 8080
            try server.start(effectivePort)
            self.port = effectivePort
            isRunning = true
            print("\(self.dynamicType) started at port \(effectivePort)")
        } catch let error {
            fatalError("The mock server failed to start on port \(port). Error: \(error)")
        }
    }
    
    /// Stop the server.
    ///
    public func stop() {
        server.stop()
        port = 0
        isRunning = false
        print("\(self.dynamicType) stopped.")
    }
    
    /// Whether the server is currently running.
    ///
    public private(set) var isRunning: Bool = false
    
    /// The port the server is running on, or 0 if the server
    /// is not running.
    ///
    public private(set) var port: in_port_t = 0
{'fraction_non_alphanumeric': 0.11901051811453058, 'fraction_numerical': 0.008570315543435918, 'mean_word_length': 1.9494543365881678, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '941236', 'n_tokens_mistral': 1444, 'n_tokens_neox': 1385, 'n_words': 478}