Skip to content

Commit

Permalink
Merge pull request #177 from pactumjs/176-mock-files
Browse files Browse the repository at this point in the history
176 mock files
  • Loading branch information
ASaiAnudeep authored Jul 6, 2022
2 parents 36f52eb + f8d7b21 commit cb9fbb6
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.1.10",
"version": "3.1.11",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions src/exports/mock.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface InteractionResponse {
status: number;
headers?: object;
body?: object;
file?: string;
fixedDelay?: number;
randomDelay?: RandomDelay;
}
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/requestProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ function setMultiPartFormData(request) {
request.headers = multiPartHeaders;
} else {
for (const prop in multiPartHeaders) {
request.headers[prop] = multiPartHeaders[prop];
if (request.headers[prop] === undefined) {
request.headers[prop] = multiPartHeaders[prop];
}
}
}
delete request._multiPartFormData;
Expand Down
3 changes: 3 additions & 0 deletions src/models/Interaction.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class InteractionResponse {
} else if (response.randomDelay) {
this.delay = new InteractionResponseDelay('RANDOM', response.randomDelay);
}
if (response.file) {
this.file = getValue(response.file);
}
}
}

Expand Down
36 changes: 29 additions & 7 deletions src/models/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const polka = require('polka');
const fs = require('fs');
const mime = require('mime-lite')
const fs_path = require('path');

const Interaction = require('./Interaction.model');
const helper = require('../helpers/helper');
Expand Down Expand Up @@ -138,9 +141,9 @@ function sendInteractionFoundResponse(req, res, interaction) {
res.status(_response.status);
const delay = getDelay(_response);
if (delay > 0) {
setTimeout(() => sendResponseBody(res, _response.body), delay);
setTimeout(() => sendResponse(res, _response), delay);
} else {
sendResponseBody(res, _response.body);
sendResponse(res, _response);
}
}
interaction.callCount += 1;
Expand All @@ -164,11 +167,13 @@ function updateCalls(req, interaction) {
/**
* sends response body
* @param {ExpressResponse} res - HTTP response
* @param {object} body - response body to be sent
* @param {object} interaction_response - response body to be sent
*/
function sendResponseBody(res, body) {
if (body) {
res.send(body);
function sendResponse(res, interaction_response) {
if (interaction_response.file) {
res.download(interaction_response);
} else if (interaction_response.body) {
res.send(interaction_response.body);
} else {
res.send();
}
Expand Down Expand Up @@ -392,7 +397,10 @@ class ExpressResponse {
send(data) {
if (data) {
if (typeof data === 'object') {
this.res.setHeader('Content-Type', 'application/json');
const header_keys = this.res.getHeaderNames();
if (!header_keys.includes('content-type')) {
this.res.setHeader('Content-Type', 'application/json');
}
this.res.end(JSON.stringify(data));
} else {
this.res.end(data);
Expand All @@ -401,6 +409,20 @@ class ExpressResponse {
this.res.end();
}
}

download(interaction_response) {
if (interaction_response.headers) {
const header_keys = Object.keys(interaction_response.headers).map(_ => _.toLowerCase());
if (!header_keys.includes('content-type')) {
const file_name = fs_path.basename(interaction_response.file)
this.res.setHeader('content-type', mime.getType(file_name));
}
} else {
const file_name = fs_path.basename(interaction_response.file)
this.res.setHeader('content-type', mime.getType(file_name));
}
fs.createReadStream(interaction_response.file).pipe(this.res);
}
}

module.exports = Server;
9 changes: 9 additions & 0 deletions test/component/files.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ describe('Files', () => {
.expectStatus(200);
});

it('with file - custom content-type header', async () => {
await pactum.spec()
.useInteraction('post file')
.post('http://localhost:9393/api/file')
.withFile('./package.json')
.withHeaders('content-type', 'any')
.expectStatus(200);
});

});
44 changes: 44 additions & 0 deletions test/component/interactions.file.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const pactum = require('../../src/index');

describe('Interactions - File', () => {

it('download file with default headers', async () => {
await pactum.spec()
.useInteraction({
request: {
method: 'GET',
path: '/download'
},
response: {
status: 200,
file: 'assets/logo.png'
}
})
.get('http://localhost:9393/download')
.expectStatus(200)
.expectBodyContains('PNG')
.expectHeader('content-type', 'image/png');
});

it('download file with custom headers', async () => {
await pactum.spec()
.useInteraction({
request: {
method: 'GET',
path: '/download'
},
response: {
status: 200,
headers: {
'content-type': 'application/json'
},
file: 'assets/logo.png'
}
})
.get('http://localhost:9393/download')
.expectStatus(200)
.expectBodyContains('PNG')
.expectHeader('content-type', 'application/json');
});

});
27 changes: 27 additions & 0 deletions test/component/interactions.mock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,31 @@ describe('Interactions - Not Strict - Follow Redirects', () => {
pactum.request.setDefaultFollowRedirects(false);
});

});

describe('Interactions - Response - Headers', () => {

it('with custom content-type headers', async () => {
await pactum.spec()
.useInteraction({
request: {
method: 'GET',
path: '/custom/header'
},
response: {
status: 200,
headers: {
'content-type': 'any'
},
body: {
message: 'hello'
}
}
})
.get('http://localhost:9393/custom/header')
.expectStatus(200)
.expectHeader('content-type', 'any')
.expectJson('message', 'hello');
});

});
1 change: 0 additions & 1 deletion test/component/interactions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ describe('Pact - VALID', () => {

});


describe('Interactions - expects skip check', () => {

it('skip expects - defaults', async () => {
Expand Down

0 comments on commit cb9fbb6

Please sign in to comment.