Skip to content

Commit

Permalink
feat: support data template name in withJson and expectJson (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Apr 5, 2024
1 parent 1dbe4e4 commit 81dbe6d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 26 deletions.
29 changes: 20 additions & 9 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RequestOptions, IncomingMessage } from 'http';
import FormData from 'form-data-lite';
import { IncomingMessage, RequestOptions } from 'http';
import { Expect } from '../exports/expect';
import { CaptureHandlerFunction, ExpectHandlerFunction, RetryHandlerFunction } from '../exports/handler';
import { Interaction } from '../exports/mock';
import { ExpectHandlerFunction, RetryHandlerFunction, CaptureHandlerFunction } from '../exports/handler';
import { LogLevel } from '../exports/settings';
import { Expect } from '../exports/expect';

declare interface RetryOptions {
/** maximum number of retries - defaults to 3 */
Expand Down Expand Up @@ -163,6 +163,7 @@ declare class Spec {
* @see https://pactumjs.github.io/api/requests/withJson.html
*/
withJson(json: object): Spec;
withJson(templateName: string): Spec;
withJson(filePath: string): Spec;

/**
Expand Down Expand Up @@ -296,37 +297,47 @@ declare class Spec {
* @see https://pactumjs.github.io/api/assertions/expectJson.html
*/
expectJson(json: object): Spec;
expectJson(path: string, value: any): Spec;
expectJson(templateName: string): Spec;
expectJson(filePath: string): Spec;
expectJson(jsonPath: string, value: any): Spec;

/**
* expects a partial json object in the response
* @see https://pactumjs.github.io/api/assertions/expectJsonLike.html
*/
expectJsonLike(json: object): Spec;
expectJsonLike(path: string, value: any): Spec;
expectJsonLike(templateName: string): Spec;
expectJsonLike(filePath: string): Spec;
expectJsonLike(jsonPath: string, value: any): Spec;

/**
* expects the response to match with json schema
* @see https://pactumjs.github.io/api/assertions/expectJsonSchema.html
*/
expectJsonSchema(schema: object): Spec;
expectJsonSchema(schema: object, options: object): Spec;
expectJsonSchema(path: string, schema: object): Spec;
expectJsonSchema(path: string, schema: object, options: object): Spec;
expectJsonSchema(templateName: string): Spec;
expectJsonSchema(filePath: string): Spec;
expectJsonSchema(jsonPath: string, schema: object): Spec;
expectJsonSchema(jsonPath: string, schema: object, options: object): Spec;

/**
* expects the json to match with value
* @see https://pactumjs.github.io/api/assertions/expectJsonMatch.html
*/
expectJsonMatch(value: object): Spec;
expectJsonMatch(path: string, value: object): Spec;
expectJsonMatch(templateName: string): Spec;
expectJsonMatch(filePath: string): Spec;
expectJsonMatch(jsonPath: string, value: object): Spec;

/**
* expects the json to strictly match with value
* @see https://pactumjs.github.io/api/assertions/expectJsonMatchStrict.html
*/
expectJsonMatchStrict(value: object): Spec;
expectJsonMatchStrict(path: string, value: object): Spec;
expectJsonMatchStrict(templateName: string): Spec;
expectJsonMatchStrict(filePath: string): Spec;
expectJsonMatchStrict(jsonPath: string, value: object): Spec;

/**
* expects the json to an array with length
Expand Down
21 changes: 13 additions & 8 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const hr = require('../helpers/handler.runner');
const rlc = require('../helpers/reporter.lifeCycle');
const config = require('../config');
const { findFile } = require('../helpers/file.utils');
const stash = require('../exports/stash');

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

withJson(json) {
if (typeof json === 'string') {
json = JSON.parse(findFile(json));
json = get_json_from_template_or_file(json);
} else if (typeof json !== 'object') {
throw new PactumRequestError(`Invalid json in request - ${json}`);
}
Expand Down Expand Up @@ -382,13 +383,13 @@ class Spec {
}

expectJson(path, value) {
typeof value === 'undefined' ? this._expect.json.push(getJson(path)) : this._expect.jsonQuery.push({ path, value });
typeof value === 'undefined' ? this._expect.json.push(get_json_from_template_or_file(path)) : this._expect.jsonQuery.push({ path, value });
return this;
}
expectJsonAt(...args) { return this.expectJson(...args); }

expectJsonLike(path, value) {
typeof value === 'undefined' ? this._expect.jsonLike.push(getJson(path)) : this._expect.jsonQueryLike.push({ path, value });
typeof value === 'undefined' ? this._expect.jsonLike.push(get_json_from_template_or_file(path)) : this._expect.jsonQueryLike.push({ path, value });
return this;
}
expectJsonLikeAt(...args) { return this.expectJsonLike(...args); }
Expand All @@ -398,7 +399,7 @@ class Spec {
this._expect.jsonSchemaQuery.push({ path, value, options });
} else {
if (typeof value === 'undefined') {
this._expect.jsonSchema.push({ value: getJson(path) });
this._expect.jsonSchema.push({ value: get_json_from_template_or_file(path) });
} else {
if (typeof path === 'object' && typeof value === 'object') {
this._expect.jsonSchema.push({ value: path, options: value });
Expand All @@ -412,13 +413,13 @@ class Spec {
expectJsonSchemaAt(...args) { return this.expectJsonSchema(...args); }

expectJsonMatch(path, value) {
typeof value === 'undefined' ? this._expect.jsonMatch.push(getJson(path)) : this._expect.jsonMatchQuery.push({ path, value });
typeof value === 'undefined' ? this._expect.jsonMatch.push(get_json_from_template_or_file(path)) : this._expect.jsonMatchQuery.push({ path, value });
return this;
}
expectJsonMatchAt(...args) { return this.expectJsonMatch(...args); }

expectJsonMatchStrict(path, value) {
typeof value === 'undefined' ? this._expect.jsonMatchStrict.push(getJson(path)) : this._expect.jsonMatchStrictQuery.push({ path, value });
typeof value === 'undefined' ? this._expect.jsonMatchStrict.push(get_json_from_template_or_file(path)) : this._expect.jsonMatchStrictQuery.push({ path, value });
return this;
}
expectJsonMatchStrictAt(...args) { return this.expectJsonMatchStrict(...args); }
Expand Down Expand Up @@ -548,9 +549,13 @@ function validateRequestUrl(request, url) {
}
}

function getJson(path) {
function get_json_from_template_or_file(path) {
if (typeof path === 'string') {
return JSON.parse(findFile(path));
if (stash.getDataTemplate()[path]) {
return { '@DATA:TEMPLATE@': path };
} else {
return JSON.parse(findFile(path));
}
}
return path;
}
Expand Down
10 changes: 5 additions & 5 deletions test/component/templates.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('Templates & Maps', () => {
pactum.handler.addDataFuncHandler('GetSum', (ctx) => parseInt(ctx.args[0]) + parseInt(ctx.args[1]));
});

after(() => {
stash.clearDataTemplates();
stash.clearDataMaps();
});

it('new user with pure template', async () => {
await pactum.spec()
.useInteraction({
Expand Down Expand Up @@ -268,9 +273,4 @@ describe('Templates & Maps', () => {
});
});

after(() => {
stash.clearDataTemplates();
stash.clearDataMaps();
});

});
33 changes: 29 additions & 4 deletions test/component/withJson.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const { spec, settings } = require('../../src/index');
const { spec, settings, stash } = require('../../src/index');
const expect = require('chai').expect;

describe('withJson', () => {

before(() => {
settings.setDataDirectory('test/data');
stash.addDataTemplate({
'RESPONSE:SAMPLE': {
"key": "value-1"
}
});
});

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

it('with file in parent folder', async () => {
Expand Down Expand Up @@ -55,10 +61,29 @@ describe('withJson', () => {
await spec()
.post('http://localhost:9393/file')
.withJson('invalid-file.json')
} catch(error) {
} catch (error) {
err = error;
}
expect(err.message).equals(`File Not Found - 'invalid-file.json'`);
})

});

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

});

0 comments on commit 81dbe6d

Please sign in to comment.