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

Code before:
        
        app.middleware.use(fileMiddleware)
        
        try await app.testable().test(.GET, "/index.html") { result async in
            XCTAssertEqual(result.status, .ok)
            XCTAssertEqual(result.body.string, "<h1>Subdirectory Default</h1>\n")
        }
    }
    
    func testFileMiddlewareFromBundleInvalidPublicDirectory() {
        XCTAssertThrowsError(try FileMiddleware(bundle: .module, publicDirectory: "/totally-real/folder")) { error in
            guard let error = error as? FileMiddleware.BundleSetupError else {
                return XCTFail("Error should be of type FileMiddleware.SetupError")
            }
            XCTAssertEqual(error, .publicDirectoryIsNotAFolder)
        }
    }
    
    func testTracingMiddleware() async throws {
        let tracer = TestTracer()
        InstrumentationSystem.bootstrap(tracer)
        app.grouped(TracingMiddleware()).get("testTracing") { req -> String in
            return "done"
        }

        try await app.testable().test(.GET, "/testTracing") { res in
            XCTAssertEqual(res.status, .ok)
            XCTAssertEqual(res.body.string, "done")
        }
        
        let span = try XCTUnwrap(tracer.spans.first)
        XCTAssertEqual(span.operationName, "GET /testTracing")
        
        XCTAssertEqual(span.attributes["http.request.method"]?.toSpanAttribute(), "GET")
        XCTAssertEqual(span.attributes["url.path"]?.toSpanAttribute(), "/testTracing")
        XCTAssertEqual(span.attributes["url.scheme"]?.toSpanAttribute(), nil)
        
        XCTAssertEqual(span.attributes["http.route"]?.toSpanAttribute(), "/testTracing")
        XCTAssertEqual(span.attributes["network.protocol.name"]?.toSpanAttribute(), "http")
        XCTAssertEqual(span.attributes["server.address"]?.toSpanAttribute(), "127.0.0.1")
        XCTAssertEqual(span.attributes["server.port"]?.toSpanAttribute(), 8080)
        XCTAssertEqual(span.attributes["url.query"]?.toSpanAttribute(), nil)
        
        XCTAssertEqual(span.attributes["client.address"]?.toSpanAttribute(), nil)
        XCTAssertEqual(span.attributes["network.peer.address"]?.toSpanAttribute(), nil)
        XCTAssertEqual(span.attributes["network.peer.port"]?.toSpanAttribute(), nil)
        XCTAssertEqual(span.attributes["network.protocol.version"]?.toSpanAttribute(), "1.1")
        
        XCTAssertEqual(span.attributes["user_agent.original"]?.toSpanAttribute(), nil)
        
        XCTAssertEqual(span.attributes["http.response.status_code"]?.toSpanAttribute(), 200)

Review comment (suggestion):
I think if you do `app.testable(method: .running(port: 0))` that will make a real HTTP request to give us more attributes below

Code after:
        
        app.middleware.use(fileMiddleware)
        
        try await app.testable().test(.GET, "/index.html") { result async in
            XCTAssertEqual(result.status, .ok)
            XCTAssertEqual(result.body.string, "<h1>Subdirectory Default</h1>\n")
        }
    }
    
    func testFileMiddlewareFromBundleInvalidPublicDirectory() {
        XCTAssertThrowsError(try FileMiddleware(bundle: .module, publicDirectory: "/totally-real/folder")) { error in
            guard let error = error as? FileMiddleware.BundleSetupError else {
                return XCTFail("Error should be of type FileMiddleware.SetupError")
            }
            XCTAssertEqual(error, .publicDirectoryIsNotAFolder)
        }
    }
    
    func testTracingMiddleware() async throws {
        app.traceAutoPropagation = true
        let tracer = TestTracer()
        InstrumentationSystem.bootstrap(tracer)
        
        struct TestServiceContextMiddleware: Middleware {
            func respond(to request: Request, chainingTo next: any Responder) -> EventLoopFuture<Response> {
                XCTAssertNotNil(ServiceContext.current)
                return next.respond(to: request)
            }
        }
        
        app.grouped(
            TracingMiddleware()
        ).grouped(
            TestServiceContextMiddleware()
        ).get("testTracing") { req -> String in
            // Validates that TracingMiddleware sets the serviceContext
            XCTAssertNotNil(req.serviceContext)
            // Validates that the span's service context is propagated into the
            // Task.local storage of the responder closure, thereby ensuring that
            // spans created in the closure are nested under the request span.
            // Requires Application.traceAutoPropagation to be enabled
            XCTAssertNotNil(ServiceContext.current)
            return "done"
        }

        try await app.testable(method: .running(hostname: "127.0.0.1", port: 8080)).test(
            .GET,
            "/testTracing?foo=bar",
            beforeRequest: { request in
                request.headers.add(name: HTTPHeaders.Name.userAgent.description, value: "test")
            },