mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-11 09:51:30 +00:00
Resolved issue: https://github.com/usebruno/bruno/issues/4672 (#5900)
- 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:
committed by
GitHub
parent
c5325c732f
commit
be7f92d77f
@@ -228,7 +228,7 @@ class Bru {
|
||||
);
|
||||
}
|
||||
|
||||
this.runtimeVariables[key] = value;
|
||||
this.runtimeVariables[key] = this.interpolate(value);
|
||||
}
|
||||
|
||||
getVar(key) {
|
||||
|
||||
@@ -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}}');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"version": "1",
|
||||
"name": "dynamic-variable-interpolation",
|
||||
"type": "collection",
|
||||
"ignore": [
|
||||
"node_modules",
|
||||
".git"
|
||||
]
|
||||
}
|
||||
@@ -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}}"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"maximized": true,
|
||||
"lastOpenedCollections": [
|
||||
"{{projectRoot}}/tests/interpolation/dynamic-variable/collection"
|
||||
]
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user