Forum Discussion

kasunrama's avatar
kasunrama
Occasional Visitor
3 years ago
Solved

Get Response Headers from script

Hi, I'm trying to execute soapui tests from my junit tests (junit integration)

How can I get the response headers for assertion?

I can get the raw response property from test step, but is there any way I can use the "messageExchange.responseHeaders" from my junit test? 

 

Thank you

  • Hello kasunrama,

     

    I have been successful at retrieving header values from a response by using the HarResponse of a service response.  I don't know if I understand your question completely, but the following is some groovy script code you can hack from to see if anything could work for you to capture a header value from a response.

     

    def harResponse = context.expand( '${HTTP Request GET authentication-service#HarResponse}' )
    
    log.info ' harResponse "' +  harResponse;
    def jsonHarResponse = new groovy.json.JsonSlurper().parseText(harResponse);
    log.info 'jsonHarResponse=' + jsonHarResponse;
    //jsonExpected = 'Bundle';
    //assert jsonContent.resourceType == jsonExpected, 'Expected "' + jsonExpected + '" but got "' + jsonContent.dbResult + '"';
    
    
    def headerResponseList = chooseNewResourceIdFromResponse();
    
    log.info ('Transform Response Headers:');
    log.info ('');
    subjectToken = '';
    headerResponseList.each {
       log.info ('   ' + it);
    
       if (it.contains('Subject-Token: ')) {
          subjectTokenList = it.tokenize(': ');
          subjectToken = subjectTokenList[1];
          subjectTokenList .each {
             log.info (' token  ' + it);
          };
       };
    };
    log.info ('');
    
    testRunner.testCase.testSteps["Properties"].setPropertyValue( "subjectToken", subjectToken);

     

    Regards,

    Todd

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3
    Please show what you have tried if that is not working?
    Hope are you running the ReadyAPI project? Do you have the assertion in the project / test step?
  • nmrao's avatar
    nmrao
    Champion Level 3
    By the way, not sure the need for running a junit tests while the same project can be run from commandline using testrunner utility.
    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Hi kasunrama ! Did you manage to solve this?

      If not, please provide us with more details!

  • TNeuschwanger's avatar
    TNeuschwanger
    Champion Level 2

    Hello kasunrama,

     

    I have been successful at retrieving header values from a response by using the HarResponse of a service response.  I don't know if I understand your question completely, but the following is some groovy script code you can hack from to see if anything could work for you to capture a header value from a response.

     

    def harResponse = context.expand( '${HTTP Request GET authentication-service#HarResponse}' )
    
    log.info ' harResponse "' +  harResponse;
    def jsonHarResponse = new groovy.json.JsonSlurper().parseText(harResponse);
    log.info 'jsonHarResponse=' + jsonHarResponse;
    //jsonExpected = 'Bundle';
    //assert jsonContent.resourceType == jsonExpected, 'Expected "' + jsonExpected + '" but got "' + jsonContent.dbResult + '"';
    
    
    def headerResponseList = chooseNewResourceIdFromResponse();
    
    log.info ('Transform Response Headers:');
    log.info ('');
    subjectToken = '';
    headerResponseList.each {
       log.info ('   ' + it);
    
       if (it.contains('Subject-Token: ')) {
          subjectTokenList = it.tokenize(': ');
          subjectToken = subjectTokenList[1];
          subjectTokenList .each {
             log.info (' token  ' + it);
          };
       };
    };
    log.info ('');
    
    testRunner.testCase.testSteps["Properties"].setPropertyValue( "subjectToken", subjectToken);

     

    Regards,

    Todd