Skip to content

Commit

Permalink
Add jest matchers for gzip (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
doochik authored Apr 29, 2024
1 parent 46a4701 commit aae7a2d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -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',
};
Expand Down
19 changes: 19 additions & 0 deletions tests/expect.js
Original file line number Diff line number Diff line change
@@ -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,
};
},
} );
5 changes: 2 additions & 3 deletions tests/http_block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

// --------------------------------------------------------------------------------------------------------------- //

Expand Down Expand Up @@ -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( 'Привет!' );
} );
} );

Expand Down
16 changes: 5 additions & 11 deletions tests/request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );

Expand Down Expand Up @@ -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 ) => {
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit aae7a2d

Please sign in to comment.