Forum Discussion

richie's avatar
richie
Community Hero
6 years ago
Solved

Parameterize Assertion HTTP Header Values in Script Assertion?

Hey!

 

Ok - I queried the forum to find posts about asserting the values on HTTP headers using script assertions and found yet more code nmrao had put together to assert on custom header values

 

//all Rao's work

def expectedHTTPResponse = ['HTTP/1.1 200 OK']
def headers = messageExchange.response.responseHeaders
def actualHTTPResponse = headers['#status#']
assert expectedHTTPResponse == actualHTTPResponse

I am trying to assert a couple of custom headers have the appropriate values - but I want to parameterize the values if possible.

 

I have a GET request with the following URI:

/api/1/{namespace}/{dataset}?_format=csv

I have a Content-Disposition header with the following expected value

attachment; {namespace}_{dataset}.csv

So for my request

GET --> /api/1/authorised-establishments/sector?_format=csv

I'm expecting the Content-Disposition header will display

attachment; authorised-establishments_sector.csv

 

I am trying to use the script assertion Rao put together to parameterize the header value - but it's not working

 

I tried the following:

def expectedHTTPResponse = ['attachment; filename='${REST Request#namespace}'_'${REST Request#dataset}'.csv']
def headers = messageExchange.response.responseHeaders
def actualHTTPResponse = headers['Content-Disposition']
assert expectedHTTPResponse == actualHTTPResponse

and I also tried several variations using the 'context.expand' as follows:

def expectedHTTPResponse = ['attachment; filename=context.expand( '${REST Request#namespace}' )_context.expand( '${REST Request#dataset}' ).csv']
def headers = messageExchange.response.responseHeaders
def actualHTTPResponse = headers['Content-Disposition']
assert expectedHTTPResponse == actualHTTPResponse

Would anyone be able to point me in the right direction as to how I can successfully parameterize this script assertion?

 

I think I can see why its not working - but I don't know how to fix it.

 

Thanks to all!

 

richie :)

 

  • richie,
    What does below shows if you add at the start of the script?

    def ns = context.expand('${REST Request#namespace}')
    def ds = context.expand('${REST Request#dataset}')
    def value = "attachment; ${ns}_${ds}.csv" as String
    log.info value

3 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    richie,
    What does below shows if you add at the start of the script?

    def ns = context.expand('${REST Request#namespace}')
    def ds = context.expand('${REST Request#dataset}')
    def value = "attachment; ${ns}_${ds}.csv" as String
    log.info value
    • richie's avatar
      richie
      Community Hero

      Hey nmrao 

       

      so I updated the script assertion as follows:

      def ns = context.expand('${REST Request#namespace}')
      def ds = context.expand('${REST Request#dataset}')
      def value = "attachment; filename=${ns}_${ds}.csv" as String
      log.info value
      
      //def expectedHTTPResponse = ['attachment; filename=context.expand( '${REST Request#namespace}' )_context.expand( '${REST Request#dataset}' ).csv']
      //def headers = messageExchange.response.responseHeaders
      //def actualHTTPResponse = headers['Content-Disposition']
      //assert expectedHTTPResponse == actualHTTPResponse

      and it generated the following:

      Thu Feb 21 11:17:37 GMT 2019: INFO: attachment; filename=certification-nomenclature_certificates.csv
      

      so I thought about your code and tried the following:

       

      def ns = context.expand('${REST Request#namespace}')
      def ds = context.expand('${REST Request#dataset}')
      //def value = "attachment; filename=${ns}_${ds}.csv" as String
      //log.info value
      
      def expectedHTTPResponse = "attachment; filename=${ns}_${ds}.csv" as String
      def headers = messageExchange.response.responseHeaders
      def actualHTTPResponse = headers['Content-Disposition']
      assert expectedHTTPResponse == actualHTTPResponse

      and got the result

      assert expectedHTTPResponse == actualHTTPResponse | | | | | [attachment; filename=certification-nomenclature_certificates.csv] | false attachment; filename=certification-nomenclature_certificates.cs

       

      so I removed the 'as String' and wrapped the value in square brackets as 

       

      def ns = context.expand('${REST Request#namespace}')
      def ds = context.expand('${REST Request#dataset}')
      
      def expectedHTTPResponse = ["attachment; filename=${ns}_${ds}.csv"]
      def headers = messageExchange.response.responseHeaders
      def actualHTTPResponse = headers['Content-Disposition']
      assert expectedHTTPResponse == actualHTTPResponse

      and it worked! YAY! 

       

      thanks a lot man - that was doing my head in.

       

      For my education - can you clarify why I have to declare those variables (ns and ds) rather than use the original script?

       

      Again - thanks so much - you're saving my life here!

       

      richie

       

       

       

      \zz\

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        That was just to see what that statements were throwing.

        When something does not work, use log.info for that piece of code. So that, we know where to focus on.