<commit_before>/**
 * Copyright IBM Corporation 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import XCTest
import Kitura

@testable import KituraNet

import Foundation
import Dispatch

enum SSLOption {
    case both
    case httpOnly
    case httpsOnly
}

class KituraTest: XCTestCase {

    static let httpPort = 8080
    static let httpsPort = 8443

    static private(set) var httpServer: HTTPServer? = nil
    static private(set) var httpsServer: HTTPServer? = nil

    private(set) var port = -1
    private(set) var useSSL = false

    static let sslConfig: SSLConfig = {
        let path = #file
        let sslConfigDir: String
        if let range = path.range(of: "/", options: .backwards) {
            sslConfigDir = path.substring(to: range.lowerBound) + "/SSLConfig/"
        } else {
            sslConfigDir = "./SSLConfig/"
        }

        #if os(Linux)
            let certificatePath = sslConfigDir + "certificate.pem"
            let keyPath = sslConfigDir + "key.pem"
            return SSLConfig(withCACertificateDirectory: nil, usingCertificateFile: certificatePath,
                             withKeyFile: keyPath, usingSelfSignedCerts: true)
        #else
            let chainFilePath = sslConfigDir + "certificateChain.pfx"
            return SSLConfig(withChainFilePath: chainFilePath, withPassword: "<PASSWORD>",
                             usingSelfSignedCerts: true)
        #endif
    }()

    override func setUp() {
        super.setUp()
        PrintLogger.use(colored: true)
    }

    func performServerTest(_ router: ServerDelegate, sslOption: SSLOption = SSLOption.both,
                           line: Int = #line, asyncTasks: @escaping (XCTestExpectation) -> Void...) {
        if sslOption != SSLOption.httpsOnly {
            self.port = KituraTest.httpPort
            self.useSSL = false
            doPerformServerTest(router: router, line: line, asyncTasks: asyncTasks)
        }

        if sslOption != SSLOption.httpOnly {
            self.port = KituraTest.httpsPort
            self.useSSL = true
            doPerformServerTest(router: router, line: line, asyncTasks: asyncTasks)
        }
    }

    func doPerformServerTest(router: ServerDelegate, line: Int, asyncTasks: [(XCTestExpectation) -> Void]) {

        guard startServer(router: router) else {
            XCTFail("Error starting server on port \(port). useSSL:\(useSSL)")<commit_msg>#996 Initialize Log.logger once across all tests (#998)<commit_after>/**
 * Copyright IBM Corporation 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import XCTest
import Kitura

@testable import KituraNet

import Foundation
import Dispatch

enum SSLOption {
    case both
    case httpOnly
    case httpsOnly
}

class KituraTest: XCTestCase {

    static let httpPort = 8080
    static let httpsPort = 8443

    static private(set) var httpServer: HTTPServer? = nil
    static private(set) var httpsServer: HTTPServer? = nil

    private(set) var port = -1
    private(set) var useSSL = false

    static let sslConfig: SSLConfig = {
        let path = #file
        let sslConfigDir: String
        if let range = path.range(of: "/", options: .backwards) {
            sslConfigDir = path.substring(to: range.lowerBound) + "/SSLConfig/"
        } else {
            sslConfigDir = "./SSLConfig/"
        }

        #if os(Linux)
            let certificatePath = sslConfigDir + "certificate.pem"
            let keyPath = sslConfigDir + "key.pem"
            return SSLConfig(withCACertificateDirectory: nil, usingCertificateFile: certificatePath,
                             withKeyFile: keyPath, usingSelfSignedCerts: true)
        #else
            let chainFilePath = sslConfigDir + "certificateChain.pfx"
            return SSLConfig(withChainFilePath: chainFilePath, withPassword: "<PASSWORD>",
                             usingSelfSignedCerts: true)
        #endif
    }()

    private static let initOnce: () = {
        PrintLogger.use(colored: true)
    }()

    override func setUp() {
        super.setUp()
        KituraTest.initOnce
    }

    func performServerTest(_ router: ServerDelegate, sslOption: SSLOption = SSLOption.both,
                           line: Int = #line, asyncTasks: @escaping (XCTestExpectation) -> Void...) {
        if sslOption != SSLOption.httpsOnly {
            self.port = KituraTest.httpPort
            self.useSSL = false
            doPerformServerTest(router: router, line: line, asyncTasks: asyncTasks)
        }

        if sslOption != SSLOption.httpOnly {
            self.port = KituraTest.httpsPort
            self.useSSL = true
            doPerformServerTest(router: router, line: line, asyncTasks: asyncTasks)
        }
    }

    func doPerformServerTest(router: ServerDelegate, line: Int, asyncTasks: [(XCTestExpectation) -> Void]) {

        guard startServer(router: router) else {
            XCTFail("Error starting server on port \(port). useSSL:\(useSSL)")
{'fraction_non_alphanumeric': 0.08574778094121588, 'fraction_numerical': 0.0066990453860324905, 'mean_word_length': 2.5337278106508876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6065129', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1600, 'n_words': 517}