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

Can you write one test and just change the environment variable? #152

Closed
FrankRua opened this issue Dec 9, 2021 · 10 comments
Closed

Can you write one test and just change the environment variable? #152

FrankRua opened this issue Dec 9, 2021 · 10 comments

Comments

@FrankRua
Copy link

FrankRua commented Dec 9, 2021

Is it possible that you can have one test in your *.http file and just simply just the variable on one iteration, and then another iteration have it change?

I have a use case for this as I want to run tests against a CDN and the Load Balancer behind the CDN. An example would be like:

// Iteration 1&2
GET {{cdnDns}}/healthcheck
GET {{lbDns}}/healthcheck

// Assertions should be the same, since response should be the same.

// Assert Result on both iterations
@AnWeber
Copy link
Owner

AnWeber commented Dec 9, 2021

This code should do the trick

### health_cdn
GET {{cdnDns}}/healthcheck
### health_lb
# @ref health_cdn
GET {{lbDns}}/healthcheck

{{
  const { equal } = require('assert');
  test('should be same response', () => {
    equal(JSON.stringify(health_lb),JSON.stringify(health_cdn));
  });
}}

@FrankRua
Copy link
Author

FrankRua commented Dec 9, 2021

@AnWeber How would I extract the status codes out of health_lb and health_cdn so I can do something like

    equal(health_lb.status, 200);
    equal(health_cdn.status, 200);

@AnWeber
Copy link
Owner

AnWeber commented Dec 9, 2021

@FrankRua not directly. My main use case is that I want to share the response body between requests. status is no longer important to me. For this reason you have to define a variable with the response of first request yourself

### health_cdn
GET https://httpbin.org/json

{{
  exports.health_cdn_response = response
}}

### health_lb
# @ref health_cdn
GET https://httpbin.org/json

{{
  const { equal } = require('assert');
  test('should be same response', () => {
    equal(JSON.stringify(health_lb),JSON.stringify(health_cdn));
  });
  test('should be same status', () => {
    equal(response.statusCode, health_cdn_response.statusCode);
  });
}}

Here is the interface for a httpResponse

@AnWeber
Copy link
Owner

AnWeber commented Dec 9, 2021

I would meanwhile already set the response as a variable, but initially overlooked it and now I don't want to change the api. But actually I could define an additional variable with ${name}Response.

@AnWeber
Copy link
Owner

AnWeber commented Dec 9, 2021

:-) sorry I closed the wrong issue.

@FrankRua
Copy link
Author

FrankRua commented Dec 9, 2021

@AnWeber Is there a way to see if the body of a response contains a string? I see the assert library has a "match" but I don't think I can use it with our library as it gives me match is not defined.

Could I possibly use a framework like Jest and read the 'response'? I just don't see many examples, sorry for all the questions 😄

@AnWeber
Copy link
Owner

AnWeber commented Dec 10, 2021

Jest is another test runner. httpyac is also used here as a test runner. I have also already tested the assertion with chai, here is a example. Maybe you can also use jest for this, But does not feel right.
If I were you, I would rather look at debugging the tests. You would have to install httpyac as CLI tool and start the call in VSCode in the javascript debug terminal (httpyac test.http). Add a debugger; to your Code and view response. Here are the steps documented in more detail

@FrankRua
Copy link
Author

FrankRua commented Dec 10, 2021

Is it possible to do a "contains" on on the response with HttpYac for an assertion?

@AnWeber
Copy link
Owner

AnWeber commented Dec 10, 2021

Yeah, the script Context is a real NodeJS Context. You can use all functionality of JavaScript, I prefer using indexOf but NodeJS should also support includes (not sure). You could JSON.stringify the complete response and use indexOf

@AnWeber
Copy link
Owner

AnWeber commented Dec 11, 2021

If there are any questions just contact me again:-)

@AnWeber AnWeber closed this as completed Dec 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants