Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'master' into no_server_headers
  • Loading branch information
thornjad committed Aug 6, 2021
2 parents a7fdf0f + a4ec10b commit c57654d
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 143 deletions.
71 changes: 0 additions & 71 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions LICENSE
@@ -1,5 +1,4 @@
Copyright (c) 2011-2021 Charlie Robbins, Marak Squires, Jade Michael Thornton
and the Contributors.
Copyright (c) 2011-2021 Charlie Robbins, Marak Squires, Jade Michael Thornton and the Contributors.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
12 changes: 7 additions & 5 deletions lib/core/index.js
Expand Up @@ -228,11 +228,9 @@ module.exports = function createMiddleware(_dir, _options) {
const etag = generateEtag(stat, weakEtags);
let cacheControl = cache;
let stream = null;
if (contentType) {
charSet = mime.charsets.lookup(contentType, 'utf-8');
if (charSet) {
contentType += `; charset=${charSet}`;
}
if (contentType && isTextFile(contentType)) {
// Assume text types are utf8
contentType += '; charset=UTF-8';
}

if (file === gzippedFile) { // is .gz picked up
Expand Down Expand Up @@ -394,6 +392,10 @@ module.exports = function createMiddleware(_dir, _options) {
});
}

function isTextFile(mimeType) {
return (/^text\/|^application\/(javascript|json)/).test(mimeType);
}

// serve gzip file if exists and is valid
function tryServeWithGzip() {
fs.stat(gzippedFile, (err, stat) => {
Expand Down
17 changes: 17 additions & 0 deletions test/check-headers.js
@@ -0,0 +1,17 @@
const request = require('request');

module.exports = (t, server, path, check) => {
server.listen(() => {
const port = server.address().port;
const uri = `http://localhost:${port}/${path}`;

request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
check(t, res.headers);
});
});
t.once('end', () => {
server.close();
});
}
6 changes: 3 additions & 3 deletions test/cli.test.js
Expand Up @@ -98,7 +98,7 @@ test('setting mimeTypes via cli - .types file', (t) => {
ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/secret; charset=utf-8');
t.equal(res.headers['content-type'], 'application/secret');
});
});
});
Expand All @@ -118,7 +118,7 @@ test('setting mimeTypes via cli - directly', (t) => {
ecstatic.stdout.on('data', (msg) => {
checkServerIsRunning(`${defaultUrl}:${port}/custom_mime_type.opml`, msg, t, (err, res) => {
t.error(err);
t.equal(res.headers['content-type'], 'application/x-my-type; charset=utf-8');
t.equal(res.headers['content-type'], 'application/x-my-type');
});
});
});
});
43 changes: 32 additions & 11 deletions test/content-type.test.js
Expand Up @@ -2,14 +2,16 @@

const test = require('tap').test;
const http = require('http');
const request = require('request');
const ecstatic = require('../lib/core');
const checkHeaders = require('./check-headers.js');

test('default default contentType', (t) => {
const root = `${__dirname}/public/`;

test('global default contentType', (t) => {
let server = null;
try {
server = http.createServer(ecstatic({
root: `${__dirname}/public/`,
root,
contentType: 'text/plain',
}));
} catch (e) {
Expand All @@ -19,13 +21,32 @@ test('default default contentType', (t) => {

t.plan(3);

server.listen(0, () => {
const port = server.address().port;
request.get(`http://localhost:${port}/f_f`, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(res.headers['content-type'], 'text/plain; charset=UTF-8');
server.close(() => { t.end(); });
});

checkHeaders(t, server, 'f_f', (t, headers) => {
t.equal(headers['content-type'], 'text/plain; charset=UTF-8');
});
});

test('content type text', (t) => {
t.plan(3);

const server = http.createServer(
ecstatic({root})
);

checkHeaders(t, server, 'subdir/e.html', (t, headers) => {
t.equal(headers['content-type'], 'text/html; charset=UTF-8');
});
});

test('content type binary', (t) => {
t.plan(3);

const server = http.createServer(
ecstatic({root})
);

checkHeaders(t, server, 'subdir/app.wasm', (t, headers) => {
t.equal(headers['content-type'], 'application/wasm');
});
});
2 changes: 1 addition & 1 deletion test/custom-content-type-file-secret.test.js
Expand Up @@ -24,7 +24,7 @@ test('custom contentType via .types file', (t) => {
request.get(`http://localhost:${port}/custom_mime_type.opml`, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200, 'custom_mime_type.opml should be found');
t.equal(res.headers['content-type'], 'application/secret; charset=utf-8');
t.equal(res.headers['content-type'], 'application/secret');
server.close(() => { t.end(); });
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/custom-content-type-file.test.js
Expand Up @@ -40,7 +40,7 @@ test('custom contentType via .types file', (t) => {
request.get(`http://localhost:${port}/custom_mime_type.opml`, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200, 'custom_mime_type.opml should be found');
t.equal(res.headers['content-type'], 'application/foo; charset=utf-8');
t.equal(res.headers['content-type'], 'application/foo');
server.close(() => { t.end(); });
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/custom-content-type.test.js
Expand Up @@ -26,7 +26,7 @@ test('custom contentType', (t) => {
request.get(`http://localhost:${port}/custom_mime_type.opml`, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200, 'custom_mime_type.opml should be found');
t.equal(res.headers['content-type'], 'application/jon; charset=utf-8');
t.equal(res.headers['content-type'], 'application/jon');
server.close(() => { t.end(); });
});
});
Expand Down
58 changes: 10 additions & 48 deletions test/headers.test.js
Expand Up @@ -3,7 +3,7 @@
const test = require('tap').test;
const ecstatic = require('../lib/core');
const http = require('http');
const request = require('request');
const checkHeaders = require('./check-headers.js');

const root = `${__dirname}/public`;

Expand All @@ -22,19 +22,9 @@ test('headers object', (t) => {
})
);

server.listen(() => {
const port = server.address().port;
const uri = `http://localhost:${port}/subdir`;

request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(res.headers.wow, 'sweet');
t.equal(res.headers.cool, 'beans');
});
});
t.once('end', () => {
server.close();
checkHeaders(t, server, 'subdir', (t, headers) => {
t.equal(headers.wow, 'sweet');
t.equal(headers.cool, 'beans');
});
});

Expand All @@ -50,18 +40,8 @@ test('header string', (t) => {
})
);

server.listen(() => {
const port = server.address().port;
const uri = `http://localhost:${port}/subdir`;

request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(res.headers.beep, 'boop');
});
});
t.once('end', () => {
server.close();
checkHeaders(t, server, 'subdir', (t, headers) => {
t.equal(headers.beep, 'boop');
});
});

Expand All @@ -80,17 +60,8 @@ test('header array', (t) => {
})
);

server.listen(() => {
const port = server.address().port;
const uri = `http://localhost:${port}/subdir`;
request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(res.headers.beep, 'boop');
});
});
t.once('end', () => {
server.close();
checkHeaders(t, server, 'subdir', (t, headers) => {
t.equal(headers.beep, 'boop');
});
});

Expand All @@ -109,16 +80,7 @@ test('H array', (t) => {
})
);

server.listen(() => {
const port = server.address().port;
const uri = `http://localhost:${port}/subdir`;
request.get({ uri }, (err, res) => {
t.ifError(err);
t.equal(res.statusCode, 200);
t.equal(res.headers.beep, 'boop');
});
});
t.once('end', () => {
server.close();
checkHeaders(t, server, 'subdir', (t, headers) => {
t.equal(headers.beep, 'boop');
});
});
1 change: 1 addition & 0 deletions test/public/subdir/app.wasm
@@ -0,0 +1 @@
Fake WebAssemble file

0 comments on commit c57654d

Please sign in to comment.