Skip to content

Commit

Permalink
Run swift-format
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Nov 27, 2024
1 parent a38fb99 commit ff87433
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
26 changes: 16 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ let package = Package(
name: "hummingbird-compression",
platforms: [.macOS(.v14), .iOS(.v17), .tvOS(.v17)],
products: [
.library(name: "HummingbirdCompression", targets: ["HummingbirdCompression"]),
.library(name: "HummingbirdCompression", targets: ["HummingbirdCompression"])
],
dependencies: [
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
.package(url: "https://github.com/adam-fowler/compress-nio.git", from: "1.4.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.32.1"),
],
targets: [
.target(name: "HummingbirdCompression", dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "CompressNIO", package: "compress-nio"),
]),
.testTarget(name: "HummingbirdCompressionTests", dependencies: [
.byName(name: "HummingbirdCompression"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]),
.target(
name: "HummingbirdCompression",
dependencies: [
.product(name: "Hummingbird", package: "hummingbird"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "CompressNIO", package: "compress-nio"),
]
),
.testTarget(
name: "HummingbirdCompressionTests",
dependencies: [
.byName(name: "HummingbirdCompression"),
.product(name: "HummingbirdTesting", package: "hummingbird"),
]
),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ public struct RequestDecompressionMiddleware<Context: RequestContext>: RouterMid
public func handle(_ request: Request, context: Context, next: (Request, Context) async throws -> Response) async throws -> Response {
if let algorithm = algorithm(from: request.headers[values: .contentEncoding]) {
var request = request
request.body = .init(asyncSequence: DecompressByteBufferSequence(
base: request.body,
algorithm: algorithm,
windowSize: self.windowSize,
logger: context.logger
))
request.body = .init(
asyncSequence: DecompressByteBufferSequence(
base: request.body,
algorithm: algorithm,
windowSize: self.windowSize,
logger: context.logger
)
)
let response = try await next(request, context)
return response
} else {
Expand Down
22 changes: 16 additions & 6 deletions Tests/HummingbirdCompressionTests/CompressionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HummingBirdCompressionTests: XCTestCase {
let router = Router()
router.middlewares.add(ResponseCompressionMiddleware())
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
try await app.test(.router) { client in
Expand All @@ -50,7 +50,7 @@ class HummingBirdCompressionTests: XCTestCase {
let router = Router()
router.middlewares.add(ResponseCompressionMiddleware())
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
let buffer = self.randomBuffer(size: 512_000)
Expand Down Expand Up @@ -86,7 +86,12 @@ class HummingBirdCompressionTests: XCTestCase {
group.addTask {
try await app.test(.router) { client in
let testBuffer = buffer.getSlice(at: Int.random(in: 0...256_000), length: Int.random(in: 0...256_000))
try await client.execute(uri: "/echo", method: .post, headers: [.acceptEncoding: "gzip"], body: testBuffer) { response in
try await client.execute(
uri: "/echo",
method: .post,
headers: [.acceptEncoding: "gzip"],
body: testBuffer
) { response in
var body = response.body
let uncompressed: ByteBuffer
if response.headers[.contentEncoding] == "gzip" {
Expand Down Expand Up @@ -162,7 +167,7 @@ class HummingBirdCompressionTests: XCTestCase {
router.middlewares.add(VerifyResponseBodyChunkSize(bufferSize: 256))
router.middlewares.add(ResponseCompressionMiddleware(windowSize: 256))
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
try await app.test(.router) { client in
Expand Down Expand Up @@ -199,7 +204,7 @@ class HummingBirdCompressionTests: XCTestCase {
let router = Router()
router.middlewares.add(RequestDecompressionMiddleware())
router.post("/echo") { request, _ -> Response in
return .init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
.init(status: .ok, headers: [:], body: .init(asyncSequence: request.body))
}
let app = Application(router: router)
try await app.test(.router) { client in
Expand Down Expand Up @@ -257,7 +262,12 @@ class HummingBirdCompressionTests: XCTestCase {
group.addTask {
let testBuffer = buffer.getSlice(at: Int.random(in: 0...256_000), length: Int.random(in: 0...256_000))!
let compressedBuffer = try compress(testBuffer)
try await client.execute(uri: "/echo", method: .post, headers: [.contentEncoding: "gzip"], body: compressedBuffer) { response in
try await client.execute(
uri: "/echo",
method: .post,
headers: [.contentEncoding: "gzip"],
body: compressedBuffer
) { response in
XCTAssertEqual(response.body, testBuffer)
}
}
Expand Down

0 comments on commit ff87433

Please sign in to comment.