test(gzip): add unit test

This commit is contained in:
curbengh 2019-12-27 05:39:29 +00:00
parent 7e1ec95f9a
commit 626369e3e0
No known key found for this signature in database
GPG Key ID: 21EA847C35D6E034
1 changed files with 75 additions and 0 deletions

View File

@ -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()
})
})