Skip to content

Instantly share code, notes, and snippets.

@krisrice
Last active November 15, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisrice/d8c77308934ded45ee81ef8188fbeea3 to your computer and use it in GitHub Desktop.
Save krisrice/d8c77308934ded45ee81ef8188fbeea3 to your computer and use it in GitHub Desktop.
import http from "k6/http";
import encoding from 'k6/encoding';
/*
*
Run with No Auth
k6 run -e AUTH=NONE --vus 10 --iterations 100 k6.js
Run with OAuth
k6 run -e AUTH=OAUTH --vus 10 --iterations 100 k6.js
Run with BASIC Auth
k6 run -e AUTH=BASIC --vus 10 --iterations 100 k6.js
*
*/
var basicHash;
var bearerToken;
export function setup() {
if ( `${__ENV.AUTH}` === "NONE" ) {
console.log('NONE!!');
return;
} else if ( `${__ENV.AUTH}` === "BASIC" ) {
const BasicCredentials = 'klrice:klrice';
const BasicEncodedCredentials = encoding.b64encode(BasicCredentials);
const BasicOptions = {
headers: {
Authorization: `Basic ${BasicEncodedCredentials}`,
},
};
console.log('Basic!!! >> ' + JSON.stringify(BasicOptions));
return BasicOptions;
} if ( `${__ENV.AUTH}` === "OAUTH" ) {
//
// get the bearer token for later
//
const clientId = '_XlTKAsdFEeo5a-wWMZ4iQ..';
const clientSecret = 'x0tEQtZfHnp-LzCstMIlpQ..';
const encodedCredentials = encoding.b64encode(`${clientId}:${clientSecret}`);
const options = { headers: { Authorization: `Basic ${encodedCredentials}`, }, };
const authurl = 'http://localhost:8080/ords/klrice/oauth/token';
const requestBody = {grant_type: 'client_credentials', };
const res = http.post(authurl,requestBody,options);
const access_token=res.json().access_token; //console.log(access_token);
const authorization = `Bearer ${access_token}`;
const testOptions = { headers: { Authorization: `${authorization}`, }, };
console.log('OAuth!!! >> ' + JSON.stringify(testOptions));
return testOptions;
}
}
//
// run some tests
//
export default function(data) {
var response;
if ( `${__ENV.AUTH}` === "NONE" ) {
response = http.get("http://localhost:8080/ords/klrice/emp/emps");
} else if ( `${__ENV.AUTH}` === "BASIC" ) {
response = http.get("http://localhost:8080/ords/klrice/emp-with-auth/emps",data);
} if ( `${__ENV.AUTH}` === "OAUTH" ) {
response = http.get("http://localhost:8080/ords/klrice/emp-with-auth/emps",data);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment