- Added interpolation to setVar method's value field.
- Added playwright test to test the fix.
- Added jest test to test out the fix.

---
Playwright - PASS
Jest - PASS
---
This commit is contained in:
Chirag Chandrashekhar
2025-10-27 16:26:49 +05:30
committed by GitHub
parent c5325c732f
commit be7f92d77f
6 changed files with 100 additions and 1 deletions

View File

@@ -228,7 +228,7 @@ class Bru {
);
}
this.runtimeVariables[key] = value;
this.runtimeVariables[key] = this.interpolate(value);
}
getVar(key) {

View File

@@ -246,4 +246,16 @@ describe('runtime', () => {
expect(result.envVariables.number).toBe(42);
});
});
describe('bru.setVar random variable', () => {
it('should not be equal to {{$randomFirstName}}', async () => {
const script = `bru.setVar('title', '{{$randomFirstName}}')`;
const runtime = new ScriptRuntime({ runtime: 'vm2' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.runtimeVariables.title).not.toBe('{{$randomFirstName}}');
});
});
});

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "dynamic-variable-interpolation",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,25 @@
meta {
name: set-var-dynamic-variable
type: http
seq: 1
}
post {
url: https://echo.usebruno.com
body: json
auth: none
}
headers {
Content-Type: application/json
}
script:pre-request {
bru.setVar("title", "{{$randomFirstName}}");
}
body:json {
{
"title": "{{title}}"
}
}

View File

@@ -0,0 +1,6 @@
{
"maximized": true,
"lastOpenedCollections": [
"{{projectRoot}}/tests/interpolation/dynamic-variable/collection"
]
}

View File

@@ -0,0 +1,47 @@
import { test, expect } from '../../../playwright';
import { closeAllCollections, openCollectionAndAcceptSandbox } from '../../utils/page';
test.describe.serial('Dynamic Variable Interpolation', () => {
test.afterAll(async ({ pageWithUserData: page }) => {
// cleanup: close all collections
await closeAllCollections(page);
});
test('Verifying if the bru.setVar method interpolates random generator functions properly', async ({ pageWithUserData: page }) => {
// Open collection and accept sandbox mode
await openCollectionAndAcceptSandbox(page, 'dynamic-variable-interpolation', 'safe');
// Navigate to the request
await page.getByRole('complementary').getByText('set-var-dynamic-variable').click();
// Send the request
await page.getByTestId('send-arrow-icon').click();
// Wait for the response and verify status code
await expect(page.getByTestId('response-status-code')).toHaveText(/200/);
// Verify response contains the title field and that it's not the literal interpolation string
const responsePane = page.locator('.response-pane');
// Check that the response contains a title field
await expect(responsePane).toContainText('"title":');
// Get the response body text to extract the actual title value
const responseBodyText = await responsePane.innerText();
// Extract the title value from the JSON response
const titleMatch = responseBodyText.match(/"title":\s*"([^"]+)"/) ?? [];
expect(titleMatch).toBeTruthy();
const actualTitle = titleMatch[1];
// Verify that the title is not the literal interpolation string
// This ensures that the randomFirstName function was properly interpolated
expect(actualTitle).not.toEqual('{{$randomFirstName}}');
// Additional verification: ensure the title is a string and not empty
expect(actualTitle).toBeDefined();
expect(typeof actualTitle).toBe('string');
expect(actualTitle.length).toBeGreaterThan(0);
});
});