<commit_before>import Kitura
import SwiftyRequest
import XCTest
@testable import AroundTheTable

class RoutesTests: XCTestCase {
    
    static var allTests: [(String, (RoutesTests) -> () throws -> Void)] {
        return [
            ("testStaticFileServer", testStaticFileServer),
            ("testErrorHandlerForAPIRoutes", testErrorHandlerForAPIRoutes),
            ("testErrorHandlerForWebRoutes", testErrorHandlerForWebRoutes)
        ]
    }
    
    func testStaticFileServer() {
        let persistence = try! Persistence()
        let router = Router()
        Routes(persistence: persistence).configure(using: router)
        
        Kitura.addHTTPServer(onPort: 8081, with: router)
        Kitura.start()
        
        let requestReceived = expectation(description: "request received")
        let request = RestRequest(url: "http://localhost:8081/public/img/logo.png")
        request.responseVoid {
            response in
            XCTAssert(response.response?.statusCode == 200)
            requestReceived.fulfill()
        }
        
        waitForExpectations(timeout: 5) {
            error in
            Kitura.stop()
            XCTAssertNil(error)
        }
    }
    
    func testErrorHandlerForAPIRoutes() {
        let persistence = try! Persistence()
        let router = Router()
        // This route must be added first, otherwise it will come after the error handler.
        router.get("/api/something-bad") {
            request, response, next in
            throw ServerError.invalidState
        }
        Routes(persistence: persistence).configure(using: router)
        
        Kitura.addHTTPServer(onPort: 8081, with: router)
        Kitura.start()
        
        let requestReceived = expectation(description: "request received")
        let request = RestRequest(url: "http://localhost:8081/api/something-bad")
        request.responseVoid {
            response in
            XCTAssert(response.response?.statusCode == 500)
            requestReceived.fulfill()
        }
        
        waitForExpectations(timeout: 5) {
            error in
            Kitura.stop()
            XCTAssertNil(error)
        }
    }
    
    func testErrorHandlerForWebRoutes() {
        let persistence = try! Persistence()
        let router = Router()
        // This route must be added first, otherwise it will come after the error handler.
        router.get("/web/something-bad") {<commit_msg>Remove unnecessary usage of try! from tests.<commit_after>import Kitura
import SwiftyRequest
import XCTest
@testable import AroundTheTable

class RoutesTests: XCTestCase {
    
    static var allTests: [(String, (RoutesTests) -> () throws -> Void)] {
        return [
            ("testStaticFileServer", testStaticFileServer),
            ("testErrorHandlerForAPIRoutes", testErrorHandlerForAPIRoutes),
            ("testErrorHandlerForWebRoutes", testErrorHandlerForWebRoutes)
        ]
    }
    
    func testStaticFileServer() throws {
        let persistence = try Persistence()
        let router = Router()
        Routes(persistence: persistence).configure(using: router)
        
        Kitura.addHTTPServer(onPort: 8081, with: router)
        Kitura.start()
        
        let requestReceived = expectation(description: "request received")
        let request = RestRequest(url: "http://localhost:8081/public/img/logo.png")
        request.responseVoid {
            response in
            XCTAssert(response.response?.statusCode == 200)
            requestReceived.fulfill()
        }
        
        waitForExpectations(timeout: 5) {
            error in
            Kitura.stop()
            XCTAssertNil(error)
        }
    }
    
    func testErrorHandlerForAPIRoutes() throws {
        let persistence = try Persistence()
        let router = Router()
        // This route must be added first, otherwise it will come after the error handler.
        router.get("/api/something-bad") {
            request, response, next in
            throw ServerError.invalidState
        }
        Routes(persistence: persistence).configure(using: router)
        
        Kitura.addHTTPServer(onPort: 8081, with: router)
        Kitura.start()
        
        let requestReceived = expectation(description: "request received")
        let request = RestRequest(url: "http://localhost:8081/api/something-bad")
        request.responseVoid {
            response in
            XCTAssert(response.response?.statusCode == 500)
            requestReceived.fulfill()
        }
        
        waitForExpectations(timeout: 5) {
            error in
            Kitura.stop()
            XCTAssertNil(error)
        }
    }
    
    func testErrorHandlerForWebRoutes() throws {
        let persistence = try Persistence()
        let router = Router()
        // This route must be added first, otherwise it will come after the error handler.
        router.get("/web/something-bad") {
{'fraction_non_alphanumeric': 0.0889564336372847, 'fraction_numerical': 0.009726443768996961, 'mean_word_length': 2.12999365884591, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '7583362', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1275, 'n_words': 303}