mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-11 09:51:30 +00:00
fix: isJson assertion should accept arrays as valid JSON (#7620)
* fix(assert-runtime): update JSON validation to allow arrays and enhance test coverage - Modified the JSON validation logic to accept arrays as valid JSON objects. - Updated tests to ensure correct behavior for various array scenarios, including empty arrays and arrays of strings and objects. * fix(assert-runtime): refine JSON validation logic to correctly handle arrays - Updated the JSON validation to allow arrays as valid JSON objects, ensuring compatibility with various data structures. - Adjusted the test assertions to reflect the new validation criteria.
This commit is contained in:
@@ -17,8 +17,8 @@ chai.use(function (chai, utils) {
|
||||
// Objects created inside Node's vm.createContext() have a different Object constructor,
|
||||
// so obj.constructor === Object fails for objects passed via res.setBody() from scripts.
|
||||
// Note: toString check is more permissive than constructor check — custom class instances
|
||||
const isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj)
|
||||
&& Object.prototype.toString.call(obj) === '[object Object]';
|
||||
const isJson = typeof obj === 'object' && obj !== null
|
||||
&& (Array.isArray(obj) || Object.prototype.toString.call(obj) === '[object Object]');
|
||||
|
||||
this.assert(isJson, `expected ${utils.inspect(obj)} to be JSON`, `expected ${utils.inspect(obj)} not to be JSON`);
|
||||
});
|
||||
|
||||
@@ -69,8 +69,8 @@ const addBruShimToContext = (vm, __brunoTestResults) => {
|
||||
Object.defineProperty(proto, 'json', {
|
||||
get: function () {
|
||||
var obj = this._obj;
|
||||
var isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) &&
|
||||
Object.prototype.toString.call(obj) === '[object Object]';
|
||||
var isJson = typeof obj === 'object' && obj !== null &&
|
||||
(Array.isArray(obj) || Object.prototype.toString.call(obj) === '[object Object]');
|
||||
this.assert(isJson, 'expected #{this} to be JSON', 'expected #{this} not to be JSON');
|
||||
return this;
|
||||
},
|
||||
|
||||
@@ -317,12 +317,36 @@ describe('runtime', () => {
|
||||
expect(results[0].status).toBe('pass');
|
||||
});
|
||||
|
||||
it('should fail for an array', () => {
|
||||
it('should pass for an array', () => {
|
||||
const results = runAssertions(
|
||||
[{ name: 'res.body', value: 'isJson', enabled: true }],
|
||||
makeResponse([1, 2, 3])
|
||||
);
|
||||
expect(results[0].status).toBe('fail');
|
||||
expect(results[0].status).toBe('pass');
|
||||
});
|
||||
|
||||
it('should pass for an array of strings', () => {
|
||||
const results = runAssertions(
|
||||
[{ name: 'res.body', value: 'isJson', enabled: true }],
|
||||
makeResponse(['A55001213ZX0A'])
|
||||
);
|
||||
expect(results[0].status).toBe('pass');
|
||||
});
|
||||
|
||||
it('should pass for an empty array', () => {
|
||||
const results = runAssertions(
|
||||
[{ name: 'res.body', value: 'isJson', enabled: true }],
|
||||
makeResponse([])
|
||||
);
|
||||
expect(results[0].status).toBe('pass');
|
||||
});
|
||||
|
||||
it('should pass for an array of objects', () => {
|
||||
const results = runAssertions(
|
||||
[{ name: 'res.body', value: 'isJson', enabled: true }],
|
||||
makeResponse([{ id: 1 }, { id: 2 }])
|
||||
);
|
||||
expect(results[0].status).toBe('pass');
|
||||
});
|
||||
|
||||
it('should fail for a string', () => {
|
||||
|
||||
Reference in New Issue
Block a user