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 loading them?
Example:
SQL table name: RequestHeader
SQL table Fields: ID, HeaderKey, HeaderValue
In ReadyAPI: A POST request where "Name" and "Value" properties under the "Request" tab in the request must be populated from the table.
"Name" = RequestHeader.HeaderKey
"Value" = RequestHeader.HeaderValue
There could be zero or more entries in this "RequestHeader" table for multiple, discrete test cases. The name and value of each header are unknown to the ReadyAPI request prior to being loaded to the "Name/Value" properties in the "Request" tab from the table at run time. I want to be able to load multiple records from the table to the request header so that value from the "HeaderKey" field in each record loads to a discrete "Name" property and the value from "HeaderValue" field in each record loads to the associated "Value" property. Can this be done in the provided ReadyAPI request object or will this require a request that is scripted in Groovy? If this request must be scripted, are there any examples of this specific scenario?
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.