1

应用程序使用 SSO 登录进行访问。

如果 SSO 登录不起作用,浏览器会要求提供凭据以登录应用程序。

为了使用 K6 工具对此类应用程序进行负载测试,我试图了解如何编写脚本以成功登录。

我已尝试将凭据作为 URL 的一部分传递,如下面的代码所示,并尝试作为 NTLM 身份验证。

下面是我的脚本;

const username = "global\\user001", 
    password = "Password";

let pURL="abc.xyz.dev";

let req, res;
req = [{
 "method": "get",
 "url": `https://${username}:${password}@${pURL}/pqrs`,
 "params": {
  "headers": {
   "Host": ""+pURL+"",
   "Connection": "keep-alive",
   "Pragma": "no-cache",
   "Cache-Control": "no-cache",
   "Upgrade-Insecure-Requests": "1",
   "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
   "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
   "Accept-Encoding": "gzip, deflate, br",
   "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
  },
  "auth":"ntlm",
 }
}];
res = http.batch(req);

响应的预期 HTTP 状态代码是 301,但我目前得到 401:未验证。

但是,如果上述请求中的 URL 更改为:

"url": `https://${username}:${password}@${pURL}/pqrs`,

我收到错误(如下),因为它不能接受带有提到的另一个域的用户名。

ERRO[0002] GoError: parse https://global\user001:Password@abc.xyz.dev/pqrs: net/url: invalid userinfo

我做错了什么,我该如何解决?

4

1 回答 1

0

您有\您的用户名,因此您需要对其进行 url 编码。你可以使用encodeURI如下图:

import http from "k6/http";

const username = encodeURI("global\\user001"),
            password = "Password";
export default function() {

    let pURL="httpbin.org";

    let req, res;
    req = [{
         "method": "get",
         "url": `https://${username}:${password}@${pURL}/basic-auth/${username}/${password}`,
         "params": {
               "headers": {
                      "Host": ""+pURL+"",
                      "Connection": "keep-alive",
                      "Pragma": "no-cache",
                      "Cache-Control": "no-cache",
                      "Upgrade-Insecure-Requests": "1",
                      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36",
                      "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                      "Accept-Encoding": "gzip, deflate, br",
                      "Accept-Language": "en-GB,en-US;q=0.9,en;q=0.8"
                     },
               "auth":"ntlm",
              }
    }];
    res = http.batch(req);

    console.log(JSON.stringify(res[0].body));
}

于 2019-07-10T07:42:09.730 回答