From aae7a2df94c2a31bdeae7629190fc7de394d37ff Mon Sep 17 00:00:00 2001 From: Aleksei Androsov Date: Mon, 29 Apr 2024 13:36:25 +0300 Subject: [PATCH] Add jest matchers for gzip (#61) --- jest.config.js | 4 ++++ tests/expect.js | 19 +++++++++++++++++++ tests/http_block.test.js | 5 ++--- tests/request.test.js | 16 +++++----------- 4 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 tests/expect.js diff --git a/jest.config.js b/jest.config.js index 45d4e10..f8986e7 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,9 +1,13 @@ module.exports = { coveragePathIgnorePatterns: [ '/node_modules/', + '/tests/expect.js', '/tests/server.js', '/tests/helpers.js', ], + setupFilesAfterEnv: [ + './tests/expect.js', + ], testEnvironment: 'node', testRunner: 'jest-circus/runner', }; diff --git a/tests/expect.js b/tests/expect.js new file mode 100644 index 0000000..18c7aab --- /dev/null +++ b/tests/expect.js @@ -0,0 +1,19 @@ +const { gunzipSync } = require( 'node:zlib' ); + +expect.extend( { + toBeValidGzip( received ) { + // http://www.zlib.org/rfc-gzip.html#header-trailer + // 2.3.1. Member header and trailer + // These have the fixed values ID1 = 31 (0x1f), ID2 = 139 (0x8b), to identify the file as being in gzip format. + return { + message: () => `incorrect header check for ${ received }`, + pass: received.slice( 0, 2 ).equals( Buffer.from( '1f8b', 'hex' ) ), + }; + }, + toHaveUngzipValue( received, value ) { + return { + message: () => `expected ${ received } to be ${ value }`, + pass: gunzipSync( received ).toString( 'utf-8' ) === value, + }; + }, +} ); diff --git a/tests/http_block.test.js b/tests/http_block.test.js index 296504c..e76e64c 100644 --- a/tests/http_block.test.js +++ b/tests/http_block.test.js @@ -9,7 +9,6 @@ const Server = require( './server' ); const { get_path, get_result_block } = require( './helpers' ); const strip_null_and_undefined_values = require( '../lib/strip_null_and_undefined_values' ); -const { gunzipSync } = require( 'node:zlib' ); // --------------------------------------------------------------------------------------------------------------- // @@ -857,9 +856,9 @@ describe( 'http', () => { expect( spy ).toHaveBeenCalledTimes( 1 ); const body = spy.mock.calls[ 0 ][ 2 ]; - expect( body.slice( 0, 2 ).equals( Buffer.from( '1f8b', 'hex' ) ) ).toBe( true ); + expect( body ).toBeValidGzip(); expect( body ).toHaveLength( 34 ); - expect( gunzipSync( body ).toString( 'utf-8' ) ).toBe( 'Привет!' ); + expect( body ).toHaveUngzipValue( 'Привет!' ); } ); } ); diff --git a/tests/request.test.js b/tests/request.test.js index 41b8c3e..86345ce 100644 --- a/tests/request.test.js +++ b/tests/request.test.js @@ -4,7 +4,7 @@ const path_ = require( 'path' ); const fs_ = require( 'fs' ); const http_ = require( 'http' ); const https_ = require( 'https' ); -const { gzipSync, gunzipSync } = require( 'node:zlib' ); +const { gzipSync } = require( 'node:zlib' ); const { compress } = require( '@fengkx/zstd-napi' ); const { Duplex } = require( 'stream' ); @@ -378,12 +378,9 @@ describe( 'request', () => { expect( req.headers ).toHaveProperty( 'transfer-encoding', 'chunked' ); expect( req.headers ).not.toHaveProperty( 'content-length' ); - // http://www.zlib.org/rfc-gzip.html#header-trailer - // 2.3.1. Member header and trailer - // These have the fixed values ID1 = 31 (0x1f), ID2 = 139 (0x8b), to identify the file as being in gzip format. - expect( body.slice( 0, 2 ).equals( Buffer.from( '1f8b', 'hex' ) ) ).toBe( true ); + expect( body ).toBeValidGzip(); expect( body ).toHaveLength( 77 ); - expect( gunzipSync( body ).toString( 'utf-8' ) ).toBe( BODY ); + expect( body ).toHaveUngzipValue( BODY ); } ); it.each( [ 'POST' ] )( '%j, body_compress with options', async ( method ) => { @@ -412,12 +409,9 @@ describe( 'request', () => { expect( req.headers ).toHaveProperty( 'transfer-encoding', 'chunked' ); expect( req.headers ).not.toHaveProperty( 'content-length' ); - // http://www.zlib.org/rfc-gzip.html#header-trailer - // 2.3.1. Member header and trailer - // These have the fixed values ID1 = 31 (0x1f), ID2 = 139 (0x8b), to identify the file as being in gzip format. - expect( body.slice( 0, 2 ).equals( Buffer.from( '1f8b', 'hex' ) ) ).toBe( true ); + expect( body ).toBeValidGzip(); expect( body ).toHaveLength( 134 ); - expect( gunzipSync( body ).toString( 'utf-8' ) ).toBe( BODY ); + expect( body ).toHaveUngzipValue( BODY ); } ); describe( 'errors', () => {