mirror of
https://github.com/nodejs/node.git
synced 2025-05-20 22:43:26 +00:00

This completely refactors the `expectsError` behavior: so far it's almost identical to `assert.throws(fn, object)` in case it was used with a function as first argument. It had a magical property check that allowed to verify a functions `type` in case `type` was passed used in the validation object. This pattern is now completely removed and `assert.throws()` should be used instead. The main intent for `common.expectsError()` is to verify error cases for callback based APIs. This is now more flexible by accepting all validation possibilites that `assert.throws()` accepts as well. No magical properties exist anymore. This reduces surprising behavior for developers who are not used to the Node.js core code base. This has the side effect that `common` is used significantly less frequent. PR-URL: https://github.com/nodejs/node/pull/31092 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
'use strict';
|
|
// Test unzipping a gzip file that has trailing garbage
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const zlib = require('zlib');
|
|
|
|
// Should ignore trailing null-bytes
|
|
let data = Buffer.concat([
|
|
zlib.gzipSync('abc'),
|
|
zlib.gzipSync('def'),
|
|
Buffer.alloc(10)
|
|
]);
|
|
|
|
assert.strictEqual(zlib.gunzipSync(data).toString(), 'abcdef');
|
|
|
|
zlib.gunzip(data, common.mustCall((err, result) => {
|
|
assert.ifError(err);
|
|
assert.strictEqual(
|
|
result.toString(),
|
|
'abcdef',
|
|
`result '${result.toString()}' should match original string`
|
|
);
|
|
}));
|
|
|
|
// If the trailing garbage happens to look like a gzip header, it should
|
|
// throw an error.
|
|
data = Buffer.concat([
|
|
zlib.gzipSync('abc'),
|
|
zlib.gzipSync('def'),
|
|
Buffer.from([0x1f, 0x8b, 0xff, 0xff]),
|
|
Buffer.alloc(10)
|
|
]);
|
|
|
|
assert.throws(
|
|
() => zlib.gunzipSync(data),
|
|
/^Error: unknown compression method$/
|
|
);
|
|
|
|
zlib.gunzip(data, common.mustCall((err, result) => {
|
|
common.expectsError({
|
|
code: 'Z_DATA_ERROR',
|
|
name: 'Error',
|
|
message: 'unknown compression method'
|
|
})(err);
|
|
assert.strictEqual(result, undefined);
|
|
}));
|
|
|
|
// In this case the trailing junk is too short to be a gzip segment
|
|
// So we ignore it and decompression succeeds.
|
|
data = Buffer.concat([
|
|
zlib.gzipSync('abc'),
|
|
zlib.gzipSync('def'),
|
|
Buffer.from([0x1f, 0x8b, 0xff, 0xff])
|
|
]);
|
|
|
|
assert.throws(
|
|
() => zlib.gunzipSync(data),
|
|
/^Error: unknown compression method$/
|
|
);
|
|
|
|
zlib.gunzip(data, common.mustCall((err, result) => {
|
|
assert(err instanceof Error);
|
|
assert.strictEqual(err.code, 'Z_DATA_ERROR');
|
|
assert.strictEqual(err.message, 'unknown compression method');
|
|
assert.strictEqual(result, undefined);
|
|
}));
|