Forum Discussion

M_McDonald's avatar
M_McDonald
Super Contributor
15 years ago

Rewriting request in Mock Service OnRequest script

Hi -

I have the following code in the OnRequest script of a Mock Service to change the request to SOAP:

def req = mockRequest
def content = req.getRequestContent()

log.info "onRequest request content = " + content

def xml = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://soapui.sterling.com">
  <soap:Header/>
  <soap:Body>
      <soap1:dummyRequest>''' + content + '''</soap1:dummyRequest>
  </soap:Body>
</soap:Envelope>'''

req.setRequestContent(xml)

log.info req.requestContent


Logging indicates that the requestContent has been changed, but I still get a SOAP missing Envelope error like this:


 
     
        Server
        org.apache.xmlbeans.XmlException: Missing/Invalid SOAP Envelope, expecting [{http://schemas.xmlsoap.org/soap/envelope/}Envelope]
     

 



What am I doing wrong?

5 Replies

  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    nice idea! I've made some fixes to the underlying API so that you can now do the following to "rewrite" a GET request into a POST counterpart:

    def req = mockRequest
    if( req.httpRequest.method != "GET" )
      return

    def content = req.getRequestContent()

    log.info "onRequest request content = " + content

    def username = req.httpRequest.getParameter( "username" )
    def password = req.httpRequest.getParameter( "password" )

    def xml = '''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.example.org/sample/">
      <soapenv:Header/>
      <soapenv:Body>
          <sam:login>
            <username>''' + username + '''</username>
            <password>''' + password + '''</password>
          </sam:login>
      </soapenv:Body>
    </soapenv:Envelope>'''

    req.httpRequest.method = "POST"
    req.setRequestContent(xml)
    req.soapAction="http://www.example.org/sample/login"

    log.info req.requestContent


    This effectively transforms the GET request into a corresponding POST with corresponding Query parameters being inserted as strings in the the SOAP message. Maybe you can use this for your purposes?

    Will be in upcoming nightly build..

    regards!

    /Ole
    eviware.com
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    I may be able to make use of these changes, but what I am getting is a simple HTTP POST with XML (non-SOAP) which requires an XML (non-SOAP) reply.

    I was hoping to "fool" the service into thinking it was a SOAP request by rewriting it in the OnRequest script, then subsequently rewriting the response after dispatch to the XML format required.

    Thanks!
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Looks like I can set the Response to anything I want, so it appears to be working now.

    Thanks.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    Yes.. actually I think this is a really neat "work-around" for mocking REST services since you can use the standard SOAP-dispatching mechanism.. of course you still need to package all incoming requests into a SOAP request, which is kind of a drag.. but at least it works..

    thanks for the idea!

    /Ole
    eviware.com
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    I would find it vary helpful if support for plain HTTP requests in Mock Services and Mock Responses was added. ( Just pull out the SOAP internals  )