diff --git a/package-lock.json b/package-lock.json index 8073c42..d671df8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "pactum", - "version": "3.3.0", + "version": "3.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -574,9 +574,9 @@ "dev": true }, "centra": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/centra/-/centra-2.5.0.tgz", - "integrity": "sha512-CnSF1HD8vOOgNbE4P2fZEhdhfAohvpcF3DSdSvEcSHDAZvr+Xfw73isT8SXJJc3VMBqSwjXhr29/ikHUgFcypg==" + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/centra/-/centra-2.6.0.tgz", + "integrity": "sha512-dgh+YleemrT8u85QL11Z6tYhegAs3MMxsaWAq/oXeAmYJ7VxL3SI9TZtnfaEvNDMAPolj25FXIb3S+HCI4wQaQ==" }, "chai": { "version": "4.3.6", @@ -1718,11 +1718,11 @@ "dev": true }, "phin": { - "version": "3.6.1", - "resolved": "https://registry.npmjs.org/phin/-/phin-3.6.1.tgz", - "integrity": "sha512-ubcFKk2jRr2WUJEh+QpMkCgOQansJDuIShKbsjpejeyI/aOMLrAf0TaisbMjKwTq9CsypFFJqUaN9ZSRmBJLPw==", + "version": "3.7.0", + "resolved": "https://registry.npmjs.org/phin/-/phin-3.7.0.tgz", + "integrity": "sha512-DqnVNrpYhKGBZppNKprD+UJylMeEKOZxHgPB+ZP6mGzf3uA2uox4Ep9tUm+rUc8WLIdHT3HcAE4X8fhwQA9JKg==", "requires": { - "centra": "^2.4.2" + "centra": "^2.6.0" } }, "picomatch": { diff --git a/package.json b/package.json index d02a9e8..6c72734 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pactum", - "version": "3.3.0", + "version": "3.3.1", "description": "REST API Testing Tool for all levels in a Test Pyramid", "main": "./src/index.js", "types": "./src/index.d.ts", @@ -61,7 +61,7 @@ "openapi-fuzzer-core": "^1.0.6", "pactum-matchers": "^1.1.3", "parse-graphql": "^1.0.0", - "phin": "^3.6.1", + "phin": "^3.7.0", "polka": "^0.5.2" }, "devDependencies": { diff --git a/src/exports/handler.d.ts b/src/exports/handler.d.ts index dc6de2d..f435e05 100644 --- a/src/exports/handler.d.ts +++ b/src/exports/handler.d.ts @@ -2,14 +2,14 @@ import * as Spec from '../models/Spec'; import { Interaction } from './mock'; import { IncomingMessage } from 'http'; -interface Request { +interface PactumRequest { url: string; method: string; timeout: number; data?: any; } -interface Response extends IncomingMessage { +interface PactumResponse extends IncomingMessage { json?: object; body?: any; text?: string; @@ -27,8 +27,8 @@ interface SpecHandlerContext { } interface RequestResponseContext { - req: Request; - res: Response; + req: PactumRequest; + res: PactumResponse; } interface CaptureContext extends RequestResponseContext { diff --git a/src/models/Spec.d.ts b/src/models/Spec.d.ts index f46d2c5..4ef47aa 100644 --- a/src/models/Spec.d.ts +++ b/src/models/Spec.d.ts @@ -207,6 +207,12 @@ declare class Spec { */ withFollowRedirects(follow: boolean): Spec; + /** + * enables compression + * @see https://pactumjs.github.io/api/requests/withCompression.html + */ + withCompression(): Spec; + /** * retry request on specific conditions * @see https://pactumjs.github.io/api/requests/retry.html diff --git a/src/models/Spec.js b/src/models/Spec.js index 0f37e3d..cd2425b 100644 --- a/src/models/Spec.js +++ b/src/models/Spec.js @@ -293,6 +293,11 @@ class Spec { return this; } + withCompression() { + this._request.compression = true; + return this; + } + retry(options, delay) { if (typeof options === 'undefined' || typeof options === 'number') { options = { count: options, delay: delay }; diff --git a/test/component/withCompression.spec.js b/test/component/withCompression.spec.js new file mode 100644 index 0000000..522249d --- /dev/null +++ b/test/component/withCompression.spec.js @@ -0,0 +1,61 @@ +const pactum = require('../../src/index'); +const expect = require('chai').expect; + +describe('withCompression', () => { + + describe('should process', () => { + + it('with encoding as br', async () => { + await pactum.spec() + .useInteraction({ + request: { + method: 'GET', + path: '/api/compression', + }, + response: { + status: 200, + headers: { + 'content-encoding': 'br' + }, + file: 'test/data/files/data.txt.br' + } + }) + .get('http://localhost:9393/api/compression') + .withCompression() + .expectStatus(200); + }); + + }); + + describe('should not process', () => { + + it('without a response body', async () => { + let err; + try { + await pactum.spec() + .useLogLevel('SILENT') + .useInteraction({ + request: { + method: 'GET', + path: '/api/compression', + }, + response: { + status: 200, + headers: { + 'content-encoding': 'br' + } + } + }) + .get('http://localhost:9393/api/compression') + .withCompression() + + .expectStatus(200); + } catch (error) { + err = error; + } + expect(err.message).contains('unexpected end of file'); + }); + + }); + +}); \ No newline at end of file diff --git a/test/data/files/convert.js b/test/data/files/convert.js new file mode 100644 index 0000000..0c104ed --- /dev/null +++ b/test/data/files/convert.js @@ -0,0 +1,13 @@ +const fs = require('fs'); +const zlib = require('zlib'); + +const READ_FILE_NAME = 'data.txt'; +const WRITE_FILE_NAME = 'data.txt.br'; + +const read_stream = fs.createReadStream(READ_FILE_NAME); +const write_stream = fs.createWriteStream(WRITE_FILE_NAME); + +const compress = zlib.createBrotliCompress(); + + +read_stream.pipe(compress).pipe(write_stream); \ No newline at end of file diff --git a/test/data/files/data.txt.br b/test/data/files/data.txt.br new file mode 100644 index 0000000..27f2d15 --- /dev/null +++ b/test/data/files/data.txt.br @@ -0,0 +1 @@ +‹€Hello World! \ No newline at end of file