cancel
Showing results for 
Search instead for 
Did you mean: 

determine the type of a request programmatically

SOLVED
Highlighted
Frequent Contributor

determine the type of a request programmatically

Hello,

I'm trying to find a way, by groovy scripting, to determine which is the type of a request in my project (PUT, POST, GET, etc.). I could only go as far as parsing the step and get its rest method (getRestMethod() on a RestTestRequestStep but it does not always give me relevant information.

for some of my APIs it gives setterXX or getterYY which is exactly what I need but for many others it does not give this type of information.

is there a property that clearly provide this information ?

 

thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Frequent Contributor

Re: determine the type of a request programmatically

Hello,

finally I found out the information.

unfortunately, in the project under test, som PUT requests are, in fact, get requests so I have an additional test to do to determine the 'functional' role of the request.

To get the rest method of a REST request step :

testRunner.testCase.testSuite.project.testSuiteList.each
{
	suite ->
	name = suite.getName()
    
    suite.testCaseList.each{
        TC -> 
        log.info "test case ****** " + TC.getName()
        TC.testSteps.each{

            if (it.getValue().config.type.equals("restrequest"))
            {
                method_functional_type = "unknown"
                method_functional_type = it.getValue().getRestMethod().getMethod().toString() // provides 'PUT', 'GET', etc.
                
                if (method_functional_type != "GET")
                {
                    // do an additional check
					// ie. the API /m2m/fim/items/fim:users:settings:manager/operations/getServerTimestamp
					// it is a PUT request but it is senseless to not use the output                    
                    op = it.getValue().getOperation().toString()
                    if (op.split("\\/")[-1].take(3) == "get")
                    {
                        // the PUT request returns relevant data that has to be used
                        method_functional_type = "GET"
                    }
                }                
                
                
                if (method_functional_type == "GET")
                {
                    if (TC.tearDownScript == null){
                        log.info "update teardown script for $name - ${TC.name}"
                        // if there is no teardown script available, set the generic teardown script for GET API's
                        TC.setTearDownScript('testCase.setPropertyValue("testResponse", context.response)')
                    }
                }
            }
        }
    }
}

View solution in original post

9 REPLIES 9
Highlighted
Frequent Contributor

Re: determine the type of a request programmatically

I have to types of requests. For rest requests, it can do 'getRestMethod' which gives information containing 'setter' or 'getter' or, at least, a string that contains the information. It's not straightforward but it works.

The problem is with http requests, in fact I found the information in the config data of the request (which is a xml structure) but I do not manage to parse it correctly.

I can get the highest level information by just pointing to it, ie. config.type = httprequest, I can also get name, id ... but the config method is not at the same level.

I get this information, which is a class com.eviware.soapui.config.impl.TestStepConfigImpl

<xml-fragment type="httprequest" name="Request 1" id="373fc2ed-1b98-4ce7-9695-dfcca6469ce8" xmlns:con="http://eviware.com/soapui/config">
  <con:settings/>
  <con:config method="PUT" xsi:type="con:HttpRequest" id="b85fadf8-b989-4b71-ba0b-f0c5aa73e5b4" name="Request 1" postQueryString="false" mediaType="application/json" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <con:settings>
      <con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers">&lt;entry key="Accept" value="application/json" xmlns="http://eviware.com/soapui/config"/></con:setting>
    </con:settings>
...

How can I get the config method field content ?

 

I tried to use xmlparser and xmlslurper but it does not accept the data from TestStepConfigImpl object.

Is there a way to cast it ? is there a particular way to use xmlparser or slurper to achieve this ?

 

Highlighted
Community Hero

Re: determine the type of a request programmatically

If you have script that which was not working?


Regards,
Rao.
Highlighted
Community Manager

Re: determine the type of a request programmatically

@krogold , any luck with solving this?


Sonya Mihaljova
Community and Education Specialist
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
Frequent Contributor

Re: determine the type of a request programmatically

well, so far no 😞

it's not critical though, I'll try to investigate this thoroughly when I'll have more spare time

Highlighted
Community Hero

Re: determine the type of a request programmatically

What is the use case?
The script you came up with so far, though it is not working? Would help to understand the issue.


Regards,
Rao.
Highlighted
Frequent Contributor

Re: determine the type of a request programmatically

it's just that I have some REST requests and some HTTP requests, and I'd like to determine the type of request (GET or PUT) in order to initialize properties and other features for each requests

Highlighted
Community Hero

Re: determine the type of a request programmatically

Here is my understanding:
If you have the test step already added to the test case, all the properties such as parameters if GET or request body if POST or PUT being inherited from service definition. So it is easy if the context or test case structure,

Where you are trying to write the code along with what you have currently would help to understand it better?



Regards,
Rao.
Highlighted
Frequent Contributor

Re: determine the type of a request programmatically

Hello,

finally I found out the information.

unfortunately, in the project under test, som PUT requests are, in fact, get requests so I have an additional test to do to determine the 'functional' role of the request.

To get the rest method of a REST request step :

testRunner.testCase.testSuite.project.testSuiteList.each
{
	suite ->
	name = suite.getName()
    
    suite.testCaseList.each{
        TC -> 
        log.info "test case ****** " + TC.getName()
        TC.testSteps.each{

            if (it.getValue().config.type.equals("restrequest"))
            {
                method_functional_type = "unknown"
                method_functional_type = it.getValue().getRestMethod().getMethod().toString() // provides 'PUT', 'GET', etc.
                
                if (method_functional_type != "GET")
                {
                    // do an additional check
					// ie. the API /m2m/fim/items/fim:users:settings:manager/operations/getServerTimestamp
					// it is a PUT request but it is senseless to not use the output                    
                    op = it.getValue().getOperation().toString()
                    if (op.split("\\/")[-1].take(3) == "get")
                    {
                        // the PUT request returns relevant data that has to be used
                        method_functional_type = "GET"
                    }
                }                
                
                
                if (method_functional_type == "GET")
                {
                    if (TC.tearDownScript == null){
                        log.info "update teardown script for $name - ${TC.name}"
                        // if there is no teardown script available, set the generic teardown script for GET API's
                        TC.setTearDownScript('testCase.setPropertyValue("testResponse", context.response)')
                    }
                }
            }
        }
    }
}

View solution in original post

Highlighted
Community Manager

Re: determine the type of a request programmatically

@krogold Great job figuring this one out! Kudos!


Sonya Mihaljova
Community and Education Specialist
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements
Script Wanted
The new challenge is here!

Create a script to сhange an XPath Match assertion to a JSONPath Match assertion for the TechCorner tag!


Scripts created in July:
- A script to generate an email when an assertion fails
- A script to change the request body before sending it to the server
Top Kudoed Authors