Skip to content

Commit 14b5a51

Browse files
authored
Fix searchForIndexHtml returns wrong content-type in HBFileMiddleware (#299)
* Fix searchForIndexHtml returns wrong content-type * Follow with the existing tests.
1 parent 49b1ad7 commit 14b5a51

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

Sources/HummingbirdFoundation/Files/FileMiddleware.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,8 @@ public struct HBFileMiddleware: HBMiddleware {
127127
headers.add(name: "eTag", value: eTag)
128128

129129
// content-type
130-
if let extPointIndex = path.lastIndex(of: ".") {
131-
let extIndex = path.index(after: extPointIndex)
132-
let ext = String(path.suffix(from: extIndex))
133-
if let contentType = HBMediaType.getMediaType(forExtension: ext) {
134-
headers.add(name: "content-type", value: contentType.description)
135-
}
130+
if let contentType = HBMediaType.getMediaType(forExtension: fullPath.pathExtension) {
131+
headers.add(name: "content-type", value: contentType.description)
136132
}
137133

138134
headers.replaceOrAdd(name: "accept-ranges", value: "bytes")

Tests/HummingbirdFoundationTests/FilesTests.swift

+7
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,16 @@ class HummingbirdFilesTests: XCTestCase {
295295
try app.XCTStart()
296296
defer { app.XCTStop() }
297297

298+
try app.XCTExecute(uri: "/index.html", method: .GET) { response in
299+
var body = try XCTUnwrap(response.body)
300+
XCTAssertEqual(body.readString(length: body.readableBytes), text)
301+
XCTAssertEqual(response.headers.first(name: "content-type"), "text/html")
302+
}
303+
298304
try app.XCTExecute(uri: "/", method: .GET) { response in
299305
var body = try XCTUnwrap(response.body)
300306
XCTAssertEqual(body.readString(length: body.readableBytes), text)
307+
XCTAssertEqual(response.headers.first(name: "content-type"), "text/html")
301308
}
302309
}
303310

0 commit comments

Comments
 (0)