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

Code before:

                let metadata = logs.getMetadata()
                #expect(metadata["ahc-request-id"] != nil)
            }
        }
    }

    // MARK: - Helpers
    func withRemoteApp<T>(_ block: @Sendable (Application, Int) async throws -> T) async throws -> T {
        let remoteApp = try await Application(.testing, configReader: testConfigReader)
        remoteApp.serverConfiguration.address = .hostname("127.0.0.1", port: 0)

        remoteApp.get("json") { _ in
            SomeJSON()
        }

        remoteApp.get("status", ":status") { req -> HTTPStatus in
            let status = try req.parameters.require("status", as: Int.self)
            return HTTPStatus(code: status)
        }

        remoteApp.post("anything") { req -> AnythingResponse in
            let headers = req.headers.reduce(into: [String: String]()) {
                $0[$1.name.canonicalName] = $1.value
            }

            guard let json:[String:Any] = try await JSONSerialization.jsonObject(with: req.newBody.data!) as? [String:Any] else {
                throw Abort(.badRequest)
            }

            let jsonResponse = json.mapValues {
                return "\($0)"
            }

            return AnythingResponse(headers: headers, json: jsonResponse)
        }

        remoteApp.get("stalling") { _ in
            try await Task.sleep(for: .seconds(1))
            return SomeJSON()
        }

        do {
            let result = try await withRunningApp(app: remoteApp) { port in
                let result = try await block(remoteApp, port)
                return result
            }

            try await remoteApp.shutdown()
            return result
        } catch {

Review comment (suggestion):
I don't know why, but without this `@Sendable` the code won't compile. I don't know why it didn't require it before.

Code after:
                let response = try await req.application.client.get("htp://localhost/status/2 1")
                return response.description
            }

            try await app.testing(method: .running).test(.get, "/client") { res in
                #expect(res.status.code == 500)
            }
        }
    }
    #endif

    @Test("Test Custom Client")
    func testCustomClient() async throws {
        try await withRemoteApp { remoteApp, remoteAppPort in
            let customClient = CustomClient()
            try await withApp(services: .init(client: .provided(customClient))) { app in
                _ = try await app.client.get("https://vapor.codes")

                #expect(customClient.requests.count == 1)
                #expect(customClient.requests.first?.url.host == "vapor.codes")
            }
        }
    }

    // MARK: - Helpers
    func withRemoteApp<T>(_ block: @Sendable (Application, Int) async throws -> T) async throws -> T {
        let remoteApp = try await Application(.testing, configReader: testConfigReader)
        remoteApp.serverConfiguration.address = .hostname("127.0.0.1", port: 0)

        remoteApp.get("json") { _ in
            SomeJSON()
        }

        remoteApp.get("status", ":status") { req -> HTTPStatus in
            let status = try req.parameters.require("status", as: Int.self)
            return HTTPStatus(code: status)
        }

        remoteApp.post("anything") { req -> AnythingResponse in
            let headers = req.headers.reduce(into: [String: String]()) {
                $0[$1.name.canonicalName] = $1.value
            }

            guard let json:[String:Any] = try await JSONSerialization.jsonObject(with: req.newBody.data!) as? [String:Any] else {
                throw Abort(.badRequest)
            }

            let jsonResponse = json.mapValues {
                return "\($0)"
            }