Files
bruno/packages/bruno-tests/collection/string interpolation/runtime vars.bru
Pragadesh-45 eb0accdf21 Update Bruno's Age 🎉 (#5328)
* feat: Update Bruno's age calculation in tests and specs to use a dynamic function instead of a static value.
2025-09-01 12:13:27 +05:30

97 lines
2.0 KiB
Plaintext

meta {
name: runtime vars
type: http
seq: 3
}
post {
url: {{host}}/api/echo/text
body: text
auth: none
}
auth:basic {
username: asd
password: j
}
auth:bearer {
token:
}
body:json {
{
"envVar1": "{{env.var1}}",
"envVar2": "{{env-var2}}"
}
}
body:text {
Hi, I am {{rUser.full_name}},
I am {{rUser.age}} years old.
My favorite food is {{rUser.fav-food[0]}} and {{rUser.fav-food[1]}}.
I like attention: {{rUser['want.attention']}}
}
assert {
res.status: eq 200
}
script:pre-request {
const brunoBirthDate = new Date('2019-08-08');
const calculateAgeFromBirthDate = (birthDate = brunoBirthDate) => {
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const hasBirthdayPassedThisYear =
today.getMonth() > birthDate.getMonth() ||
(today.getMonth() === birthDate.getMonth() && today.getDate() >= birthDate.getDate());
if (!hasBirthdayPassedThisYear) {
age--;
}
return age;
};
const brunoAge = calculateAgeFromBirthDate(brunoBirthDate);
bru.setVar("rUser", {
full_name: 'Bruno',
age: brunoAge,
'fav-food': ['egg', 'meat'],
'want.attention': true
});
}
tests {
test("should return json", function() {
const brunoBirthDate = new Date('2019-08-08');
const calculateAgeFromBirthDate = (birthDate = brunoBirthDate) => {
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const hasBirthdayPassedThisYear =
today.getMonth() > birthDate.getMonth() ||
(today.getMonth() === birthDate.getMonth() && today.getDate() >= birthDate.getDate());
if (!hasBirthdayPassedThisYear) {
age--;
}
return age;
};
const brunoAge = calculateAgeFromBirthDate(brunoBirthDate);
const expectedResponse = `Hi, I am Bruno,
I am ${brunoAge} years old.
My favorite food is egg and meat.
I like attention: true`;
expect(res.getBody()).to.equal(expectedResponse);
});
}