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:
Richard Hansen 2021-02-27 02:22:57 -05:00 committed by webzwo0i
parent f86df5322e
commit 3a34db84e6

View file

@ -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]) {
before(async function () { context(`when minify is ${minify}`, function () {
settings.minify = false; before(async function () {
}); 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) {
.set('Accept-Encoding', 'gzip') it(resource, async function () {
.use(disableAutoDeflate) await agent.get(resource)
.then((res) => { .set('Accept-Encoding', fantasyEncoding)
assert.match(res.header['content-type'], /application\/javascript/); .use(disableAutoDeflate)
assert.equal(res.header['content-encoding'], 'gzip'); .expect(200)
assert.equal(isPlaintextResponse(res.text, resource), false); .expect('Content-Type', /application\/javascript/)
return; .expect((res) => {
}))); assert.equal(res.header['content-encoding'], undefined);
}); assert(isPlaintextResponse(res.text, resource));
});
});
}
});
it('does not cache content-encoding headers', async function () { describe('gets packages compressed with Accept-Encoding gzip', function () {
await agent.get(packages[0]) for (const resource of packages) {
.set('Accept-Encoding', fantasyEncoding) it(resource, async function () {
.then((res) => assert.equal(res.header['content-encoding'], undefined)); await agent.get(resource)
await agent.get(packages[0]) .set('Accept-Encoding', 'gzip')
.set('Accept-Encoding', 'gzip') .use(disableAutoDeflate)
.then((res) => assert.equal(res.header['content-encoding'], 'gzip')); .expect(200)
await agent.get(packages[0]) .expect('Content-Type', /application\/javascript/)
.set('Accept-Encoding', fantasyEncoding) .expect('Content-Encoding', 'gzip')
.then((res) => assert.equal(res.header['content-encoding'], undefined)); .expect((res) => {
}); assert(!isPlaintextResponse(res.text, resource));
}); });
});
}
});
context('when minify is true', function () { it('does not cache content-encoding headers', async function () {
before(async function () { await agent.get(packages[0])
settings.minify = true; .set('Accept-Encoding', fantasyEncoding)
.expect(200)
.expect((res) => assert.equal(res.header['content-encoding'], undefined));
await agent.get(packages[0])
.set('Accept-Encoding', 'gzip')
.expect(200)
.expect('Content-Encoding', 'gzip');
await agent.get(packages[0])
.set('Accept-Encoding', fantasyEncoding)
.expect(200)
.expect((res) => assert.equal(res.header['content-encoding'], undefined));
});
}); });
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));
});
});
}); });