Skip to content

Commit

Permalink
Add File middleware testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler committed Jan 25, 2021
1 parent 7867fbe commit cfbc001
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ let package = Package(
.byName(name: "HummingbirdCore"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.testTarget(name: "HummingbirdFilesTests", dependencies: [
.byName(name: "HummingbirdFiles"),
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]),
.testTarget(name: "HummingbirdJSONTests", dependencies: [
.byName(name: "HummingbirdJSON"),
.byName(name: "HummingbirdXCT"),
Expand Down
17 changes: 9 additions & 8 deletions Sources/HummingbirdFiles/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ public struct HBFileMiddleware: HBMiddleware {
self.fileIO = .init(application: application)

let workingFolder: String
if let cwd = getcwd(nil, Int(PATH_MAX)) {
workingFolder = String(cString: cwd)
free(cwd)
if rootFolder.first == "/" {
workingFolder = rootFolder
} else {
workingFolder = "./"
}
defer {
application.logger.info("FileMiddleware serving from \(workingFolder)")
if let cwd = getcwd(nil, Int(PATH_MAX)) {
workingFolder = String(cString: cwd)
free(cwd)
} else {
workingFolder = "."
}
}

application.logger.info("FileMiddleware serving from \(workingFolder)/\(rootFolder)")
}

public func apply(to request: HBRequest, next: HBResponder) -> EventLoopFuture<HBResponse> {
Expand Down
55 changes: 55 additions & 0 deletions Tests/HummingbirdFilesTests/FilesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import AsyncHTTPClient
import Foundation
import Hummingbird
import HummingbirdFiles
import XCTest

class HummingbirdFilesTests: XCTestCase {

func testGet() {
let app = HBApplication(configuration: .init(address: .hostname(port: Int.random(in: 4000..<9000))))
app.middlewares.add(HBFileMiddleware(".", application: app))

let text = "Test file contents"
let data = Data(text.utf8)
let fileURL = URL(fileURLWithPath: "test.txt")
XCTAssertNoThrow(try data.write(to: fileURL))
defer { XCTAssertNoThrow(try FileManager.default.removeItem(at: fileURL)) }

app.start()
defer { app.stop(); app.wait() }

let client = HTTPClient(eventLoopGroupProvider: .shared(app.eventLoopGroup))
defer { XCTAssertNoThrow(try client.syncShutdown()) }

let future = client.get(url: "http://localhost:\(app.configuration.address.port!)/test.txt").flatMapThrowing { response in
var body = try XCTUnwrap(response.body)
XCTAssertEqual(body.readString(length: body.readableBytes), text)
}
XCTAssertNoThrow(try future.wait())
}

func testHead() throws {
let app = HBApplication(configuration: .init(address: .hostname(port: Int.random(in: 4000..<9000))))
app.middlewares.add(HBFileMiddleware(".", application: app))

let text = "Test file contents"
let data = Data(text.utf8)
let fileURL = URL(fileURLWithPath: "test.txt")
XCTAssertNoThrow(try data.write(to: fileURL))
defer { XCTAssertNoThrow(try FileManager.default.removeItem(at: fileURL)) }

app.start()
defer { app.stop(); app.wait() }

let client = HTTPClient(eventLoopGroupProvider: .shared(app.eventLoopGroup))
defer { XCTAssertNoThrow(try client.syncShutdown()) }

let request = try HTTPClient.Request(url: "http://localhost:\(app.configuration.address.port!)/test.txt", method: .HEAD)
let future = client.execute(request: request).flatMapThrowing { response in
XCTAssertEqual(response.headers["Content-Length"].first, text.utf8.count.description)
}
XCTAssertNoThrow(try future.wait())
}
}

0 comments on commit cfbc001

Please sign in to comment.