应用程序使用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
我在做什么错,我该如何解决?
答案 0 :(得分: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));
}