最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

testing - In httpYac how to assign the current system time to a variable and use this variable in http request payload? - Stack

programmeradmin1浏览0评论

For my e2e tests I'd like to get the current date and time and use this dateTime string in the payload body, like this:

### Precondition: Create a contract with current time stamp in ISO format

# currentDateTimeStringInISO = some JavaScript code 

POST http://localhost:8080/api/rest/v1/contracts/12345678
Authorization: Basic user user
Content-Type: application/json

{
  "valid_until": currentDateTimeStringInISO,
  ...
}

> {% 
  client.test("Status code is 201", function() {
    client.assert(response.status === 201, "Expected status code 201 for contract creation");
  });
  client.test("Contract status is VALID", function() {
    client.assert(response.body.contractStatus === "VALID", "Expected status to be VALID");
  });
%}

What's the correct httpYac syntax for this? I read the official doc but did not get this work.

Thanks, Nicole

For my e2e tests I'd like to get the current date and time and use this dateTime string in the payload body, like this:

### Precondition: Create a contract with current time stamp in ISO format

# currentDateTimeStringInISO = some JavaScript code 

POST http://localhost:8080/api/rest/v1/contracts/12345678
Authorization: Basic user user
Content-Type: application/json

{
  "valid_until": currentDateTimeStringInISO,
  ...
}

> {% 
  client.test("Status code is 201", function() {
    client.assert(response.status === 201, "Expected status code 201 for contract creation");
  });
  client.test("Contract status is VALID", function() {
    client.assert(response.body.contractStatus === "VALID", "Expected status to be VALID");
  });
%}

What's the correct httpYac syntax for this? I read the official doc but did not get this work.

Thanks, Nicole

Share Improve this question edited Mar 12 at 23:04 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 12 at 9:07 Nicole NaumannNicole Naumann 1,1282 gold badges11 silver badges24 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I found the solution from another thread: Is there a compatible way to create a dynamic date in .http files?

{{
  let now = new Date();
  // Add 1 second
  let cetTime = new Date(now.getTime() + 1000);
  // Adjust to CET winter time (CET, UTC+1)
  let cetDate = new Date(cetTime.getTime() + (1 * 60 * 60 * 1000));
  // Export as full ISO string with explicit CET offset
  exports.currentTime = berlinDate.toISOString().replace('Z', '+01:00');
}}
POST http://localhost:8080/api/rest/v1/contracts
Authorization: Basic user user
Content-Type: application/json

{
  "expiryDate": "{{currentTime}}",
  ...
}

@response
{{
  console.info('Generated currentTime was: ' + currentTime);
}}

...assertions

Inside the JSON payload, the double quote outside the {{}} is crucial.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论