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

Stop a POST request from sending if tests fail #3847

Closed
1 task done
Tasha53505 opened this issue Jan 21, 2025 · 4 comments
Closed
1 task done

Stop a POST request from sending if tests fail #3847

Tasha53505 opened this issue Jan 21, 2025 · 4 comments
Labels
enhancement New feature or request

Comments

@Tasha53505
Copy link

I have checked the following:

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

Describe the feature you want to add

Is there a way to already do this, or a work around? Perhaps something in the pre-request or post-request? Where if any of the tests written fail that it stops the response from passing through?

Mockups or Images of the feature

i.e
if(bru.tests === fail()) {
stop();

}

@Tasha53505 Tasha53505 added the enhancement New feature or request label Jan 21, 2025
@thiagomini
Copy link

Hey Tasha, I believe this isn't possible because tests are run after the request completes execution. You can notice that because inside the test cases you have access to the response object - hence, the request was already dispatched. Maybe you can describe your use case in more detail - there might be better-suited alternatives.

@Tasha53505
Copy link
Author

Ah okay - I think that answers my question. Unfortunate ! I can try and elaborate on my issue a little more / on what I'm after.

Here's an example;

  • I have an API endpoint that posts JSON body data when the request is sent.
  • I have 20 or so tests, all checking the validity of the post request.
  • An example might be a test to check that a JSON object contains at least one of X words in an array.
    i.e
test("AllopurinolDuration should only be 'Less than 1 Year' '1-2 Years' '3-4 Years' '5 Years or longer' OR '50 Years' ", function() {
  const allopurinolAtStartRequestData = req.getBody().AllopurinolAtStart;
  const allopurinolDurationRequestData = req.getBody().AllopurinolDuration;
  const validWords  = ["Less than 1 Year", "1-2 Years", "3-4 Years", "5 Years or longer", "50 Years"];
  
  if(allopurinolAtStartRequestData === true)  {
    expect(allopurinolDurationRequestData).to.be.oneOf(validWords);
       
  }
  
  });

with the JSON body being

{
  "AllopurinolDuration": "Less than 1 Year",
}

  • Which is great if the user ALWAYS puts in one of the "validWords" - However, what if a user put in:
{
  "AllopurinolDuration": "2500 years",
}

  • this test would fail. That's great - our test works exactly as it should because "2500 years" is not one of the validWords.
  • But, the POST request still sends through the request, updating AllopurinolDuration to 2500 even though our test has failed.

I hope this is making sense so far ! I am seeing if there is any way we can stop the request from posting if any of my script test fails. Perhaps if there is a way to force an exception thrown while tests are run so the request doesn't send through the data?

No worries if not, just wanted to check here instead of trying to spend days trying to code something that doesn't exist as functionality in Bruno.

@thiagomini
Copy link

thiagomini commented Jan 22, 2025

Hey @Tasha53505 , it sounds like you could validate that input in the pre-request script - check out the documentation. There, you have access to req.body to validate whatever you want. See the Picture below.

PS: Even though you can validate the input in the pre-request script, I'd advise doing that at the server side. As you described yourself, if the user sends "invalid" data somehow, that invalid data will be saved in the server side. The client shouldn't be the sole responsible for ensuring input validity.

Image

@Tasha53505
Copy link
Author

Thanks for this ! This is what I'm after.

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

2 participants