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_parasoftbenken_parasoft Posts: 814 ✭✭✭
    edited January 22

    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.

  • jithenderreddyjithenderreddy Posts: 73
    edited January 22

    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

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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?

  • jithenderreddyjithenderreddy Posts: 73

    i am using local server.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

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

  • jithenderreddyjithenderreddy Posts: 73

    i am using postman for testing.

  • jithenderreddyjithenderreddy Posts: 73

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

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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?

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

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

  • jithenderreddyjithenderreddy Posts: 73

    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.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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)

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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)

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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?

  • jithenderreddyjithenderreddy Posts: 73

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

  • jithenderreddyjithenderreddy Posts: 73

    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.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    i don't see this issue without date responder.

  • jithenderreddyjithenderreddy Posts: 73

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

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

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

  • jithenderreddyjithenderreddy Posts: 73

    But i still see the 200 OK only.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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?

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

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

  • jithenderreddyjithenderreddy Posts: 73

    2020-01-20 if less then current date

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

    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.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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.

  • jithenderreddyjithenderreddy Posts: 73

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

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    You're welcome!

  • jithenderreddyjithenderreddy Posts: 73

    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.

  • williammccuskerwilliammccusker Posts: 355 ✭✭

    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

Sign In or Register to comment.