Forum Discussion

em_qdx's avatar
em_qdx
Occasional Contributor
3 years ago
Solved

Add multiple values from a table to request headers

Is there a method for adding values from multiple records from a table (or any source) to a request name/value combination (header for instance) without knowing the "Name" and "Value" values prior to...
  • ChrisAdams's avatar
    ChrisAdams
    3 years ago

    Hi,

     

    I think I have something for you.

     

    I have built a basic test that looks like this....

     

    In relation to yours, Mock Headers equals you Test Header select.  The Rest Request is your Eligibility Request.  So, one new step before and two new steps in between.

     

    The first step, Initialise Headers allows us to blow away any headers from a previous test or previous iteration of your outer loop.  It looks like this...

    // Need this to build the object for the headers...
    import com.eviware.soapui.support.types.StringToStringMap;
    
    // Get the test step of interest...
    def testStep = testRunner.testCase.getTestStepByName("REST Request");
    
    // Get the request object fo rthe test step
    def testRequest = testStep.testRequest;
    
    // Create an empty map.
    def newHeaders = new StringToStringMap();
    
    // Add the empty map to blow away previously added headers.
    testRequest.setRequestHeaders(newHeaders);

     

    As I don't have the db set-up you have, my Mock Headers data source has several rows.  Think of these as the next batch of headers to apply to the test request.

     

    Here is what my datasource looks like....

     

    The next step is Set Headers.  Here's the Groovy script.

    // Need this to build the object for the headers...
    import com.eviware.soapui.support.types.StringToStringMap;
    
    // Get the test step of interest...
    def testStep = testRunner.testCase.getTestStepByName("REST Request");
    
    // Get the request object for the test step
    def testRequest = testStep.testRequest;
    
    // Grab the header and value from the Mock Header datasource for this iteration only...
    def header = context.expand( '${Mock headers - DataSource#header}' );
    def value = context.expand( '${Mock headers - DataSource#value}' );
    
    // Get headers we've added so far.  On the first iteration this will be empty.
    def currentHeaders = testRequest.getRequestHeaders();
    
    // Add the header from the current iteration
    currentHeaders.put(header, value);
    
    // Put the request headers back into the request.
    // Note : Default headers are still there.  E.g. Connection, Accept-Encdoding, but you could use this method to override the default set if you so wish
    testRequest.setRequestHeaders(currentHeaders);
    
    // Let's have a look.  For each iteration, this log statement will get bigger.
    log.info(testRequest.getRequestHeaders());
    log.info("Mock header '${header}' set to value '${value}'.  ");
    

     

    Why do I get the headers and then add to it?  My earlier attempt just added the next.  But the effect was that it overridden previously add mock headers.  So the header applied to the test request was always the last.  So, instead, get what's there; add to it; put them back in one go.

    The next step is my datasource loop.  I've bound this to the Mock Headers data source and Set Headers Groovy.  This makes my test loop over each mock header before moving on to call the request....

     

    The datasource loop looks like....

     

    No need to look at the Rest Request yet.

     

    Run this test case as a whole and look at the Script Logs...

     

    Let's have a look at the Raw tab for the request now the test has run.

     

    Suggest you create a new test case and copy the above.  Have a play then apply to yours as it will need some tweaking for the datasource values.