<commit_before>//
//  ServerTests.swift
//  Edge
//
//  Created by <NAME> on 10/30/16.
//
//

import Foundation
import XCTest
@testable import HTTP

class ServerTests: XCTestCase {
    
    func testServer() {
        let requestExpectation = expectation(description: "Did not receive a request.")
        let json = ["message": "Message to server!"]
        let jsonResponse = ["message": "Message received!"]

        func handleRequest(request: Request) -> Response {
            requestExpectation.fulfill()
            let body = try! JSONSerialization.jsonObject(with: Data(request.body)) as! [String:String]
            XCTAssert(body == json, "Received body \(body) != json \(json)")
            XCTAssert(request.method == .post, "Incorrect method: \(request.method)")
            return try! Response(json: jsonResponse)
        }
        
        let server = HTTP.Server()
        server.listen(host: "0.0.0.0", port: 3000).startWithNext { client in
            
            let requestStream = client.read()
            requestStream.map(handleRequest).onNext{ response in
                let writeStream = client.write(response)
                writeStream.onFailed { err in
                    XCTFail(String(describing: err))
                }
                writeStream.start()
            }
            
            requestStream.onFailed { clientError in
                XCTFail("ClientError: \(clientError)")
            }
            
            requestStream.onCompleted {

            }
            
            requestStream.start()
        }
        
        let session = URLSession.shared

        let urlString = "http://localhost:3000"
        let url = URL(string: urlString)!
        let responseExpectation = expectation(description: "Did not receive a response from server.")
        var req = URLRequest(url: url)
        req.httpMethod = "POST"
        req.addValue("application/json", forHTTPHeaderField: "Content-Type")
        req.httpBody = try! JSONSerialization.data(withJSONObject: json)
        session.dataTask(with: req) { (data, urlResp, err) in
            responseExpectation.fulfill()
            if let err = err {
                XCTFail("Error on response: \(err)")
            }
            guard let data = data else {
                XCTFail("No data returned")
                return
            }
            let body = try! JSONSerialization.jsonObject(with: data) as! [String:String]
            XCTAssert(body == jsonResponse, "Received body \(body) != json \(jsonResponse)")
        }.resume()
        
        waitForExpectations(timeout: 1)

    }
    
}

extension ServerTests {
    static var allTests = [
        ("testServer", testServer),<commit_msg>NSURLSession.shared is not available on Linux yet. Fixed<commit_after>//
//  ServerTests.swift
//  Edge
//
//  Created by <NAME> on 10/30/16.
//
//

import Foundation
import XCTest
@testable import HTTP

class ServerTests: XCTestCase {
    
    func testServer() {
        let requestExpectation = expectation(description: "Did not receive a request.")
        let json = ["message": "Message to server!"]
        let jsonResponse = ["message": "Message received!"]

        func handleRequest(request: Request) -> Response {
            requestExpectation.fulfill()
            let body = try! JSONSerialization.jsonObject(with: Data(request.body)) as! [String:String]
            XCTAssert(body == json, "Received body \(body) != json \(json)")
            XCTAssert(request.method == .post, "Incorrect method: \(request.method)")
            return try! Response(json: jsonResponse)
        }
        
        let server = HTTP.Server()
        server.listen(host: "0.0.0.0", port: 3000).startWithNext { client in
            
            let requestStream = client.read()
            requestStream.map(handleRequest).onNext{ response in
                let writeStream = client.write(response)
                writeStream.onFailed { err in
                    XCTFail(String(describing: err))
                }
                writeStream.start()
            }
            
            requestStream.onFailed { clientError in
                XCTFail("ClientError: \(clientError)")
            }
            
            requestStream.onCompleted {

            }
            
            requestStream.start()
        }
        
        let session = URLSession(configuration: .default)
        let urlString = "http://localhost:3000"
        let url = URL(string: urlString)!
        let responseExpectation = expectation(description: "Did not receive a response from server.")
        var req = URLRequest(url: url)
        req.httpMethod = "POST"
        req.addValue("application/json", forHTTPHeaderField: "Content-Type")
        req.httpBody = try! JSONSerialization.data(withJSONObject: json)
        session.dataTask(with: req) { (data, urlResp, err) in
            responseExpectation.fulfill()
            if let err = err {
                XCTFail("Error on response: \(err)")
            }
            guard let data = data else {
                XCTFail("No data returned")
                return
            }
            let body = try! JSONSerialization.jsonObject(with: data) as! [String:String]
            XCTAssert(body == jsonResponse, "Received body \(body) != json \(jsonResponse)")
        }.resume()
        
        waitForExpectations(timeout: 1)

    }
    
}

extension ServerTests {
    static var allTests = [
        ("testServer", testServer),
{'fraction_non_alphanumeric': 0.10636363636363637, 'fraction_numerical': 0.006909090909090909, 'mean_word_length': 1.877092050209205, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7128590', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1417, 'n_words': 365}