Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows setting content type header via middleware #47

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@ Arguments:
module.exports = {
Logger,
Cli
}
}
37 changes: 30 additions & 7 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,44 @@ class EleventyDevServer {
return (content || "") + script;
}

getFileContentType(filepath, res) {
let contentType = res.getHeader("Content-Type");

// Content-Type might be already set via middleware
if (contentType) {
return contentType;
}

let mimeType = mime.getType(filepath);
if (!mimeType) {
return;
}

contentType = mimeType;

// We only want to append charset if the header is not already set
if (contentType === "text/html") {
contentType = `text/html; charset=${this.options.encoding}`;
}

return contentType;
}

renderFile(filepath, res) {
let contents = fs.readFileSync(filepath);
let mimeType = mime.getType(filepath);
let contentType = this.getFileContentType(filepath, res);

if (mimeType === "text/html") {
res.setHeader("Content-Type", `text/html; charset=${this.options.encoding}`);
if (!contentType) {
return res.end(contents);
}

res.setHeader("Content-Type", contentType);

if (contentType.startsWith("text/html")) {
// the string is important here, wrapResponse expects strings internally for HTML content (for now)
return res.end(contents.toString());
}

if (mimeType) {
res.setHeader("Content-Type", mimeType);
}

return res.end(contents);
}

Expand Down
1 change: 1 addition & 0 deletions test/stubs/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SAMPLE PHP
48 changes: 47 additions & 1 deletion test/testServerRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ async function makeRequestTo(t, server, path) {
})
}

async function fetchHeadersForRequest(t, server, path) {
let port = await server.getPort();

return new Promise(resolve => {
const options = {
hostname: 'localhost',
port,
path,
method: 'GET',
};

http.get(options, (res) => {
const { statusCode } = res;
if(statusCode !== 200) {
throw new Error("Invalid status code" + statusCode);
}

let headers = res.headers;
resolve(headers);

}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
})
}

test("Standard request", async t => {
let server = new EleventyDevServer("test-server", "./test/stubs/", getOptions());
server.serve(8080);
Expand Down Expand Up @@ -255,4 +281,24 @@ test("Fun unicode paths", async t => {
t.true(data.startsWith("This is a test"));

server.close();
});
});

test("Content-Type header via middleware", async t => {
let server = new EleventyDevServer("test-server", "./test/stubs/", getOptions({
middleware: [
function (req, res, next) {
if (/.*\.php$/.test(req.url)) {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
}

next();
}
]
}));
server.serve(8080);

let data = await fetchHeadersForRequest(t, server, encodeURI(`/index.php`));
t.true(data['content-type'] === 'text/html; charset=utf-8');

server.close();
});