Skip to content

Commit

Permalink
Merge pull request #322 from pactumjs/321-read-json-file-from-nested-…
Browse files Browse the repository at this point in the history
…folders-with-file-name

feat: find file in withJson
  • Loading branch information
ASaiAnudeep authored Mar 18, 2024
2 parents 2d54cf0 + c2a60a6 commit 4fc5a20
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 7 deletions.
4 changes: 2 additions & 2 deletions 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.6.2",
"version": "3.6.3",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const config = {
enabled: false,
processed: false,
direct_override: false,
}
},
dir: 'data'
},
strategy: {
assert: {
Expand Down
3 changes: 2 additions & 1 deletion src/exports/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ export function setCaptureHandlerStrategy(strategy: Strategy): void;
export function setSnapshotDirectoryPath(path: string): void;
export function setReporterAutoRun(auto: boolean): void;
export function setRequestDefaultRetryCount(count: number): void;
export function setRequestDefaultRetryDelay(delay: number): void;
export function setRequestDefaultRetryDelay(delay: number): void;
export function setDataDirectory(path: string): void;
4 changes: 4 additions & 0 deletions src/exports/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const settings = {

setRequestDefaultRetryDelay(delay) {
config.request.retry.delay = delay;
},

setDataDirectory(path) {
config.data.dir = path;
}

};
Expand Down
32 changes: 31 additions & 1 deletion src/helpers/file.utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const path = require('path');
const config = require('../config');

function getSnapshotDirAndName(name) {
Expand Down Expand Up @@ -27,7 +28,36 @@ function saveSnapshot(name, data) {
fs.writeFileSync(`${snapshotDir}/${snapshotFile}`, JSON.stringify(data, null, 2));
}

/**
*
* @param {string} name
*/
function findFile(name, dir = config.data.dir) {
if (fs.existsSync(name)) {
return fs.readFileSync(name);
}
if (fs.existsSync(dir)) {
const exist = fs.existsSync(`${dir}/${name}`);
if (exist) {
return fs.readFileSync(`${dir}/${name}`);
}
// get folders in dir
const files = fs.readdirSync(dir);
for (const file of files) {
const dirPath = path.resolve(dir, file);
const stats = fs.statSync(dirPath);
if (stats.isDirectory()) {
const result = findFile(name, dirPath);
if (result) {
return result;
}
}
}
}
}

module.exports = {
getSnapshotFile,
saveSnapshot
saveSnapshot,
findFile
};
3 changes: 2 additions & 1 deletion src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const responseExpect = require('../exports/expect');
const hr = require('../helpers/handler.runner');
const rlc = require('../helpers/reporter.lifeCycle');
const config = require('../config');
const { findFile } = require('../helpers/file.utils');

class Spec {
constructor(name, data) {
Expand Down Expand Up @@ -192,7 +193,7 @@ class Spec {

withJson(json) {
if (typeof json === 'string') {
json = JSON.parse(fs.readFileSync(json));
json = JSON.parse(findFile(json));
} else if (typeof json !== 'object') {
throw new PactumRequestError(`Invalid json in request - ${json}`);
}
Expand Down
51 changes: 51 additions & 0 deletions test/component/withJson.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const { spec, settings } = require('../../src/index');

describe('withJson', () => {

before(() => {
settings.setDataDirectory('test/data');
});

after(() => {
settings.setDataDirectory('data');
});

it('with file in parent folder', async () => {
await spec()
.useInteraction({
request: {
method: 'POST',
path: '/file',
body: {
"key": "value-1"
}
},
response: {
status: 200
}
})
.post('http://localhost:9393/file')
.withJson('sample-1.json')
.expectStatus(200);
});

it('with file in child folder', async () => {
await spec()
.useInteraction({
request: {
method: 'POST',
path: '/file',
body: {
"key": "value-2"
}
},
response: {
status: 200
}
})
.post('http://localhost:9393/file')
.withJson('sample-2.json')
.expectStatus(200);
});

});
3 changes: 3 additions & 0 deletions test/data/request/child/sample-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value-2"
}
3 changes: 3 additions & 0 deletions test/data/request/sample-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": "value-1"
}

0 comments on commit 4fc5a20

Please sign in to comment.