<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 Dispatch
import Foundation

import XCTest

@testable import KituraNet

class MonitoringTests: XCTestCase {
    
    static var allTests : [(String, (MonitoringTests) -> () throws -> Void)] {
        return [
            ("testStartedFinishedHTTP", testStartedFinishedHTTP)
        ]
    }
    
    override func setUp() {
        doSetUp()
    }
    
    override func tearDown() {
        doTearDown()
    }
    
    func testStartedFinishedHTTP() {
        let startedExpectation = self.expectation(description: "started")
        let finishedExpectation = self.expectation(description: "finished")

        Monitor.delegate = TestMonitor(startedExpectation: startedExpectation,
                                       finishedExpectation: finishedExpectation)
        
        
        let server = HTTP.createServer()
        server.delegate = TestServerDelegate()
        
        server.started {
            DispatchQueue.global().async {
                self.performRequest("get", path: "/plover", callback: { response in
                    XCTAssertNotNil(response, "Received a nil response")
                    XCTAssertEqual(response!.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(response!.statusCode)")
                })
            }
        }
        
        server.failed { error in
            XCTFail("Server failed to start: \(error)")
        }
        
        server.listen(port: 8090)
        
        self.waitForExpectations(timeout: 10) { error in
            server.stop()
            XCTAssertNil(error);
            Monitor.delegate = nil
        }
    }
    
    private class TestMonitor: ServerMonitor {
        private let startedExpectation: XCTestExpectation
        private let finishedExpectation: XCTestExpectation
        private var startedTime = Date()
        
        init(startedExpectation: XCTestExpectation, finishedExpectation: XCTestExpectation) {
            self.startedExpectation = startedExpectation
            self.finishedExpectation = finishedExpectation
        }
        
        public func started(request: ServerRequest, response: ServerResponse) {
            startedTime = Date()
            startedExpectation.fulfill()
        }
        
        public func finished(request: ServerRequest?, response: ServerResponse) {
            let now = Date()
            if now >= startedTime {
                finishedExpectation.fulfill()
            }
            else {
                XCTFail("Monitoring finished called before Monitoring started")
            }
        }
    }
    
    private class TestServerDelegate : ServerDelegate {
        
        func handle(request: ServerRequest, response: ServerResponse) {
            let payloadData = contentTypesString.data(using: .utf8)!
            do {
                response.statusCode = .OK
                response.headers["Content-Length"] = ["\(payloadData.count)"]
                try response.write(from: payloadData)
                try response.end()
            }
            catch {
                print("Error writing response.")
            }
        }
    }
}
<commit_msg>IBM-Swift/Kitura#549 Update after review comment.<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 Dispatch
import Foundation

import XCTest

@testable import KituraNet

class MonitoringTests: XCTestCase {
    
    static var allTests : [(String, (MonitoringTests) -> () throws -> Void)] {
        return [
            ("testStartedFinishedHTTP", testStartedFinishedHTTP)
        ]
    }
    
    override func setUp() {
        doSetUp()
    }
    
    override func tearDown() {
        doTearDown()
    }
    
    func testStartedFinishedHTTP() {
        let startedExpectation = self.expectation(description: "started")
        let finishedExpectation = self.expectation(description: "finished")

        Monitor.delegate = TestMonitor(startedExpectation: startedExpectation,
                                       finishedExpectation: finishedExpectation)
        
        
        let server = HTTP.createServer()
        server.delegate = TestServerDelegate()
        
        server.started {
            DispatchQueue.global().async {
                self.performRequest("get", path: "/plover", callback: { response in
                    XCTAssertNotNil(response, "Received a nil response")
                    XCTAssertEqual(response!.statusCode, HTTPStatusCode.OK, "Status code wasn't .Ok was \(response!.statusCode)")
                })
            }
        }
        
        server.failed { error in
            XCTFail("Server failed to start: \(error)")
        }
        
        server.listen(port: 8090)
        
        self.waitForExpectations(timeout: 10) { error in
            server.stop()
            XCTAssertNil(error);
            Monitor.delegate = nil
        }
    }
    
    private class TestMonitor: ServerMonitor {
        private let startedExpectation: XCTestExpectation
        private let finishedExpectation: XCTestExpectation
        private var startedTime = Date()
        
        init(startedExpectation: XCTestExpectation, finishedExpectation: XCTestExpectation) {
            self.startedExpectation = startedExpectation
            self.finishedExpectation = finishedExpectation
        }
        
        public func started(request: ServerRequest, response: ServerResponse) {
            startedTime = Date()
            startedExpectation.fulfill()
        }
        
        public func finished(request: ServerRequest?, response: ServerResponse) {
            let now = Date()
            if now >= startedTime {
                finishedExpectation.fulfill()
            }
            else {
                XCTFail("Monitoring finished called before Monitoring started")
            }
        }
    }
    
    private class TestServerDelegate : ServerDelegate {
        
        func handle(request: ServerRequest, response: ServerResponse) {
            let payloadData = contentTypesString.data(using: .utf8)!
            do {
                response.statusCode = .OK
                response.headers["Content-Length"] = ["\(payloadData.count)"]
                try response.write(from: payloadData)
                try response.end()
            }
            catch {
                XCTFail("Error writing response.")
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.07974481658692185, 'fraction_numerical': 0.0043859649122807015, 'mean_word_length': 1.981378763866878, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2481887', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1793, 'n_words': 508}