Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override request header key in tests #2262

Open
1 task done
dragondive opened this issue May 8, 2024 · 0 comments
Open
1 task done

Override request header key in tests #2262

dragondive opened this issue May 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@dragondive
Copy link

dragondive commented May 8, 2024

I have checked the following:

  • I've searched existing issues and found nothing related to my issue.

Describe the feature you want to add

Keys specified in the Headers tab should be overridable in Tests. This would enable the asserts and most tests to run with the "standard" header, and specific tests to run with a different header.

Note: For the scenario I'm currently working with, the Header tab is relevant, but this could also apply to Query, Body and Auth.

Mockups or Images of the feature

I will explain my scenario using the simple API Gitlab version.

Request: GET {{GITLAB_API_V4_URL}}/version

Headers:
PRIVATE-TOKEN: {{GITLAB_TOKEN}}

(where {{GITLAB_API_V4_URL}} and {{GITLAB_TOKEN}} are defined as environment variables)

Now I define a few Asserts:

  • res.status equals 200
  • res.body.version notEquals ""
  • req.body.enterprise equals true

Then I write a test:

test("content-type should be application/json", function() {
  const token = res.getHeader("content-type");
  expect(token).to.equal("application/json");
})

So far, it's going great. Now ...

Problem scenario:

[Update: Please see the note at the end.]

I want to now write a test to ensure that the API is not accessible with an invalid token, so I do:

test("cannot access with invalid token", function() {
  req.setHeader("PRIVATE-TOKEN", "invalid-token");
  expect(res.status).to.equal(401);
})

But this doesn't work:

✘  cannot access with invalid token
expected 200 to equal 401

It appears the PRIVATE-TOKEN defined under Headers tab takes precedence over the req.setHeader() in the Tests. Hence this test fails.

What I have tried that does not work:

  1. Remove PRIVATE-TOKEN from Headers, then set it explicitly in the tests. This is problematic because (1) Assert cannot work any more and (2) it is unintuitive for the user.
  2. Use bru.setEnvVar() in the test to change the value. This is problematic as it updates the environment variable globally, and not only in the scope of the current test.

Proposed/requested solution:

req.setHeader(key, value) should override the respective value defined under Headers (if any) within the scope of the current test.

By reading through the docs (and reading the past issues and discussions here), I couldn't figure out any other approach to write these tests. If I have overlooked something or there's another solution already available, please point me to it.

Screenshots:
For convenient reference, below are some screenshots of what has been textually described above.

bruno-request-response

bruno-test-failure

bruno-asserts

bruno-environment

Note

I had initially misunderstood the Tests tab. I thought it was a place to write a "test suite" of multiple tests, akin to unit tests. Now I realize that those are not separate tests that trigger multiple mutually independent invocations of the API.

However, I do believe that would be a useful feature that lets us write "parametrized" tests for the API. I will update this enhancement request to describe this, but I thought I'd post an interim update here to reduce confusion.

@dragondive dragondive added the enhancement New feature or request label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant