2020-11-22 09:23:33 +00:00
|
|
|
/**
|
|
|
|
* caching_middleware is responsible for serving everything under path `/javascripts/`
|
|
|
|
* That includes packages as defined in `src/node/utils/tar.json` and probably also plugin code
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const settings = require('../../../src/node/utils/Settings');
|
|
|
|
const assert = require('assert').strict;
|
|
|
|
const url = require('url');
|
|
|
|
const queryString = require('querystring');
|
|
|
|
|
|
|
|
let agent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hack! Returns true if the resource is not plaintext
|
|
|
|
* The file should start with the callback method, so we need the
|
|
|
|
* URL.
|
|
|
|
*
|
|
|
|
* @param {string} fileContent the response body
|
|
|
|
* @param {URI} resource resource URI
|
2020-11-23 18:21:51 +00:00
|
|
|
* @returns {boolean} if it is plaintext
|
2020-11-22 09:23:33 +00:00
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
function isPlaintextResponse(fileContent, resource) {
|
2020-11-22 09:23:33 +00:00
|
|
|
// callback=require.define&v=1234
|
2020-11-23 18:21:51 +00:00
|
|
|
const query = url.parse(resource).query;
|
2020-11-22 09:23:33 +00:00
|
|
|
// require.define
|
2020-11-23 18:21:51 +00:00
|
|
|
const jsonp = queryString.parse(query).callback;
|
2020-11-22 09:23:33 +00:00
|
|
|
|
|
|
|
// returns true if the first letters in fileContent equal the content of `jsonp`
|
|
|
|
return fileContent.substring(0, jsonp.length) === jsonp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A hack to disable `superagent`'s auto unzip functionality
|
|
|
|
*
|
|
|
|
* @param {Request} request
|
|
|
|
*/
|
2020-11-23 18:21:51 +00:00
|
|
|
function disableAutoDeflate(request) {
|
|
|
|
request._shouldUnzip = function () {
|
|
|
|
return false;
|
|
|
|
};
|
2020-11-22 09:23:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
describe(__filename, function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
const backups = {};
|
|
|
|
const fantasyEncoding = 'brainwaves'; // non-working encoding until https://github.com/visionmedia/superagent/pull/1560 is resolved
|
|
|
|
const packages = [
|
2020-11-23 18:21:51 +00:00
|
|
|
'/javascripts/lib/ep_etherpad-lite/static/js/ace2_common.js?callback=require.define',
|
|
|
|
'/javascripts/lib/ep_etherpad-lite/static/js/ace2_inner.js?callback=require.define',
|
|
|
|
'/javascripts/lib/ep_etherpad-lite/static/js/pad.js?callback=require.define',
|
|
|
|
'/javascripts/lib/ep_etherpad-lite/static/js/timeslider.js?callback=require.define',
|
2020-11-22 09:23:33 +00:00
|
|
|
];
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
before(async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
agent = await common.init();
|
|
|
|
});
|
2020-11-23 18:21:51 +00:00
|
|
|
beforeEach(async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
backups.settings = {};
|
2020-11-23 18:21:51 +00:00
|
|
|
backups.settings.minify = settings.minify;
|
2020-11-22 09:23:33 +00:00
|
|
|
});
|
2020-11-23 18:21:51 +00:00
|
|
|
afterEach(async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
Object.assign(settings, backups.settings);
|
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
context('when minify is false', function () {
|
|
|
|
before(async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
settings.minify = false;
|
|
|
|
});
|
2020-11-23 18:21:51 +00:00
|
|
|
it('gets packages uncompressed without Accept-Encoding gzip', async function () {
|
|
|
|
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
2020-11-22 09:23:33 +00:00
|
|
|
.set('Accept-Encoding', fantasyEncoding)
|
|
|
|
.use(disableAutoDeflate)
|
|
|
|
.then((res) => {
|
|
|
|
assert.match(res.header['content-type'], /application\/javascript/);
|
|
|
|
assert.equal(res.header['content-encoding'], undefined);
|
|
|
|
assert.equal(isPlaintextResponse(res.text, resource), true);
|
|
|
|
return;
|
2020-11-23 18:21:51 +00:00
|
|
|
})));
|
|
|
|
});
|
2020-11-22 09:23:33 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('gets packages compressed with Accept-Encoding gzip', async function () {
|
|
|
|
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
2020-11-22 09:23:33 +00:00
|
|
|
.set('Accept-Encoding', 'gzip')
|
|
|
|
.use(disableAutoDeflate)
|
|
|
|
.then((res) => {
|
|
|
|
assert.match(res.header['content-type'], /application\/javascript/);
|
|
|
|
assert.equal(res.header['content-encoding'], 'gzip');
|
|
|
|
assert.equal(isPlaintextResponse(res.text, resource), false);
|
|
|
|
return;
|
2020-11-23 18:21:51 +00:00
|
|
|
})));
|
|
|
|
});
|
2020-11-22 09:23:33 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('does not cache content-encoding headers', async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
await agent.get(packages[0])
|
2020-11-23 18:21:51 +00:00
|
|
|
.set('Accept-Encoding', fantasyEncoding)
|
|
|
|
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
2020-11-22 09:23:33 +00:00
|
|
|
await agent.get(packages[0])
|
2020-11-23 18:21:51 +00:00
|
|
|
.set('Accept-Encoding', 'gzip')
|
|
|
|
.then((res) => assert.equal(res.header['content-encoding'], 'gzip'));
|
2020-11-22 09:23:33 +00:00
|
|
|
await agent.get(packages[0])
|
2020-11-23 18:21:51 +00:00
|
|
|
.set('Accept-Encoding', fantasyEncoding)
|
|
|
|
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
|
|
|
});
|
2020-11-22 09:23:33 +00:00
|
|
|
});
|
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
context('when minify is true', function () {
|
|
|
|
before(async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
settings.minify = true;
|
|
|
|
});
|
2020-11-23 18:21:51 +00:00
|
|
|
it('gets packages uncompressed without Accept-Encoding gzip', async function () {
|
|
|
|
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
2020-11-22 09:23:33 +00:00
|
|
|
.set('Accept-Encoding', fantasyEncoding)
|
|
|
|
.use(disableAutoDeflate)
|
|
|
|
.then((res) => {
|
|
|
|
assert.match(res.header['content-type'], /application\/javascript/);
|
|
|
|
assert.equal(res.header['content-encoding'], undefined);
|
|
|
|
assert.equal(isPlaintextResponse(res.text, resource), true);
|
|
|
|
return;
|
2020-11-23 18:21:51 +00:00
|
|
|
})));
|
|
|
|
});
|
2020-11-22 09:23:33 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('gets packages compressed with Accept-Encoding gzip', async function () {
|
|
|
|
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
2020-11-22 09:23:33 +00:00
|
|
|
.set('Accept-Encoding', 'gzip')
|
|
|
|
.use(disableAutoDeflate)
|
|
|
|
.then((res) => {
|
|
|
|
assert.match(res.header['content-type'], /application\/javascript/);
|
|
|
|
assert.equal(res.header['content-encoding'], 'gzip');
|
|
|
|
assert.equal(isPlaintextResponse(res.text, resource), false);
|
|
|
|
return;
|
2020-11-23 18:21:51 +00:00
|
|
|
})));
|
|
|
|
});
|
2020-11-22 09:23:33 +00:00
|
|
|
|
2020-11-23 18:21:51 +00:00
|
|
|
it('does not cache content-encoding headers', async function () {
|
2020-11-22 09:23:33 +00:00
|
|
|
await agent.get(packages[0])
|
2020-11-23 18:21:51 +00:00
|
|
|
.set('Accept-Encoding', fantasyEncoding)
|
|
|
|
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
2020-11-22 09:23:33 +00:00
|
|
|
await agent.get(packages[0])
|
2020-11-23 18:21:51 +00:00
|
|
|
.set('Accept-Encoding', 'gzip')
|
|
|
|
.then((res) => assert.equal(res.header['content-encoding'], 'gzip'));
|
2020-11-22 09:23:33 +00:00
|
|
|
await agent.get(packages[0])
|
2020-11-23 18:21:51 +00:00
|
|
|
.set('Accept-Encoding', fantasyEncoding)
|
|
|
|
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
|
|
|
});
|
2020-11-22 09:23:33 +00:00
|
|
|
});
|
|
|
|
});
|