Forum Discussion

M_McDonald's avatar
M_McDonald
Super Contributor
16 years ago

Rewriting HTTP RequestStep response

I'd like to know if it would be possible to rewrite the response for an HTTP RequestStep with a script in the RequestFilter.afterRequest event.

I want to be able to apply assertions to an XML that I would construct via Groovy SQL from a database without needing to have a Mock Service.

If this is possible a small example would be appreciated.

(What I am really missing is some kind of generic Assertion step which could be applied to any request, data source, property, script result, etc. with the appropriate assertion types.)

Thanks.

8 Replies

  • Hi!

    sure its possible.. add a SubmitListener.afterSubmit event with (for example)

    if( submit.response == null )
      return

    // get existing response
    def content = submit.response.responseContent
    content = ...

    log.info( content )
    submit.response.responseContent =  content


    Does that suffice?

    regards!

    /Ole
    eviware.com
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    I tried the sample you provided, but the result in the Response window is still the original responseContent.
  • I've made a fix for this which will be in the upcoming nightly build. With the new build, use the following RequestFilter.afterRequest (NOTE: not SubmitListener.afterSubmit) script:



    if( context.httpResponse == null )
    return

    // get existing response

    def content = context.httpResponse.responseContent
    content = ...

    log.info content
    context.httpResponse.responseContent = content



    Regards,
    Dain
    eviware.com
  • Thomas_Bonds's avatar
    Thomas_Bonds
    Occasional Contributor
    I'm using SOAPUI 3.6.1 and I'm trying to run tests in a REST based project.

    This code works when I run the test step all by itself:

    if( context.httpResponse == null )
    return

    def content = context.httpResponse.responseContent

    assert content.ResponseStatusCode != 201



    But when I try to run the test step by running the test case that contains the test step, I get
    this error:

    Cannot get property 'ResponseStatusCode' on null object


    I just want to test if the response status code is 201.

    What am I doing wrong?
  • Thomas_Bonds's avatar
    Thomas_Bonds
    Occasional Contributor
    This seems to work:

    status = messageExchange.getResponseStatusCode()
    assert status == 201
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Sorry, you had != in your code, didn't read the description carefully! If you replace it with ==,

    assert messageExchange.responseStatusCode == 201 


    does my example work or are you still getting an exception?