mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-22 16:36:15 -04:00
tests: Refine CachingMiddleware tests
* Lint functions * Fix assignment of `settings.minify` * Use a for loop to avoid copied code for the `minify = true` and `minify = false` cases * Put each resource fetch into its own test case * Check for 200 status code * Use `.expect()` to check header value * Use `.expect(fn)` instead of `.then(fn)`
This commit is contained in:
parent
f86df5322e
commit
3a34db84e6
1 changed files with 56 additions and 87 deletions
|
@ -22,7 +22,7 @@ let agent;
|
||||||
* @param {URI} resource resource URI
|
* @param {URI} resource resource URI
|
||||||
* @returns {boolean} if it is plaintext
|
* @returns {boolean} if it is plaintext
|
||||||
*/
|
*/
|
||||||
function isPlaintextResponse(fileContent, resource) {
|
const isPlaintextResponse = (fileContent, resource) => {
|
||||||
// callback=require.define&v=1234
|
// callback=require.define&v=1234
|
||||||
const query = (new URL(resource, 'http://localhost')).search.slice(1);
|
const query = (new URL(resource, 'http://localhost')).search.slice(1);
|
||||||
// require.define
|
// require.define
|
||||||
|
@ -30,18 +30,16 @@ function isPlaintextResponse(fileContent, resource) {
|
||||||
|
|
||||||
// returns true if the first letters in fileContent equal the content of `jsonp`
|
// returns true if the first letters in fileContent equal the content of `jsonp`
|
||||||
return fileContent.substring(0, jsonp.length) === jsonp;
|
return fileContent.substring(0, jsonp.length) === jsonp;
|
||||||
}
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A hack to disable `superagent`'s auto unzip functionality
|
* A hack to disable `superagent`'s auto unzip functionality
|
||||||
*
|
*
|
||||||
* @param {Request} request
|
* @param {Request} request
|
||||||
*/
|
*/
|
||||||
function disableAutoDeflate(request) {
|
const disableAutoDeflate = (request) => {
|
||||||
request._shouldUnzip = function () {
|
request._shouldUnzip = () => false;
|
||||||
return false;
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe(__filename, function () {
|
describe(__filename, function () {
|
||||||
const backups = {};
|
const backups = {};
|
||||||
|
@ -55,94 +53,65 @@ describe(__filename, function () {
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
agent = await common.init();
|
agent = await common.init();
|
||||||
});
|
|
||||||
beforeEach(async function () {
|
|
||||||
backups.settings = {};
|
backups.settings = {};
|
||||||
backups.settings.minify = settings.minify;
|
backups.settings.minify = settings.minify;
|
||||||
});
|
});
|
||||||
afterEach(async function () {
|
after(async function () {
|
||||||
Object.assign(settings, backups.settings);
|
Object.assign(settings, backups.settings);
|
||||||
});
|
});
|
||||||
|
|
||||||
context('when minify is false', function () {
|
for (const minify of [false, true]) {
|
||||||
|
context(`when minify is ${minify}`, function () {
|
||||||
before(async function () {
|
before(async function () {
|
||||||
settings.minify = false;
|
settings.minify = minify;
|
||||||
});
|
|
||||||
it('gets packages uncompressed without Accept-Encoding gzip', async function () {
|
|
||||||
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
|
||||||
.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;
|
|
||||||
})));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('gets packages compressed with Accept-Encoding gzip', async function () {
|
describe('gets packages uncompressed without Accept-Encoding gzip', function () {
|
||||||
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
for (const resource of packages) {
|
||||||
|
it(resource, async function () {
|
||||||
|
await agent.get(resource)
|
||||||
|
.set('Accept-Encoding', fantasyEncoding)
|
||||||
|
.use(disableAutoDeflate)
|
||||||
|
.expect(200)
|
||||||
|
.expect('Content-Type', /application\/javascript/)
|
||||||
|
.expect((res) => {
|
||||||
|
assert.equal(res.header['content-encoding'], undefined);
|
||||||
|
assert(isPlaintextResponse(res.text, resource));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('gets packages compressed with Accept-Encoding gzip', function () {
|
||||||
|
for (const resource of packages) {
|
||||||
|
it(resource, async function () {
|
||||||
|
await agent.get(resource)
|
||||||
.set('Accept-Encoding', 'gzip')
|
.set('Accept-Encoding', 'gzip')
|
||||||
.use(disableAutoDeflate)
|
.use(disableAutoDeflate)
|
||||||
.then((res) => {
|
.expect(200)
|
||||||
assert.match(res.header['content-type'], /application\/javascript/);
|
.expect('Content-Type', /application\/javascript/)
|
||||||
assert.equal(res.header['content-encoding'], 'gzip');
|
.expect('Content-Encoding', 'gzip')
|
||||||
assert.equal(isPlaintextResponse(res.text, resource), false);
|
.expect((res) => {
|
||||||
return;
|
assert(!isPlaintextResponse(res.text, resource));
|
||||||
})));
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not cache content-encoding headers', async function () {
|
it('does not cache content-encoding headers', async function () {
|
||||||
await agent.get(packages[0])
|
await agent.get(packages[0])
|
||||||
.set('Accept-Encoding', fantasyEncoding)
|
.set('Accept-Encoding', fantasyEncoding)
|
||||||
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
.expect(200)
|
||||||
|
.expect((res) => assert.equal(res.header['content-encoding'], undefined));
|
||||||
await agent.get(packages[0])
|
await agent.get(packages[0])
|
||||||
.set('Accept-Encoding', 'gzip')
|
.set('Accept-Encoding', 'gzip')
|
||||||
.then((res) => assert.equal(res.header['content-encoding'], 'gzip'));
|
.expect(200)
|
||||||
|
.expect('Content-Encoding', 'gzip');
|
||||||
await agent.get(packages[0])
|
await agent.get(packages[0])
|
||||||
.set('Accept-Encoding', fantasyEncoding)
|
.set('Accept-Encoding', fantasyEncoding)
|
||||||
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
.expect(200)
|
||||||
});
|
.expect((res) => assert.equal(res.header['content-encoding'], undefined));
|
||||||
});
|
|
||||||
|
|
||||||
context('when minify is true', function () {
|
|
||||||
before(async function () {
|
|
||||||
settings.minify = true;
|
|
||||||
});
|
|
||||||
it('gets packages uncompressed without Accept-Encoding gzip', async function () {
|
|
||||||
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
|
||||||
.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;
|
|
||||||
})));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('gets packages compressed with Accept-Encoding gzip', async function () {
|
|
||||||
await Promise.all(packages.map(async (resource) => agent.get(resource)
|
|
||||||
.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;
|
|
||||||
})));
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not cache content-encoding headers', async function () {
|
|
||||||
await agent.get(packages[0])
|
|
||||||
.set('Accept-Encoding', fantasyEncoding)
|
|
||||||
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
|
||||||
await agent.get(packages[0])
|
|
||||||
.set('Accept-Encoding', 'gzip')
|
|
||||||
.then((res) => assert.equal(res.header['content-encoding'], 'gzip'));
|
|
||||||
await agent.get(packages[0])
|
|
||||||
.set('Accept-Encoding', fantasyEncoding)
|
|
||||||
.then((res) => assert.equal(res.header['content-encoding'], undefined));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue