Forum Discussion

max1965's avatar
max1965
Contributor
13 years ago

post/get http request with groovy

Actualy in my soapui project I sent post and get HTTP requests using the HTTP Test Request step. Follow are some exmples of the requests:

GET

http://138.132.115.21:80/AuthInterface/ ... essionType

=APT&msgType=START HTTP/1.1
Accept-Encoding: gzip,deflate
Host: 138.132.115.21:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

POST http://138.132.115.21:80/CxInterface/multimediaAuth HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 388
Host: 138.132.115.21:80
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

input=<?xml version="1.0" encoding="UTF-8"?><multimediaAuthRequest xmlns="UdbCxInterface"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="CxInterface

multimediaAuthRequest.xsd"><PublicIdentity>0803300000</PublicIdentity><username>0803300000</us

ername><ServerName>84.175.19.217</ServerName><version>1.1</version><msgType>REGISTER</msgType>

</multimediaAuthRequest>

POST https://138.132.115.23:443/provEngine_pkg3/provisioning HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: ""
Content-Length: 2915
Host: 138.132.115.23:443
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<acceptRequest><request><![CDATA[<spml:batchRequest xmlns:spml='urn:oasis:names:tc:SPML:1:0' xmlns:dsml='urn:oasis:names:tc:DSML:2:0:core' requestID='20130228104143645' processing='urn:oasis:names:tc:SPML:1:0#sequential' onError='urn:oasis:names:tc:SPML:1:0#exit'>
<spml:addRequest requestID='20130228104143645-0001'>
<spml:identifier type='urn:oasis:names:tc:SPML:1:0#GUID'>
<spml:id>042823880498</spml:id>
</spml:identifier>
<spml:attributes>
<dsml:attr name='district'>
<dsml:value>0428</dsml:value>
</dsml:attr>
<dsml:attr name='svcType'>
<dsml:value>APT</dsml:value>
</dsml:attr>
<dsml:attr name='isMaster'>
<dsml:value>Y</dsml:value>
</dsml:attr>
<dsml:attr name='cli'>
<dsml:value>042823550249</dsml:value>
</dsml:attr>
<dsml:attr name='cliDistrict'>
<dsml:value>0428</dsml:value>
</dsml:attr>
<dsml:attr name='outboundProxy'>
<dsml:value>138.132.116.39</dsml:value>
</dsml:attr>
<dsml:attr name='presenceServer'>
<dsml:value>138.132.106.13</dsml:value>
</dsml:attr>
<dsml:attr name='applicationServer'>
<dsml:value>sip:10.19.20.8</dsml:value>
</dsml:attr>
<dsml:attr name='administrativeStatus'>
<dsml:value>SUSPENDED</dsml:value>
</dsml:attr>
<dsml:attr name='operationalStatus'>
<dsml:value>RUNNING</dsml:value>
</dsml:attr>
<dsml:attr name='geographicArea'>
<dsml:value>milan000</dsml:value>
</dsml:attr>
<dsml:attr name='exchangeId'>
<dsml:value>1234567890</dsml:value>
</dsml:attr>
<dsml:attr name='accessDevice'>
<dsml:value>AG</dsml:value>
</dsml:attr>
<dsml:attr name='userDevice'>
<dsml:value>WIFI</dsml:value>
</dsml:attr>
<dsml:attr name='sipKey'>
<dsml:value></dsml:value>
</dsml:attr>
</spml:attributes>
</spml:addRequest>
<spml:modifyRequest requestID='20130228104143645-0002'>
<spml:operationalAttributes>
<dsml:attr name='svcType'>
<dsml:value>APT</dsml:value>
</dsml:attr>
<dsml:attr name='isRoutingNumber'>
<dsml:value>N</dsml:value>
</dsml:attr>
<dsml:attr name='isMaster'>
<dsml:value>Y</dsml:value>
</dsml:attr>
</spml:operationalAttributes>
<spml:identifier type='urn:oasis:names:tc:SPML:1:0#GUID'>
<spml:id>042823880498</spml:id>
</spml:identifier>
<spml:modifications>
<dsml:modification name='STS_disablingClass' operation='add'>
<dsml:value>sts_subscription=access</dsml:value>
<dsml:value>disablingClass=C10</dsml:value>
</dsml:modification>
</spml:modifications>
</spml:modifyRequest>
</spml:batchRequest>]]></request></acceptRequest></s:Body>
</s:Envelope>

It is possible sent the same request using a groovy script ? Are available some examples on how write the groovy request ?
  • I write the following groovy script:

    import groovyx.net.http.HTTPBuilder
    import static groovyx.net.http.Method.GET
    import static groovyx.net.http.ContentType.TEXT
    def Indirizzo_F5_FE = context.expand( '${#Global#Indirizzo_F5_FE}' )
    def Porta_F5_FE = context.expand( '${#Global#Porta_F5_FE}' )
    def props = testRunner.testCase.getTestStepByName( "Properties" )
    def cli = props.getPropertyValue("cli");
    def ippresenza = props.getPropertyValue("ippresenza");
    // log.info ("$Indirizzo_F5_FE, $Porta_F5_FE, $cli, $ippresenza")
    def http = new HTTPBuilder( "http://${Indirizzo_F5_FE}:${Porta_F5_FE}/AuthInterface/acctRequest?cli=${cli}&ip=${ippresenza}&sessionType=APT&msgType=START" )
    http.request(GET,TEXT) { req ->
    headers.'Connection' = 'Keep-Alive'
    headers.'User-Agent' = 'SoapUI 4.5.1'
    response.success = { resp, reader ->
    assert resp.status == 200
    System.out << reader
    }
    }

    The request is correctly sent, and in the soapui http.log is trace the request/answer:

    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "GET /AuthInterface/acctRequest?cli=0804000000&ip=206.100.0.110&sessionType=APT&msgType=START HTTP/1.1[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "Accept: text/plain[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "User-Agent: SoapUI 4.5.1[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "Connection: Keep-Alive[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "Host: 138.132.115.21:80[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "Accept-Encoding: gzip,deflate[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:>> "[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "HTTP/1.1 200 OK[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "Date: Thu, 28 Feb 2013 14:45:15 GMT[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "Server: WebLogic Server 8.1 SP2 Fri Dec 5 15:01:51 PST 2003 316284 [\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "Content-Length: 84[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "Content-Type: text/html[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "[\r][\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "<?xml version="1.0" ?>[\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "<ACA errorCode="5000">[\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "<errorMessage></errorMessage>[\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "</ACA>[\n]"
    Thu Feb 28 15:45:15 CET 2013:DEBUG:<< "[\n]"

    When I used the HTTP Test Request to send the get request, I have the following groovy script to analyze the response:

    import com.eviware.soapui.support.xml.XmlUtils
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    xmlResponseHolder = groovyUtils.getXmlHolder("ACCT START UMA#ResponseAsXML") //ACCT START UMA is the test step name
    xmlBody = new XmlSlurper().parseText(xmlResponseHolder.getXml())
    errorCode = xmlBody.@errorCode
    def props = testRunner.testCase.getTestStepByName( "Properties" )
    def cli = props.getPropertyValue("cli");
    if ( errorCode != "5000" )
    {
    log.info("ACCT START UMA $cli - wrong errorCode: $errorCode")
    testRunner.fail( "errorCode" )
    }

    How can now analyze the answer if i use the groovy script ?
  • I fix the problem with get http. I use the following groovy script to send the get request:

    import groovyx.net.http.HTTPBuilder
    import static groovyx.net.http.Method.*
    import static groovyx.net.http.ContentType.*
    def Indirizzo_F5_FE = context.expand( '${#Global#Indirizzo_F5_FE}' )
    def Porta_F5_FE = context.expand( '${#Global#Porta_F5_FE}' )
    def props = testRunner.testCase.getTestStepByName( "Properties" )
    def cli = props.getPropertyValue("cli");
    def ippresenza = props.getPropertyValue("ippresenza");
    def http = new HTTPBuilder( "http://${Indirizzo_F5_FE}:${Porta_F5_FE}/AuthInterface/acctRequest?cli=${cli}&ip=${ippresenza}&sessionType=APT&msgType=START" )
    http.request(GET,TEXT){ req ->
    headers.'Connection' = 'Keep-Alive'
    headers.'User-Agent' = 'SoapUI 4.5.1'
    response.success = { resp, reader ->
    assert resp.status == 200
    ret = reader.getText()
    }
    }
    props.setPropertyValue("answer", ret);

    and the following groovy script to check the response:

    import com.eviware.soapui.support.xml.XmlUtils
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def props = testRunner.testCase.getTestStepByName( "Properties" )
    def answer = props.getPropertyValue("answer");
    xmlBody = new XmlParser().parseText(answer)
    errorCode = xmlBody.@errorCode
    def cli = props.getPropertyValue("cli");
    if ( errorCode != "5000" )
    {
    log.info("ACCT START UMA $cli - wrong errorCode: $errorCode")
    testRunner.fail( "errorCode" )
    }

    Now I try to send a post http request, but I'm not able to build the correct request to send the following post boby:

    input=<?xml version="1.0" encoding="UTF-8"?><UNCMARRequest xmlns="UdbCxInterface"xmlns:xsi="http://www.w3.org/2001 /XMLSchema-instance"xsi:schemaLocation="UdbCxInterface C:\UNCMARRequest.xsd"><username>${id}</username><resourceIp>${ippresenza}</resourceIp><msgType>REGISTER</msgType><methodType>UserProfile</methodType><version>${versioneCx}</version><uncId>TIMUNC1</uncId><olo>TIM</olo></UNCMARRequest>

    Someone could help me ? Thanks.
  • swlinn's avatar
    swlinn
    Occasional Contributor
    Were you able to get the POST request working? I've tried your GET request, substituting my own backend url, and even that seems to hang on the HTTPBuilder invocation. I'm using http-builder-0.7.1.jar which I placed in the SoapUI 5.0.0 install/lib directory.

    Thanks,
    Steve