From 626369e3e00afd1d672636a0b949ecb52bdb6f6e Mon Sep 17 00:00:00 2001 From: curbengh <43627182+curbengh@users.noreply.github.com> Date: Fri, 27 Dec 2019 05:39:29 +0000 Subject: [PATCH] test(gzip): add unit test --- test/filter.test.js | 75 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/test/filter.test.js b/test/filter.test.js index 662ae60..2e089cf 100644 --- a/test/filter.test.js +++ b/test/filter.test.js @@ -1,6 +1,7 @@ /* eslint-env jest */ 'use strict' +const { promisify } = require('util') const Hexo = require('hexo') const hexo = new Hexo(__dirname) global.hexo = hexo @@ -376,3 +377,77 @@ describe('svg', () => { }) }) }) + +describe('gzip', () => { + const { gzipDefault } = require('../index') + const g = require('../lib/filter').gzipFn.bind(hexo) + const zlib = require('zlib') + const gzip = promisify(zlib.gzip) + const unzip = promisify(zlib.unzip) + const path = 'foo.txt' + const input = 'Lorem ipsum dolor sit amet consectetur adipiscing elit fusce' + + beforeEach(() => { + hexo.config.minify.gzip = Object.assign({}, gzipDefault) + hexo.route.set(path, input) + }) + + afterEach(() => { + const routeList = hexo.route.list() + routeList.forEach((path) => hexo.route.remove(path)) + }) + + test('default', async () => { + await g() + + const output = hexo.route.get(path.concat('.gz')) + const buf = [] + output.on('data', (chunk) => (buf.push(chunk))) + output.on('end', async () => { + const result = Buffer.concat(buf) + const expected = await gzip(input, { level: zlib.constants.Z_BEST_COMPRESSION }) + const resultUnzip = await unzip(result) + const expectedUnzip = await unzip(expected) + + expect(result.toString('base64')).toBe(Buffer.from(expected, 'binary').toString('base64')) + expect(resultUnzip.toString()).toBe(input) + expect(expectedUnzip.toString()).toBe(input) + }) + }) + + test('disable', async () => { + hexo.config.minify.gzip.enable = false + const result = await g() + + expect(result).toBeUndefined() + }) + + test('include - exclude non-text file by default', async () => { + const path = 'foo.jpg' + hexo.route.set(path, input) + await g() + + const result = hexo.route.get(path.concat('.gz')) + expect(result).toBeUndefined() + }) + + test('include - basename', async () => { + hexo.config.minify.gzip.include = 'bar.txt' + const fooPath = 'foo/bar.txt' + hexo.route.set(fooPath, input) + await g() + + const result = hexo.route.get(fooPath.concat('.gz')) + expect(result).toBeDefined() + }) + + test('include - slash in pattern', async () => { + hexo.config.minify.gzip.include = '**/foo/*.txt' + const fooPath = 'blog/site/example/foo/bar.txt' + hexo.route.set(fooPath, input) + await g() + + const result = hexo.route.get(fooPath.concat('.gz')) + expect(result).toBeDefined() + }) +})