How to load external js library in Jmeter?

自闭症网瘾萝莉.ら 提交于 2019-12-23 16:19:09

问题


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


回答1:


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:

  • Re-use Amazon authentication samples in your test
  • Get maximum performance and confidence as groovy scripts can be compiled while other languages are interpreted so groovy implementation will take less resources and will work faster. See Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For! article for more details.


来源:https://stackoverflow.com/questions/36293800/how-to-load-external-js-library-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!