-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7867fbe
commit cfbc001
Showing
3 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} | ||
|