Skip to content

Commit

Permalink
Fix searchForIndexHtml returns wrong content-type in HBFileMiddleware (
Browse files Browse the repository at this point in the history
…#299)

* Fix searchForIndexHtml returns wrong content-type

* Follow with the existing tests.
  • Loading branch information
sidepelican authored Dec 3, 2023
1 parent 49b1ad7 commit 14b5a51
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 2 additions & 6 deletions Sources/HummingbirdFoundation/Files/FileMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,8 @@ public struct HBFileMiddleware: HBMiddleware {
headers.add(name: "eTag", value: eTag)

// content-type
if let extPointIndex = path.lastIndex(of: ".") {
let extIndex = path.index(after: extPointIndex)
let ext = String(path.suffix(from: extIndex))
if let contentType = HBMediaType.getMediaType(forExtension: ext) {
headers.add(name: "content-type", value: contentType.description)
}
if let contentType = HBMediaType.getMediaType(forExtension: fullPath.pathExtension) {
headers.add(name: "content-type", value: contentType.description)
}

headers.replaceOrAdd(name: "accept-ranges", value: "bytes")
Expand Down
7 changes: 7 additions & 0 deletions Tests/HummingbirdFoundationTests/FilesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,16 @@ class HummingbirdFilesTests: XCTestCase {
try app.XCTStart()
defer { app.XCTStop() }

try app.XCTExecute(uri: "/index.html", method: .GET) { response in
var body = try XCTUnwrap(response.body)
XCTAssertEqual(body.readString(length: body.readableBytes), text)
XCTAssertEqual(response.headers.first(name: "content-type"), "text/html")
}

try app.XCTExecute(uri: "/", method: .GET) { response in
var body = try XCTUnwrap(response.body)
XCTAssertEqual(body.readString(length: body.readableBytes), text)
XCTAssertEqual(response.headers.first(name: "content-type"), "text/html")
}
}

Expand Down

0 comments on commit 14b5a51

Please sign in to comment.