简体   繁体   中英

How to load external js library in Jmeter?

I have the following code in a jsr223 sampler:

var key = "key";
var dateStamp = "20160329T134359Z";
var regionName = "us-east-1";
var serviceName = "execute-api";

   var kDate= Crypto.HMAC(Crypto.SHA256, dateStamp, "AWS4" + key, { asBytes: true})
   var kRegion= Crypto.HMAC(Crypto.SHA256, regionName, kDate, { asBytes: true });
   var kService=Crypto.HMAC(Crypto.SHA256, serviceName, kRegion, { asBytes: true });
   var kSigning= Crypto.HMAC(Crypto.SHA256, "aws4_request", kService, { asBytes: true });
   vars.put("AWSKey", kSigning);

Now when I run it i get this error: Response code: 500 Response message: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: ReferenceError: "Crypto" is not defined. (#6) in at line number 6

Obviously I dont have the crypto libs. However I am at loss on how to load them. I downloaded all the relavant js and put them in the /lib folder and still nothing. I downloaded this file: https://github.com/Boussetta/CryptoJS-v3.1.2 Which handles the functions in the code above but for the life of me I have not idea how to import it.

TIA

If you want to go for JavaScript - there are 2 options:

  1. Use Rhino load() method like:

    load("crypto.js")

  2. Use HmacUtils class from Apache Commons Codec from JavaScript

     var rawhmac = org.apache.commons.codec.digest.HmacUtils.hmacSha1(key,data) var encoded = org.apache.commons.codec.binary.Base64.encodeBase64String(rawhmac) 

However I would recommend going for option 3 - switch to "groovy" language instead of JavaScript, that way you will be able to:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM