usebruno / bruno

Opensource IDE For Exploring and Testing Api's (lightweight alternative to postman/insomnia)
https://www.usebruno.com/
MIT License
27.01k stars 1.24k forks source link

Bruno CLI does not set variables #2612

Closed MikhailStrokov closed 3 months ago

MikhailStrokov commented 4 months ago

I have checked the following:

Describe the bug

CLI does not set variables. version - 1.20.3 run command - bru run --env test --output results.json

1 - Get any json response and set any field via vars. Here I set data via Vars and dataVar via Script (screenshots below)

let data = res.getBody();
bru.setVar("dataVar",data.slideshow.title)
bru.setEnvVar("dataVarEnv",data.slideshow.title)

Response:

{
  "slideshow": {
    "author": "Yours Truly",
    "date": "date of publication",
    "slides": [
      {
        "title": "Wake up to WonderWidgets!",
        "type": "all"
      },
      {
        "items": [
          "Why <em>WonderWidgets</em> are great",
          "Who <em>buys</em> WonderWidgets"
        ],
        "title": "Overview",
        "type": "all"
      }
    ],
    "title": "Sample Slide Show"
  }
}

2 - Send request using variable set via Vars (get-dynamic-data-from-var) :x: request from output file:

{
        "test": {
          "filename": "get-dynamic-data-from-var.bru"
        },
        "request": {
          "method": "GET",
          "url": "https://httpbin.org/base64/{{data}}",
          "headers": {}
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "headers": {
            "date": "Wed, 10 Jul 2024 14:22:19 GMT",
            "content-type": "text/html; charset=utf-8",
            "content-length": "51",
            "connection": "keep-alive",
            "server": "gunicorn/19.9.0",
            "access-control-allow-origin": "*",
            "access-control-allow-credentials": "true"
          },
          "data": "Incorrect Base64 data try: SFRUUEJJTiBpcyBhd2Vzb21l",
          "responseTime": 1276
        },
        "error": null,
        "assertionResults": [],
        "testResults": [],
        "runtime": 1.277278458,
        "suitename": "get-dynamic-data-from-var",
        "iterationIndex": 0
      }

As you can see in url placeholder {{data}} is used instead of value

3 - Send request using variable set via Scripts (get-dynamic-data-from-script) :x: request from output file:

{
        "test": {
          "filename": "get-dynamic-data-from-script.bru"
        },
        "request": {
          "method": "GET",
          "url": "https://httpbin.org/base64/{{dataVar}}",
          "headers": {}
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "headers": {
            "date": "Wed, 10 Jul 2024 14:22:20 GMT",
            "content-type": "text/html; charset=utf-8",
            "content-length": "51",
            "connection": "keep-alive",
            "server": "gunicorn/19.9.0",
            "access-control-allow-origin": "*",
            "access-control-allow-credentials": "true"
          },
          "data": "Incorrect Base64 data try: SFRUUEJJTiBpcyBhd2Vzb21l",
          "responseTime": 1207
        },
        "error": null,
        "assertionResults": [],
        "testResults": [],
        "runtime": 1.208722666,
        "suitename": "get-dynamic-data-from-script",
        "iterationIndex": 0
      }

As you can see in url placeholder {{dataVar}} is used instead of value

4 - Send request using environment variable set via Scripts (get-dynamic-data-from-env) :white_check_mark: request from output file:

{
        "test": {
          "filename": "get-dynamic-data-from-env.bru"
        },
        "request": {
          "method": "GET",
          "url": "https://httpbin.org/base64/Sample Slide Show",
          "headers": {}
        },
        "response": {
          "status": 200,
          "statusText": "OK",
          "headers": {
            "date": "Wed, 10 Jul 2024 14:22:20 GMT",
            "content-type": "text/html; charset=utf-8",
            "content-length": "51",
            "connection": "keep-alive",
            "server": "gunicorn/19.9.0",
            "access-control-allow-origin": "*",
            "access-control-allow-credentials": "true"
          },
          "data": "Incorrect Base64 data try: SFRUUEJJTiBpcyBhd2Vzb21l",
          "responseTime": 133
        },
        "error": null,
        "assertionResults": [],
        "testResults": [],
        "runtime": 0.134861667,
        "suitename": "get-dynamic-data-from-env",
        "iterationIndex": 0
      }

As you can see in url the value for {{dataVarEnv}} placeholder is used. So with environment variables it works. It might be a work around but if you set new value for same env key it will not override old value and the old value will be used. I'm not sure is it also a bug or not but there is no method to clear env variable (but exists for variable) so you have to set new key every time if you want to use WA.

All bru files with cli output file are attached in zip archive

.bru file to reproduce the bug

demo.zip

Screenshots/Live demo link

image image
MikhailStrokov commented 4 months ago

Update: I started creating ticket before https://github.com/usebruno/bruno/issues/2611 was created. I guess this one might be marked as duplicated because problem is the same. Maybe examples are useful for investigation

MikhailStrokov commented 3 months ago

works in version 1.20.4