Skip to content

Commit

Permalink
Merge pull request #248 from pactumjs/226-cannot-find-brotli-compress…
Browse files Browse the repository at this point in the history
…ed-page

feat: compression request option
  • Loading branch information
ASaiAnudeep authored Nov 27, 2022
2 parents 360a4f7 + 689dadf commit 70ccb73
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 14 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions src/exports/handler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,8 +27,8 @@ interface SpecHandlerContext {
}

interface RequestResponseContext {
req: Request;
res: Response;
req: PactumRequest;
res: PactumResponse;
}

interface CaptureContext extends RequestResponseContext {
Expand Down
6 changes: 6 additions & 0 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
61 changes: 61 additions & 0 deletions test/component/withCompression.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});

});

});
13 changes: 13 additions & 0 deletions test/data/files/convert.js
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions test/data/files/data.txt.br
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��Hello World!

0 comments on commit 70ccb73

Please sign in to comment.