Groovy 中读取响应体中的 UnicodeToChar 转换和正则表达式

问题描述

Http 请求将我作为响应正文返回给我

<html>
.......

</div>
<script type="text/javascript">
  var oktaData = {"redirectUri":"https\x3A\x2F\x2Fauth.mygateid.test\x2Foauth2\x2Fv1\x2Fauthorize\x2Fredirect\x3Fokta_key\x3Dxd0dasdlCKR8V7urrjwasdunAu3Pb7OJti8TQi7EApvB2NrdI","isMobileSso":false,"fromUri":"\x2Foauth2\x817\x2Fauthorize\x2Fredirect\x3Fokta_key\x3Dxd0dlCKR8V7urrjwunAu3Pb7OJti8TQi7EApvB2NrdI","isMobileClientLogin":false,"requestContext":{"target":{"clientId":"1238hasd3aghahj","name":"oidc_client","links":{},"label":"My Gate Id Testing","type":{}},"authentication":{"request":{"scope":"openid online_access","response_type":"code","state":"lcumasdasd8bWrfA","redirect_uri":"https://auth.rc.mygateid.test/Platform/login","response_mode":"query"},"protocol":{},"amr":[],"client":{"name":"My Gate Id Testing","id":"0oao1xsyelNgrTJ1sdsdv0h7"},"issuer":{"name":"RC","id":"ausdstr432l3MnsdsXPGu0h7","uri":"https://auth.mygateid.test/oauth2/austr432sdl3MnsXPGu0h7"}}},"signIn":{"logoText":"mygateid\x20logo","language":"en","consent":{"cancel":function(){window.location.href='https\x3A\x2F\x2Fauth.mygateid.test\x2Flogin\x2Fstep\x2Dup\x2Fredirect\x3Fstatetoken\x3D00Wv3Qt4F7oG7tXsdfLYz4sdfWg2dog4G28CkKtQsdOpFtiExg';}},"i18n":{"en":{"mfa.challenge.password.placeholder":"Password","help":"Help","password.forgot.email.or.username.tooltip":"Email\x20or\x20Username","needhelp":"Need\x20help\x20signing\x20in\x3F","primaryauth.username.placeholder":"Username","password.forgot.email.or.username.placeholder":"Email\x20or\x20Username","account.unlock.email.or.username.tooltip":"Email\x20or\x20Username","unlockaccount":"Unlock\x20account\x3F","account.unlock.email.or.username.placeholder":"Email\x20or\x20Username","primaryauth.password.placeholder":"Password","primaryauth.title":"Log\x20In","forgotpassword":"Forgot\x20password\x3F"}},"relayState":"\x2Foauth2\x2Fv1\x2Fauthorize\x2Fredirect\x3Fokta_key\x3Dxd0dlCKR8V7usdrrjwunAu3Pb7OJti8TQi7EApvB2NrdI","features":{"emailRecovery":true,"restrictRedirectToForeground":true,"deviceFingerprinting":true,"consent":true,"useDeviceFingerprintForSecurityImage":true,"customExpiredPassword":true,"router":true,"showPasswordToggleOnSignInPage":false,"securityImage":true,"autopush":true,"smsRecovery":false,"idpdiscovery":true,"selfServiceUnlock":false,"webauthn":true,"showPasswordRequirementsAsHtmlList":true,"registration":false,"rememberMe":true,"callRecovery":false,"multiOptionalFactorEnroll":true},"baseUrl":"https\x3A\x2F\x2Fauth.mygateid.test","assets":{"baseUrl":"https\x3A\x2F\x2Fop1static.oktacdn.com\x2Fassets\x2Fjs\x2Fsdk\x2Fokta\x2Dsignin\x2Dwidget\x2F4.5.2"},"customButtons":[],"idpdiscovery":{"requestContext":"\x2Foauth2\x2Fv1\x2Fauthorize\x2Fredirect\x3Fokta_key\x3Dxd0dlCKR8V7urrjwunAu3Pb7OJti8TQi7EApvB2NrdI"},"logo":"https\x3A\x2F\x2Fop1static.oktacdn.com\x2Ffs\x2Fbco\x2F1\x2Ffssd0t6nfsyzlvqJbto0h7","statetoken":"00Wv3Qt4F7oG7tXLYz4Wfsfdsdfg2dog4G28CkKtsdQOpFtiExg","helpLinks":{"help":"","forgotPassword":"","unlock":"","custom":[]},"piv":{}},"accountChooserdiscoveryUrl":"https\x3A\x2F\x2Flogin.okta.com\x2Fdiscovery\x2Fiframe.html","preventbrowserFromSavingOktaPassword":true};
....
....
</html>

我正在尝试阅读 oktaData.signIn.statetokenokta_key。我正在使用 JMeter 和 JSR233 后处理器(Groovy)。为了评估性能,我还想在 K6/Java Script 中读取上述值。

使用下面的代码,我可以在 Java Script (K6) 中成功读取值

 let body = response.body;
 let rawDataAfterConversion = unicodetochar(body.match(/oktaData = (.*);/)[1]);
 let oktaData = eval('(' + rawDataAfterConversion + ')');
 let oktaStatetoken = oktaData.signIn.statetoken;
 let okta_key_param = 'okta_key=';
 let okta_key = oktaData.signIn.relayState.substring(oktaData.signIn.relayState.indexOf(okta_key_param) + okta_key_param.length);

但是,我在 JMeter 中读取相同值时遇到问题。

以下是我尝试过的几件事。

def responseBody = prev.getResponseDataAsstring()
def match = responseBody =~ /oktaData = (.*);/

if (match.find()) {
    def value = match.group(0)
    log.info('------------------')
    log.info('a value=' + value) 
    log.info('------------------')
}
else
{
    vars.put('rawData',"ERROR")
}


vars.put('oktaData',(prev.getResponseDataAsstring() =~ /oktakey=(\w+);//)[0][1])

不幸的是,一切都不是很好。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)