Problem in JSR223 script JSR223 Sampler, while connecting to Azure Cosmos DB from Jmeter

Jyoti Prakash Mallick

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?

enter image description here

Dmitri T

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:

  1. Use Azure Cosmos DB Java SDK for SQL API from JSR223 Test Elements using Groovy language
  2. Or replicate the Postman's JavaScript in Groovy like it's described in How to Handle Dynamic AWS SigV4 in JMeter for API Testing article

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Scala JSR223 script using JMeter/Java context

JMeter - Can't use Java stream in JSR223 script

JMeter - Exception when using Beanshell as JSR223 Sampler with file

Jmeter JSR223 Sampler - Unable to Write Data To CSV File

Problem in JSR223 script, JSR223 PostProcessor : javax.script.ScriptException

JMeter - JSR223 Sampler missing character encoding

JMeter - Access JSR223 variable in HTTP sampler

Difference between a JSR223 Sampler and JSR223 PreProcessor

Jmeter - Using varible: from JDBC request into JSR223 PostProcessor

Jmeter - declare array variable in one JSR223 Sampler in order to access it in another JSR223 Sampler

JMeter Javascript language not available for JSR223 Sampler and WebDriver Sampler

Read json file in jsr223 sampler in jmeter and extract data

Generate random values in JSR223 sampler in JMeter

JMeter Load Tesing::Moving from WebDriver sampler To JSR223 sampler with groovy

unexpected token while reading xml to string in JSR223 sampler

JMeter saving document to MongoDb with Java BeanShell: Problem in JSR223 script Sampler

JMeter - groovy MultipleCompilationErrorsException in JSR223 Sampler

How to update User Defined Variable in JMeter in JSR223 Sampler?

Create JMeter Test Plan using JSR223 Sampler

In what ways can I access a variable defined in a JSR223 Sampler from inside a child JSR223 PostProcessor?

Blazemeter running Jmeter script - Problem in JSR223 script, Retrieve Tasks javax.script.ScriptException: groovy.lang.MissingMethodException

Extract value from jsr223 sampler

Jmeter: Access all results from all threadGroups from JSR223 Sampler or BeanShell Sampler in TeardownThread

JMeter - Error when using Jython/Python Engine in JSR223 Sampler "Cannot find Engine"

File declaration throwing error in JMeter, JSR223 sampler with groovy script

"Precondition Failed" for post request in JSR223 Sampler in JMeter

Code in JMeter JSR223 Sampler comments is executed

Error when trying to execute existing Jmeter script in Maven project that is written in Jsr223 sampler in groovy

upload a file using jsr223 sampler getting failure on JMeter