Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How can i customize status codes in parasoft

Hi,

I want to display custom status code based on my response. i always gets status as 200 OK but i want to change on some conditions.

ex: if passing date is greater then current date then display status = 400 and message = Bad Request.

  1. Can i do this in parasoft? (Yes - how) or
  2. Can i do this using groovy? (Yes - how)

Thanks You, Looking for response.

Tagged:

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited January 2020

    The Message Responder has a "Return Status" section on the "Options" tab.

    Docs:
    Message Responder Overview > Configuration Options > Options Tab > Return Status

    You would setup a different Message Responder for each status code then configure the correlation settings for each one so that desired responder is used depending on the correlations criteria you define.

  • jithenderreddy
    jithenderreddy Posts: 73
    edited January 2020

    Thank you for quick response. I tried above method and i still gets Status: 200 OK after i disable check box for Use Default Return Status. Do i miss something here?

    Steps for config:-
    1. Navigated to Options.
    2. Disabled - Use Default Return Status.
    3. Entered custom values in Return Message: this is test
    4. Entered custom values in Return Status: 444

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    Those steps should do it. Can you share your pva file? Are you deploying the virtual asset to the local server in your desktop or are you deploying it to a remote server?

  • i am using local server.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Can you attach the pva you setup? What tool are you using to test the response?

  • i am using postman for testing.

  • can we do it using Groovy script? Do we have a method where i can pass the value in script?

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    The steps outlined using Options and Return Status work for me. I don't know of a way to configure it using a groovy script. The fact that you are not seeing it work makes me think perhaps there is something misconfigured? Does your asset have multiple responders? In that case the first responder that correlates would return the response so if it were set to '200 OK' then you would not see the custom status code you entered on a different responder.

    If you could share the pva file you created it would help me understand what settings having been configured.

  • I have two scenarios where there is dates and without dates. It is working if i pass dates.
    i have a condition here wre it should throw error and message if i try to pass dates

    1.if passing date is greater then current date then display status = 400 and message = Bad Request.

    This should not effect if i don't pass any dates and data should display.
    Can we handle them differently?

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    You would setup a responder with the correlation condition for the date being greater than the current date. That responder would then have the status code 400. Then you would have a different responder for when no dates are passed.

  • Thank You, I will try creating another responder without dates and test it.

  • hi,

    i have created two responders without and with dates. I can able to get results for without dates but not with dates.

    1. scenario - created responder without date parameter and it is working fine.
    2. scenarios - created responder with dates parameter and it is not working.

    Note: i always see same error message even with valid dates.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    Is the without dates responder first in order? When looking for a matching responder they are checking in order and the first one to match returns the response.

  • Thank You. Yes - it is only responding to without dates. i have created another and placed under but it is only executing first one. It is not going to second one. Is there any thing wrong in the sequence?

    Test Assets:-
    Test1
    Test2
    Testwithoutdate-responder (without date)
    Test1
    Test2
    Testwithdate-responder (with date)

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    I would need to see for sure but if you're responder without dates doesn't have a correlation then it will always match. Try putting the with dates responder first. Responders are checked in order from first to last for a match.

  • Thank You. I have without dates first and with dates next. i created two responders and kept them as mentioned above. You mean do i need to move the dates first and without dates next level as below?

    Test1
    Test2
    Testwithdate-responder (with date)

    Test1
    Test2
    Testwithoutdate-responder (without date)

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    Yes I would try that, its hard to say without seeing how you setup the responders but if the "without dates" doesn't have a correlation set and "with dates" does then "with dates" needs to be first.

  • Thank You. My date responder have 4 parameters and that include date. My other responder do not have any parameters. I am passing * value in all date responder parameter so my question is should i have * as value or others. I can see data when i disable responders. I have tried with moving date responder first but still not working.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    It sounds like you have wild carded the first responder so that it will always match. The first responder should have the option "Correlate whenever present" checked so that if the date parameter is absent then the first responder will not match and the second responder will reply.

    Here is a simple example I configured, if I use a web browser to hit this endpoint

    http://localhost:9080/parameters?date=2020-01-24

    Then the first responder reply but if I remove date then the second responder replies.

  • Thank You. It looks like i am doing the same. I am passing date parameter all the time but it is not showing data. it is always showing me same error Bad Request - 400 which is false. It should only show when date is greater then current date. How should i handle it? Should i have logic as display data if date is not null?

  • I don't see "Correlate whenever present" check box. Correlate only when the list of parameters matches exactly and enable Correlation.

  • Ok. it looks like i can see data coming. I can also see error message with invalid date but i am not seeing Bad Request. it always shows 200 OK.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    You will probably need to add a custom correlation condition for the "greater than" case. The parameter correlation just checks for the presents of the parameter. Here is an example with a custom correlation that checks for a date in the format of "2020-02-01" is greater than today.

  • i don't see this issue without date responder.

  • Thank You. I tried with the information provided but i still see 200 OK.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    I didn't customize the status code on the first responder I only set an example response message so it would be easy to see. If you configure the response status to the code you desire for the greater than responder then that should model your desired setup.

  • i have setup the response status as 400 and message = Bad Request.

  • But i still see the 200 OK only.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    When I set the response status code to 400 on the "greater than" response I am getting the expected 400 when I hit the endpoint using a web browser. I can see in the network traffic that the response has the expected 400 status code.

    Here is the pva I configured.

  • Thank You. I can see the status code and message display when date is > today. I am also seeing same status code and message if my date is < today. Should i have responder before the asset or below?

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    What format are the dates that you are sending in the parameter? With the example I sent if the date is "2020-01-21" for example I am seeing the second response form the second responder.

  • i am also following as: 2020-01-27 for greater date.

  • 2020-01-20 if less then current date

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    In the example I sent the second responder, "default response" needs to come last because it doesn't have any correlation conditions. If the custom condition of the first responder fails then the second responder will be checked to see if it matches. In this case it will always match. For the matching to work the "default response" responder needs to come second.

  • Thank You. Now it is working as expected but it is throwing error for today's date. It should throw error if greater then today's date.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,
    It looks like the compare logic was checking for the wrong condition. I updated it to what I think wrongs for your case. It there are still issues look at the custom correlation on the first responder. The compare is what checks the dates and can be adjusted to return true of false based on what you need.

  • Thank You. Working now. I really appreciate helping me on this.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    You're welcome!

  • Thank you for helping. I have one more condition here is if date is greater then today or date is less then 7 days then it should throw the message. Currently i can see the message if date is greater then today's but it is not working if i add logic date is greater then 7 days from current date.

    it should satisfy if date is greater then current date and less then 7 days from current.

  • williammccusker
    williammccusker Posts: 642 ✭✭✭

    Hi,

    I am not sure I fully understand your condition but it should be a matter of writing the appropriate script. The groovy script in the example of does the greater than checking however you should be able to find some resources for how to script your desired date comparison, take a look at this if you need help getting started

    https://www.tutorialspoint.com/groovy/groovy_dates_times_compareto.htm