【问题标题】:Validating Oauth credentials in K6 load test script在 K6 负载测试脚本中验证 Oauth 凭据
【发布时间】:2022-09-05 06:10:41
【问题描述】:

我正在尝试获取令牌并将其传递给 GET 请求。

下面的工作,但每次请求运行时都会获取一个令牌。理想情况下,我想每次运行一次抓取它并将其传递给请求。

关于如何从下面的代码中实现这一点的任何想法?

import http from "k6/http";
import { sleep } from "k6";
import { check } from "k6";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";

export let options = {
  insecureSkipTLSVerify: true,
  noConnectionReuse: false,
  vus: 5,
  duration: "10s",
};

  
var client_id = "clientId123";
var secret = "secret123";
var scope = "scope123";


export default () => {

  var body =
  "grant_type=client_credentials&client_id=" +
  client_id +
  "&client_secret=" +
  secret +
  "&scope=" +
  scope;

  var tokenResponse = http.post( "https://login.microsoftonline.com/tenantID123/oauth2/v2.0/token", body, { headers: { ContentType: "application/x-www-form-urlencoded"}});
  var result = JSON.parse(tokenResponse.body);
  var token = result.access_token;

   check(tokenResponse, {
    'is status 200': (r) => r.status === 200
   })

  var resp1 = http.get("url_1", {
    headers: { Authorization: `Bearer ${token}` },
  });
  var resp2 = http.get("url_2", {
    headers: { Authorization: `Bearer ${token}` },
  });

  check(resp1, {
      'is status 200': (r) => r.status === 200,
    })
    check(resp2, {
      'is status 200': (r) => r.status === 200,
    })

};

【问题讨论】:

    标签: javascript oauth load-testing k6


    【解决方案1】:

    k6 lifecycle 中有 4 个阶段。您需要根据需要使用合适的。

    为整个测试获取一个令牌

    可以使用setup函数

    export function setup(){
      var client_id = "clientId123";
      var secret = "secret123";
      var scope = "scope123";
    
      var body =
        "grant_type=client_credentials&client_id=" +
        client_id +
        "&client_secret=" +
        secret +
        "&scope=" +
        scope;
    
      var tokenResponse = http.post(
        "https://login.microsoftonline.com/tenantID123/oauth2/v2.0/token",
        body,
        { headers: { ContentType: "application/x-www-form-urlencoded" } }
      );
      var result = JSON.parse(tokenResponse.body);
      var token = result.access_token;
    
      return {token}
    }
    
    export default (data) => {
        var token = data.token;
        // Rest ...
    
    }
    

    为每个 VU(虚拟用户)获取一个令牌

    可以使用“初始化代码”。

    // ...
    var body =
    "grant_type=client_credentials&client_id=" +
    client_id +
    "&client_secret=" +
    secret +
    "&scope=" +
    scope;
    
    var tokenResponse = http.post(
    "https://login.microsoftonline.com/tenantID123/oauth2/v2.0/token",
    body,
    { headers: { ContentType: "application/x-www-form-urlencoded" } }
    );
    var result = JSON.parse(tokenResponse.body);
    var token = result.access_token;
    
    export default () => {
      var resp1 = http.get("url_1", {
        headers: { Authorization: `Bearer ${token}` },
      });
      // ...
    };
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多