thunderclient / thunder-client-support

Thunder Client is a lightweight Rest API Client Extension for VS Code.
https://www.thunderclient.com
Other
3.62k stars 127 forks source link

TC CLI Bug with --var-data and Pre Run requests: variables not passed on to Pre Run requests #1586

Open raShMan777 opened 1 month ago

raShMan777 commented 1 month ago

I have this login request 'Logon': POST: {{url}}/ims/api/v1/access_keys/login Headers: Accept */* JSON Body:

{
         "access_key": "{{access_key}}",
         "access_secret_key": "{{access_secret_key}}"
}

Test: json.json_web_token setTo {{json_web_token}}

It is set as a Pre Request to run always in this request 'read classes': GET: {{url}}/events-service/api/v1.0/events/classes Headers: Accept */* Auth Bearer: {{json_web_token}} Test: json.statusCode equal 200 Pre Run: Logon (Run Always)

And it is also set as a Pre Request to run always in this request 'delete class - srz_heartbeatTest': DELETE: {{url}}/events-service/api/v1.0/events/classes/{{class_id}} Headers: Accept */* Auth Bearer: {{json_web_token}} Test: json.statusCode equal 200 Pre Run: Logon (Run Always) Pre Run Script:

tc.setVar("class_id", "");
// execute 'read classes' request
let response = await tc.runRequest("21594a53-568b-466f-82bc-58f1c6abc9c3");
console.log("'read classes' response status': " + response.status);
let jsonObject = JSON.parse(response.text);
let srz_heartbeatTestid = jsonObject.eventClassList.find(event => event.name === "srz_heartbeatTest").id;
tc.setVar("class_id", srz_heartbeatTestid);

Now, when I'm executing it in VSCode everything's running fine. But when I'm executing it in the CLI I'm emptying the variables access_key, access_secret_key, json_web_token before executing this: ~/node_modules/.bin/tc --env 'Helix-Dev' --reqlist 'delete class - srz_heartbeatTest' --log 0 --var-data "access_key=foo,access_secret_key=bar"

This works when I'm executing e.g. 'read classes' directly and all other request which have Logon as Pre Run. When I'm executing 'delete class - srz_heartbeatTest' I get this:

Running Collection: Helix
Environment Used: Helix-Dev (Active)

Running Iteration 1 of 1

Event-Classes

  DELETE - delete class - srz_heartbeatTest      Error   0 ms       0/0   .

Request URL:  DELETE -
Environment:  Helix-Dev (Active)

Response
Error in Pre Request Script: - Unexpected token 'A', "Authentica"... is not valid JSON

Logs from Scripts

Script Log: 'read classes' response status': 401

Summary Report

┌────────────────────┬─────────┬──────────┬──────────┬───────────┐
│  Name              │  Total  │  Passed  │  Failed  │  Skipped  │
├────────────────────┼─────────┼──────────┼──────────┼───────────┤
│  Total Iterations  │  1      │  0       │  1       │  0        │
├────────────────────┼─────────┼──────────┼──────────┼───────────┤
│  Total Requests    │  1      │  0       │  1       │  0        │
└────────────────────┴─────────┴──────────┴──────────┴───────────┘

Total Duration: 332ms
Average Response Time: 0ms

It works when the variables are saved in the environment 'Helix-Dev' and not provided via --var-data:

...
Response
{
  "responseTimeStamp": 1723185044280,
  "statusCode": "200",
  "statusMsg": "[Event class deletion successful.]",
  "resourceId": [
    "cf413738-558a-11ef-86bb-f1d4307d6e3c"
  ]
}

Tests Results

Query: json.statusCode equal to 200                  Pass   .

Logs from Scripts

Script Log: 'read classes' response status': 200
...

$ ~/node_modules/.bin/tc --debug:

Version: 1.16.3
Data loaded from the path - /foo/bar/thunder-tests

Settings Data:
saveToWorkspace: undefined
workspaceRelativePath: undefined
customLocation: undefined
htmlReportResponseLimit: 100
httpLibrary: got

Platform:

The CLI is executed in my WSL Fedora 40 with:

Using the paid version.

rangav commented 1 month ago

Thanks for reporting the bug,

let response = await tc.runRequest("21594a53-568b-466f-82bc-58f1c6abc9c3");

Please log the env values you passed in CLI argument before runRequest and verify it.

raShMan777 commented 1 month ago

I put it in all three requests (values masked by me):

Script Log: access_key inside delete class: XRB5**********************WTCS
Script Log: access_secret_key inside delete class: ym4i******************************************6F97
Script Log: access_key inside read classes:
Script Log: access_secret_key inside read classes:
Script Log: access_key inside logon:
Script Log: access_secret_key inside logon:
Script Log: 'read classes' Response Status': 401

The values were correct but they don't get passed along…

rangav commented 1 month ago

try log the response from this request and verify it. let response = await tc.runRequest("21594a53-568b-466f-82bc-58f1c6abc9c3");

also, see logs you may find other useful info https://docs.thunderclient.com/features/logs

raShMan777 commented 1 month ago

Not sure if what I'm seeing is helpful or not:

Response
Error in Pre Request Script: - Unexpected token 'A', "Authentica"... is not valid JSON

Logs from Scripts

Script Log: access_key inside delete class: XRB5**********************WTCS
Script Log: access_secret_key inside delete class: ym4i******************************************6F97
Script Log: access_key inside read classes:
Script Log: access_secret_key inside read classes:
Script Log: access_key inside logon:
Script Log: access_secret_key inside logon:

Script Log: status: 401
Script Log: time: 37
Script Log: size: 24
Script Log: contentType: application/json
Script Log: json: undefined
Script Log: text: Authentication Failed!!!
Script Log: header name: date, Value: Fri, 09 Aug 2024 11:29:15 GMT
Script Log: header name: content-type, Value: application/json
Script Log: header name: transfer-encoding, Value: chunked
Script Log: header name: connection, Value: keep-alive
Script Log: header name: referrer-policy, Value: same-origin
Script Log: header name: x-frame-options, Value: SAMEORIGIN
Script Log: header name: strict-transport-security, Value: max-age=15724800; includeSubDomains
Script Log: header name: x-content-type-options, Value: nosniff
Script Log: header name: permissions-policy, Value: idle-detection=self
Script Log: header name: content-encoding, Value: gzip
Script Log: header name: x-xss-protection, Value: 1; mode=block

Also I don't see the run/response in VSCode at all and I also don't see any logs there.

For me the question's still the same: why is it working when the values are saved in the environment and not when the values are passed via CLI?

raShMan777 commented 1 month ago

you got any news on the bug?

rangav commented 1 month ago

@raShMan777 We will investigate the issue and get back to you in 1 or 2 days.