From 05c36b57453d23ea63785d58a7dbc7b70ba1745e Mon Sep 17 00:00:00 2001 From: Dimitri Bouniol Date: Tue, 18 Jun 2024 04:51:32 -0700 Subject: [PATCH] Added a test for responding with 304 Not Modified with HTTP compression (#224) --- Package.swift | 2 +- .../HTTPResponseCompressorTest.swift | 28 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index b32ae1f5..f7c31beb 100644 --- a/Package.swift +++ b/Package.swift @@ -164,7 +164,7 @@ let package = Package( .library(name: "NIOHTTPTypesHTTP2", targets: ["NIOHTTPTypesHTTP2"]), ], dependencies: [ - .package(url: "https://github.com/apple/swift-nio.git", from: "2.42.0"), + .package(url: "https://github.com/apple/swift-nio.git", from: "2.67.0"), .package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.27.0"), .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), .package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"), diff --git a/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift b/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift index 01c93af7..44a2ef08 100644 --- a/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift +++ b/Tests/NIOHTTPCompressionTests/HTTPResponseCompressorTest.swift @@ -660,6 +660,34 @@ class HTTPResponseCompressorTest: XCTestCase { } } } + + func testBypassCompressionWhenNotModified() throws { + let channel = EmbeddedChannel() + XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseCompressor()).wait()) + defer { + XCTAssertNoThrow(try channel.finish()) + } + + try sendRequest(acceptEncoding: "deflate;q=2.2, gzip;q=0.3", channel: channel) + + let head = HTTPResponseHead( + version: .init(major: 1, minor: 1), + status: .notModified, + headers: .init() + ) + try channel.writeOutbound(HTTPServerResponsePart.head(head)) + try channel.writeOutbound(HTTPServerResponsePart.end(nil)) + + while let part = try channel.readOutbound(as: HTTPServerResponsePart.self) { + switch part { + case .head(let head): + XCTAssertEqual(head.headers[canonicalForm: "content-encoding"], []) + case .body: + XCTFail("Unexpected body") + case .end: break + } + } + } } extension EventLoopFuture {