usebruno / bruno

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

CLI does not update environment variables #2406

Open phkorn opened 3 months ago

phkorn commented 3 months ago

I have checked the following:

Describe the bug

If I run the following examples you can see that the update of the environment variable access_token based on the first request does not work and the second uses the one already persisted in the environments bru file.

.bru file to reproduce the bug

first.bru

meta {
  name: first
  type: http
  seq: 2
}

get {
  url: http://example.com
  body: none
  auth: none
}

script:post-response {
  bru.setEnvVar("access_token", res.headers.age)
}

second.bru

meta {
  name: second
  type: http
  seq: 3
}

post {
  url: http://example.com
  body: none
  auth: bearer
}

auth:bearer {
  token: {{access_token}}
}

Screenshots/Live demo link

bru run first.bru --env clitest --bail -o first.txt && cat first.txt
Running Request

first (200 OK) - 245 ms

Requests:    1 passed, 1 total
Tests:       0 passed, 0 total
Assertions:  0 passed, 0 total
Ran all requests - 245 ms
Wrote results to first.txt
{
  "summary": {
    "totalRequests": 1,
    "passedRequests": 1,
    "failedRequests": 0,
    "totalAssertions": 0,
    "passedAssertions": 0,
    "failedAssertions": 0,
    "totalTests": 0,
    "passedTests": 0,
    "failedTests": 0
  },
  "results": [
    {
      "test": {
        "filename": "first.bru"
      },
      "request": {
        "method": "GET",
        "url": "http://example.com",
        "headers": {}
      },
      "response": {
        "status": 200,
        "statusText": "OK",
        "headers": {
          "accept-ranges": "bytes",
          "age": "310710",
          "cache-control": "max-age=604800",
          "content-type": "text/html; charset=UTF-8",
          "date": "Mon, 03 Jun 2024 09:00:16 GMT",
          "etag": "\"3147526947\"",
          "expires": "Mon, 10 Jun 2024 09:00:16 GMT",
          "last-modified": "Thu, 17 Oct 2019 07:18:26 GMT",
          "server": "ECAcc (nyd/D17D)",
          "vary": "Accept-Encoding",
          "x-cache": "HIT",
          "content-length": "648",
          "connection": "close"
        },
        "data": "<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset=\"utf-8\" />\n    <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <style type=\"text/css\">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        div {\n            margin: 0 auto;\n            width: auto;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.</p>\n    <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
        "responseTime": 245
      },
      "error": null,
      "assertionResults": [],
      "testResults": [],
      "runtime": 0.257352667,
      "suitename": "first"
    }
  ]
}%   

bru run second.bru --env clitest --bail -o second.txt && cat second.txt
Running Request

second (200 OK) - 327 ms

Requests:    1 passed, 1 total
Tests:       0 passed, 0 total
Assertions:  0 passed, 0 total
Ran all requests - 327 ms
Wrote results to second.txt
{
  "summary": {
    "totalRequests": 1,
    "passedRequests": 1,
    "failedRequests": 0,
    "totalAssertions": 0,
    "passedAssertions": 0,
    "failedAssertions": 0,
    "totalTests": 0,
    "passedTests": 0,
    "failedTests": 0
  },
  "results": [
    {
      "test": {
        "filename": "second.bru"
      },
      "request": {
        "method": "POST",
        "url": "http://example.com",
        "headers": {
          "Authorization": "Bearer 310031"
        }
      },
      "response": {
        "status": 200,
        "statusText": "OK",
        "headers": {
          "accept-ranges": "bytes",
          "cache-control": "max-age=604800",
          "content-type": "text/html; charset=UTF-8",
          "date": "Mon, 03 Jun 2024 09:00:29 GMT",
          "etag": "\"3147526947\"",
          "expires": "Mon, 10 Jun 2024 09:00:29 GMT",
          "last-modified": "Thu, 17 Oct 2019 07:18:26 GMT",
          "server": "EOS (vny/044F)",
          "content-length": "1256",
          "connection": "close"
        },
        "data": "<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset=\"utf-8\" />\n    <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <style type=\"text/css\">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe UI\", \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        div {\n            margin: 0 auto;\n            width: auto;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.</p>\n    <p><a href=\"https://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n",
        "responseTime": 327
      },
      "error": null,
      "assertionResults": [],
      "testResults": [],
      "runtime": 0.332336792,
      "suitename": "second"
    }
  ]
}
``
` 
platform-kit commented 2 months ago

I'm running into this too. Trying to use the CLI to test a folder of requests using environment variables I'm assigning programmatically via the --env-var flag mentioned in the docs.

It does not work.

kompetenzbestie commented 3 weeks ago

Hey just wanted to ask if there's been any progress on this as I've run into this problem too.