I am trying to grab a token and pass it into the GET requests.
The below works, but it’s grabbing a token every single time a request runs. Ideally I want to grab it once per run and pass it to the requests.
Any ideas on how to make that happen from the below code?
JavaScript
x
52
52
1
import http from "k6/http";
2
import { sleep } from "k6";
3
import { check } from "k6";
4
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
5
6
export let options = {
7
insecureSkipTLSVerify: true,
8
noConnectionReuse: false,
9
vus: 5,
10
duration: "10s",
11
};
12
13
14
var client_id = "clientId123";
15
var secret = "secret123";
16
var scope = "scope123";
17
18
19
export default () => {
20
21
var body =
22
"grant_type=client_credentials&client_id=" +
23
client_id +
24
"&client_secret=" +
25
secret +
26
"&scope=" +
27
scope;
28
29
var tokenResponse = http.post( "https://login.microsoftonline.com/tenantID123/oauth2/v2.0/token", body, { headers: { ContentType: "application/x-www-form-urlencoded"}});
30
var result = JSON.parse(tokenResponse.body);
31
var token = result.access_token;
32
33
check(tokenResponse, {
34
'is status 200': (r) => r.status === 200
35
})
36
37
var resp1 = http.get("url_1", {
38
headers: { Authorization: `Bearer ${token}` },
39
});
40
var resp2 = http.get("url_2", {
41
headers: { Authorization: `Bearer ${token}` },
42
});
43
44
check(resp1, {
45
'is status 200': (r) => r.status === 200,
46
})
47
check(resp2, {
48
'is status 200': (r) => r.status === 200,
49
})
50
51
};
52
Advertisement
Answer
In k6 lifecycle there are 4 stages. You need to use the proper one for your need.
Get exactly one token for the whole test
Can use setup
function
JavaScript
1
30
30
1
export function setup(){
2
var client_id = "clientId123";
3
var secret = "secret123";
4
var scope = "scope123";
5
6
var body =
7
"grant_type=client_credentials&client_id=" +
8
client_id +
9
"&client_secret=" +
10
secret +
11
"&scope=" +
12
scope;
13
14
var tokenResponse = http.post(
15
"https://login.microsoftonline.com/tenantID123/oauth2/v2.0/token",
16
body,
17
{ headers: { ContentType: "application/x-www-form-urlencoded" } }
18
);
19
var result = JSON.parse(tokenResponse.body);
20
var token = result.access_token;
21
22
return {token}
23
}
24
25
export default (data) => {
26
var token = data.token;
27
// Rest ...
28
29
}
30
Get one token for every VU (Virtual User)
Can use “init code.”
JavaScript
1
25
25
1
// ...
2
var body =
3
"grant_type=client_credentials&client_id=" +
4
client_id +
5
"&client_secret=" +
6
secret +
7
"&scope=" +
8
scope;
9
10
var tokenResponse = http.post(
11
"https://login.microsoftonline.com/tenantID123/oauth2/v2.0/token",
12
body,
13
{ headers: { ContentType: "application/x-www-form-urlencoded" } }
14
);
15
var result = JSON.parse(tokenResponse.body);
16
var token = result.access_token;
17
18
export default () => {
19
var resp1 = http.get("url_1", {
20
headers: { Authorization: `Bearer ${token}` },
21
});
22
// ...
23
};
24
25