mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-19 14:13:34 +01:00
favicon: Refactor handler and add tests
This commit is contained in:
parent
d0d4b95980
commit
92e0bff80c
3 changed files with 75 additions and 16 deletions
|
@ -2,9 +2,12 @@
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const eejs = require('../../eejs');
|
const eejs = require('../../eejs');
|
||||||
|
const fs = require('fs');
|
||||||
|
const fsp = fs.promises;
|
||||||
const toolbar = require('../../utils/toolbar');
|
const toolbar = require('../../utils/toolbar');
|
||||||
const hooks = require('../../../static/js/pluginfw/hooks');
|
const hooks = require('../../../static/js/pluginfw/hooks');
|
||||||
const settings = require('../../utils/Settings');
|
const settings = require('../../utils/Settings');
|
||||||
|
const util = require('util');
|
||||||
const webaccess = require('./webaccess');
|
const webaccess = require('./webaccess');
|
||||||
|
|
||||||
exports.expressCreateServer = (hookName, args, cb) => {
|
exports.expressCreateServer = (hookName, args, cb) => {
|
||||||
|
@ -74,23 +77,24 @@ exports.expressCreateServer = (hookName, args, cb) => {
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
args.app.get('/favicon.ico', (req, res) => {
|
args.app.get('/favicon.ico', (req, res, next) => {
|
||||||
let filePath = path.join(
|
(async () => {
|
||||||
settings.root,
|
const fns = [
|
||||||
'src',
|
path.join(settings.root, 'src', 'static', 'skins', settings.skinName, 'favicon.ico'),
|
||||||
'static',
|
path.join(settings.root, 'src', 'static', 'favicon.ico'),
|
||||||
'skins',
|
];
|
||||||
settings.skinName,
|
for (const fn of fns) {
|
||||||
'favicon.ico'
|
try {
|
||||||
);
|
await fsp.access(fn, fs.constants.R_OK);
|
||||||
res.setHeader('Cache-Control', `public, max-age=${settings.maxAge}`);
|
} catch (err) {
|
||||||
res.sendFile(filePath, (err) => {
|
continue;
|
||||||
// there is no custom favicon, send the default favicon
|
}
|
||||||
if (err) {
|
res.setHeader('Cache-Control', `public, max-age=${settings.maxAge}`);
|
||||||
filePath = path.join(settings.root, 'src', 'static', 'favicon.ico');
|
await util.promisify(res.sendFile.bind(res))(fn);
|
||||||
res.sendFile(filePath);
|
return;
|
||||||
}
|
}
|
||||||
});
|
next();
|
||||||
|
})().catch((err) => next(err || new Error(err)));
|
||||||
});
|
});
|
||||||
|
|
||||||
return cb();
|
return cb();
|
||||||
|
|
BIN
src/tests/backend/specs/favicon-test-skin.png
Normal file
BIN
src/tests/backend/specs/favicon-test-skin.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 419 B |
55
src/tests/backend/specs/favicon.js
Normal file
55
src/tests/backend/specs/favicon.js
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const assert = require('assert').strict;
|
||||||
|
const common = require('../common');
|
||||||
|
const fs = require('fs');
|
||||||
|
const fsp = fs.promises;
|
||||||
|
const path = require('path');
|
||||||
|
const settings = require('../../../node/utils/Settings');
|
||||||
|
const superagent = require('superagent');
|
||||||
|
|
||||||
|
describe(__filename, function () {
|
||||||
|
let agent;
|
||||||
|
let backupSettings;
|
||||||
|
let skinDir;
|
||||||
|
let wantDefaultIcon;
|
||||||
|
let wantSkinIcon;
|
||||||
|
|
||||||
|
before(async function () {
|
||||||
|
agent = await common.init();
|
||||||
|
wantDefaultIcon = await fsp.readFile(path.join(settings.root, 'src', 'static', 'favicon.ico'));
|
||||||
|
wantSkinIcon = await fsp.readFile(path.join(__dirname, 'favicon-test-skin.png'));
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(async function () {
|
||||||
|
backupSettings = {...settings};
|
||||||
|
skinDir = await fsp.mkdtemp(path.join(settings.root, 'src', 'static', 'skins', 'test-'));
|
||||||
|
settings.skinName = path.basename(skinDir);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async function () {
|
||||||
|
delete settings.skinName;
|
||||||
|
Object.assign(settings, backupSettings);
|
||||||
|
try {
|
||||||
|
// TODO: The {recursive: true} option wasn't added to fsp.rmdir() until Node.js v12.10.0 so we
|
||||||
|
// can't rely on it until support for Node.js v10 is dropped.
|
||||||
|
await fsp.unlink(path.join(skinDir, 'favicon.ico'));
|
||||||
|
await fsp.rmdir(skinDir, {recursive: true});
|
||||||
|
} catch (err) { /* intentionally ignored */ }
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses skin favicon if present', async function () {
|
||||||
|
await fsp.writeFile(path.join(skinDir, 'favicon.ico'), wantSkinIcon);
|
||||||
|
const {body: gotIcon} = await agent.get('/favicon.ico')
|
||||||
|
.accept('png').buffer(true).parse(superagent.parse.image)
|
||||||
|
.expect(200);
|
||||||
|
assert(gotIcon.equals(wantSkinIcon));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('falls back to default favicon', async function () {
|
||||||
|
const {body: gotIcon} = await agent.get('/favicon.ico')
|
||||||
|
.accept('png').buffer(true).parse(superagent.parse.image)
|
||||||
|
.expect(200);
|
||||||
|
assert(gotIcon.equals(wantDefaultIcon));
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue