Forum Discussion

Target_MN_-_soa's avatar
Target_MN_-_soa
Occasional Contributor
12 years ago

Validating header values in REST response

I have a need to validate a value coming back in header of REST response. Only available assertion against header seems to be http status code. Is there a way to do it? Groovy Script?

Thanks,
Sohail

7 Replies

  • Target_MN_-_soa's avatar
    Target_MN_-_soa
    Occasional Contributor
    That post does not explain how to capture header values in service response. I'm trying to validate some variable value in message header?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    this example is what I meant:

    assert messageExchange.responseHeaders["x-amz-id-1"] != null

    perhaps you could use

    def headers = messageExchange.responseHeaders["<name of your response header>"]
    assert headers != null && headers[0] != null && headers[0].indexOf( "<some substring>" ) != -1

    !?

    regards,

    /Ole
    SmartBear Software
  • Target_MN_-_soa's avatar
    Target_MN_-_soa
    Occasional Contributor
    Thanks, that worked as a script assertion.
    How could I do the same if I wanted to do it in a separate groovy script step and not as a part of script assertion?

    Thanks!
  • You can capture the script assertion header Value to properties and use those properties in another groovy script
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi,

    in a script step directly after the request step you would do

    // get results of previous teststep
    def result = testRunner.results[testRunner.results.size()-1]

    // assert desired header
    def headers = result.responseHeaders["<name of your response header>"]
    assert headers != null && headers[0] != null && headers[0].indexOf( "<some substring>" ) != -1


    regards,

    /Ole
    SmartBear Software
  • Necraatall's avatar
    Necraatall
    New Contributor

    try this sample code (valuesin UpperCases change):

     

    //Validate request nodes REST

    // the library import
    import groovy.json.JsonSlurper

    //Validate response nodes REST
    //def requestMessage = message.Exchange.request.requestContent
    def responseMessage = messageExchange.response.responseContent
    def json = new JsonSlurper().parseText(responseMessage)


    //assert node values
    // syntax: assert json.JSONPATH.ELEMENT.toString() COMPARE SYMBOLS LIKE IN IF (>,<,>=,<=,==,!=,you may use as comparing value "null")
    def headers = messageExchange.getResponseHeaders()
    assert ['HTTP/1.1 200 OK'] == headers["#status#"] : "Test Case HTTP Erorr: " + headers["#status#"]
    log.info "Header status" + headers["#status#"]
    assert ["YOURSERVER"] == headers["Server"] : "Test Case Server Erorr: " + headers["Server"]
    log.info "Server: " + headers["Server"]

    assert json.response.responseHeader.toString() != null : "responseHeader is empty: " + json.response.responseHeader //validation on responseHeader is not empty
    log.info "responseHeader is: " + json.response.responseHeader
    assert json.response.responseHeader.ELEMENT.toString() != null : "ELEMENT is empty: " + json.response.responseHeader.ELEMENT //validation on ELEMENT is not empty
    log.info "providerId is: " + json.response.responseHeader.providerId
    assert json.response.responseBody.returnCode.toString() == "0" : "returnCode is not 0: " + json.response.responseBody.returnCode
    log.info "returnCode is: " + json.response.responseBody.returnCode

     


    //check for attachments
    assert messageExchange.responseAttachments.length == 0 : "Response Attachments is not NULL"
    log.info "Number of atachments " + (messageExchange.responseAttachments.length)

     

    // some other thing
    /*
    testStepName = messageExchange.modelItem.testStep.name //to get the Test Step Name
    log.info testStepName
    xmlHold = messageExchange.responseContentAsXml.toString() //to store the response as Xml
    */