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

Code before:
        
        let unsupportedNoncompressedResponse = try app.client.post("http://localhost:\(port)/compressed") { request in
            request.body = ByteBuffer(data: compressedPayload)
        }.wait()
        
        if let body = unsupportedNoncompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.contentString, String(decoding: compressedPayload, as: UTF8.self))
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.count)
        } else {
            XCTFail()
        }
        
        // TODO: The server should probably reject this?
        let unsupportedCompressedResponse = try app.client.post("http://localhost:\(port)/compressed") { request in
            request.headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
            request.body = ByteBuffer(data: compressedPayload)
        }.wait()
        
        if let body = unsupportedCompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.contentString, String(decoding: compressedPayload, as: UTF8.self))
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.count)
        } else {
            XCTFail()
        }
        
        app.http.server.configuration.requestDecompression = .enabled(limit: .size(compressiblePayload.utf8.count))
        
        let supportedUncompressedResponse = try app.client.post("http://localhost:\(port)/compressed") { request in
            request.body = ByteBuffer(data: compressedPayload)
        }.wait()
        
        if let body = supportedUncompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.contentString, String(decoding: compressedPayload, as: UTF8.self))
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.count)
        } else {
            XCTFail()
        }
        
        let supportedCompressedResponse = try app.client.post("http://localhost:\(port)/compressed") { request in
            request.headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
            request.body = ByteBuffer(data: compressedPayload)
        }.wait()
        
        if let body = supportedCompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.contentString, compressiblePayload)
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.count)
        } else {

Review comment (suggestion):
Probably better as a follow-up PR, but a question regarding this currently working behavior: Should this actually be rejected, because the server explicitly doesn't support request decompression for this test case? I can file an issue if there is agreement here.

Code after:
        
        let unsupportedNoncompressedResponse = try await app.client.post("http://localhost:\(port)/compressed") { request in
            request.body = compressedPayload
        }
        
        if let body = unsupportedNoncompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.content, compressedPayload)
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.readableBytes)
        } else {
            XCTFail("Missing unsupportedNoncompressedResponse.body")
        }
        
        // TODO: The server should probably reject this?
        let unsupportedCompressedResponse = try await app.client.post("http://localhost:\(port)/compressed") { request in
            request.headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
            request.body = compressedPayload
        }
        
        if let body = unsupportedCompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.content, compressedPayload)
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.readableBytes)
        } else {
            XCTFail("Missing unsupportedCompressedResponse.body")
        }
        
        app.http.server.configuration.requestDecompression = .enabled(limit: .size(compressiblePayload.utf8.count))
        
        let supportedUncompressedResponse = try await app.client.post("http://localhost:\(port)/compressed") { request in
            request.body = compressedPayload
        }
        
        if let body = supportedUncompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.content, compressedPayload)
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.readableBytes)
        } else {
            XCTFail("Missing supportedUncompressedResponse.body")
        }
        
        let supportedCompressedResponse = try await app.client.post("http://localhost:\(port)/compressed") { request in
            request.headers.replaceOrAdd(name: .contentEncoding, value: "gzip")
            request.body = compressedPayload
        }
        
        if let body = supportedCompressedResponse.body {
            let decodedResponse = try JSONDecoder().decode(TestResponse.self, from: body)
            XCTAssertEqual(decodedResponse.content, ByteBuffer(string: compressiblePayload))
            XCTAssertEqual(decodedResponse.contentLength, compressedPayload.readableBytes)
        } else {