mirror of
https://github.com/usebruno/bruno.git
synced 2025-05-05 15:32:58 +00:00
feat: add tests for mock variable interpolation in interpolate function (#4507)
* feat: add tests for mock variable interpolation in interpolate function * test: enhance mock variable interpolation tests for additional types and JSON validation --------- Co-authored-by: sanjai0py <sanjailucifer666@gmail.com>
This commit is contained in:
parent
d376947a91
commit
e8affcfde9
@ -354,3 +354,68 @@ describe('interpolate - recursive', () => {
|
||||
}`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('interpolate - mock variable interpolation', () => {
|
||||
it('should replace mock variables with generated values', () => {
|
||||
const inputString = '{{$randomInt}}, {{$randomIP}}, {{$randomIPV4}}, {{$randomIPV6}}, {{$randomBoolean}}';
|
||||
|
||||
const result = interpolate(inputString, {});
|
||||
|
||||
// Validate the result using regex patterns
|
||||
const randomIntPattern = /^\d+$/;
|
||||
const randomIPPattern = /^([\da-f]{1,4}:){7}[\da-f]{1,4}$|^(\d{1,3}\.){3}\d{1,3}$/;
|
||||
const randomIPV4Pattern = /^(\d{1,3}\.){3}\d{1,3}$/;
|
||||
const randomIPV6Pattern = /^([\da-f]{1,4}:){7}[\da-f]{1,4}$/;
|
||||
const randomBooleanPattern = /^(true|false)$/;
|
||||
|
||||
const [randomInt, randomIP, randomIPV4, randomIPV6, randomBoolean] = result.split(', ');
|
||||
|
||||
expect(randomIntPattern.test(randomInt)).toBe(true);
|
||||
expect(randomIPPattern.test(randomIP)).toBe(true);
|
||||
expect(randomIPV4Pattern.test(randomIPV4)).toBe(true);
|
||||
expect(randomIPV6Pattern.test(randomIPV6)).toBe(true);
|
||||
expect(randomBooleanPattern.test(randomBoolean)).toBe(true);
|
||||
});;
|
||||
|
||||
it('should leave mock variables unchanged if no corresponding function exists', () => {
|
||||
const inputString = 'Random number: {{$nonExistentMock}}';
|
||||
|
||||
const result = interpolate(inputString, {});
|
||||
|
||||
expect(result).toBe('Random number: {{$nonExistentMock}}');
|
||||
});
|
||||
|
||||
it('should escape special characters in mock variable values and produce valid JSON when escapeJSONStrings is true', () => {
|
||||
const inputString = '{"escapedValue": "{{$randomLoremParagraphs}}"}';
|
||||
|
||||
expect(() => {
|
||||
const result = interpolate(inputString, {}, { escapeJSONStrings: true });
|
||||
JSON.parse(result); // This should not throw an error
|
||||
}).not.toThrow();
|
||||
});
|
||||
|
||||
it('should not produce valid JSON when escapeJSONStrings is false', () => {
|
||||
const inputString = '{"escapedValue": "{{$randomLoremParagraphs}}"}';
|
||||
|
||||
expect(() => {
|
||||
const result = interpolate(inputString, {}, { escapeJSONStrings: false });
|
||||
JSON.parse(result); // This should throw an error
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
it('should throw an error when producing invalid JSON regardless of escapeJSONStrings option', () => {
|
||||
const inputString = '{"escapedValue": "{{$randomLoremParagraphs}}"}';
|
||||
|
||||
// Test without providing the options argument
|
||||
expect(() => {
|
||||
const result = interpolate(inputString, {});
|
||||
JSON.parse(result); // This should throw an error
|
||||
}).toThrow();
|
||||
|
||||
// Test with escapeJSONStrings explicitly set to false
|
||||
expect(() => {
|
||||
const result = interpolate(inputString, {}, { escapeJSONStrings: false });
|
||||
JSON.parse(result); // This should throw an error
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user