I am trying to push my JMeter result to Azure Cosmos DB through the rest API exposed from the azure portal.
To achieve so I am using JSR223 sampler (as my pre processer) to get the auth token to connect to cosmos db also using the stand script to generate the auth_token
(refer:https://github.com/MicrosoftCSA/documentdb-postman-collection/issues).
But I am getting Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: ReferenceError: "request" is not defined
.
Code Snippet:
var mastKey = "master_key_for_cosmos_db";
log.info("mastKey = " + mastKey);
var today = new Date();
var UTCstring = today.toUTCString();
var url = "uri_key_for_cosmos_db"
var strippedurl = url.replace(new RegExp('^https?://[^/]+/'), '/');
log.info("stripped Url = " + strippedurl);
var strippedparts = strippedurl.split("/");
var truestrippedcount = (strippedparts.length - 1);
var resourceId = "";
var resType = "";
if (truestrippedcount % 2) {
resType = strippedparts[truestrippedcount];
if (truestrippedcount > 1) {
var lastPart = strippedurl.lastIndexOf("/");
resourceId = strippedurl.substring(1, lastPart);
}
} else // its even (item request on resource)
{
resType = strippedparts[truestrippedcount - 1];
strippedurl = strippedurl.substring(1);
resourceId = strippedurl;
}
var verb = request.method.toLowerCase();
var date = UTCstring.toLowerCase();
var key = CryptoJS.enc.Base64.parse(mastKey);
var text = (verb || "").toLowerCase() + "\n" +
(resType || "").toLowerCase() + "\n" +
(resourceId || "") + "\n" +
(date || "").toLowerCase() + "\n" +
"" + "\n";
var signature = CryptoJS.HmacSHA256(text, key);
var base64Bits = CryptoJS.enc.Base64.stringify(signature);
var MasterToken = "master";
var TokenVersion = "1.0";
auth = encodeURIComponent("type=" + MasterToken + "&ver=" + TokenVersion + "&sig=" + base64Bits);
vars.put("authToken", auth);
anything I am doing wrong or missed?
This request
object belongs to Postman tool, you cannot use it in your JMeter scripts as it is not defined there.
You will also need to import this CryptoJS which might not be trivial.
Moreover the recommended language for scripting in JMeter is Groovy
So instead of trying to copy and paste someone's Postman code into JMeter which will not work I would rather recommend going into one of the following directions:
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments