Repository: vapor/vapor
File: Tests/VaporTests/ServerTests.swift (Swift)

Code before:
        
        /// Configure server name without stopping the server
        app.http.server.configuration.serverName = "New"
        /// Configure TLS without stopping the server
        var serverConfig = TLSConfiguration.makeServerConfiguration(certificateChain: [.certificate(cert)], privateKey: .privateKey(key))
        serverConfig.certificateVerification = .noHostnameVerification
        
        app.http.server.configuration.tlsConfiguration = serverConfig
        app.http.server.configuration.customCertificateVerifyCallback = { peerCerts, successPromise in
            /// This lies and accepts the above cert, which has actually expired.
            XCTAssertEqual(peerCerts, [cert])
            successPromise.succeed(.certificateVerified)
        }
        
        /// Make a TLS request this time around
        let b = try app.http.client.shared.execute(
            request: try HTTPClient.Request(
                url: "https://\(ip):\(port)/hello",
                method: .GET
            )
        ).wait()
        XCTAssertEqual(b.headers[.server], ["New"])
        XCTAssertEqual(b.body, ByteBuffer(string: "world"))
        
        /// Non-TLS request should now fail
        let c = try? app.http.client.shared.execute(
            request: try HTTPClient.Request(
                url: "http://\(ip):\(port)/hello",
                method: .GET
            )
        ).wait()
        XCTAssertNil(c)
    }
    
    override class func setUp() {
        XCTAssertTrue(isLoggingConfigured)
    }
}

extension Application.Servers.Provider {
    static var custom: Self {
        .init {
            $0.servers.use { $0.customServer }
        }
    }
}

extension Application {
    struct Key: StorageKey {
        typealias Value = CustomServer
    }

Review comment (suggestion):
Using an `XCTAssertThrows` may be better and checking the actual error in case we're missing the server actually being down etc

Code after:
        
        /// Configure server name without stopping the server
        app.http.server.configuration.serverName = "New"
        /// Configure TLS without stopping the server
        var serverConfig = TLSConfiguration.makeServerConfiguration(certificateChain: [.certificate(cert)], privateKey: .privateKey(key))
        serverConfig.certificateVerification = .noHostnameVerification
        
        app.http.server.configuration.tlsConfiguration = serverConfig
        app.http.server.configuration.customCertificateVerifyCallback = { peerCerts, successPromise in
            /// This lies and accepts the above cert, which has actually expired.
            XCTAssertEqual(peerCerts, [cert])
            successPromise.succeed(.certificateVerified)
        }
        
        /// Make a TLS request this time around
        let b = try app.http.client.shared.execute(
            request: try HTTPClient.Request(
                url: "https://\(ip):\(port)/hello",
                method: .GET
            )
        ).wait()
        XCTAssertEqual(b.headers[.server], ["New"])
        XCTAssertEqual(b.body, ByteBuffer(string: "world"))
        
        /// Non-TLS request should now fail
        XCTAssertThrowsError(try app.http.client.shared.execute(
            request: try HTTPClient.Request(
                url: "http://\(ip):\(port)/hello",
                method: .GET
            )
        ).wait()) { error in
            XCTAssertEqual(error as? HTTPClientError, HTTPClientError.remoteConnectionClosed)
        }
    }
    
    override class func setUp() {
        XCTAssertTrue(isLoggingConfigured)
    }
}

extension Application.Servers.Provider {
    static var custom: Self {
        .init {
            $0.servers.use { $0.customServer }
        }
    }
}

extension Application {
    struct Key: StorageKey {
        typealias Value = CustomServer