I perform a performance testing of the application with an endpoint that should have Authorization.
Authorization to this application was done by the AWS Cognito tool.
The tool that I use for performance testing is k6 with Webpack.
But when I try to test the endpoint, I catch an exception from
await client.initiateAuth(params)
as ReferenceError: URL is not defined
. Where client is CognitoIdentityProvider
During debugging I found that AWS await client.config.endpoint()
return me the correct URL of Cognito when I run it without Webpack. But with Webpack it is throwing the same error.
Could someone face the same issue? How to resolve it?
Code examples:
index.js
import http from 'k6/http';
import { Rate, Trend } from 'k6/metrics';
import { check } from 'k6';
import { getToken } from "./cognito-service";
var errors = new Rate("errors");
var trends = new Trend("trends");
export const options = {
vus: 1,
duration: '10s',
};
const token = getToken();
const params = {
headers: {
'Authorization': 'Bearer ' + token,
},
};
export default function () {
const res = http.get('https://myurl.com/api/need/to/test', params);
check(res, { 'status was 200': (r) => r.status == 200 });
errors.add(res.error_code);
trends.add(res.timings.sending + res.timings.receiving);
}
webpack.config.js
const path = require('path');
module.exports = {
mode: 'production',
entry: {
login: './index.js',
},
output: {
path: path.resolve(__dirname, 'build'),
libraryTarget: 'commonjs',
filename: '[name].bundle.js',
},
module: {
rules: [
{ test: /\.js$/, use: 'babel-loader' }
],
},
target: 'web',
externals: /k6(\/.*)?/,
};
cognito-service.js
import { CognitoIdentityProvider } from "@aws-sdk/client-cognito-identity-provider";
export const getToken = async () => {
const clientId = "XXXXXXXXXX";
const params = {
AuthFlow: "USER_PASSWORD_AUTH",
ClientId: clientId,
AuthParameters: { "USERNAME": "xxxxxx@gmail.com", "PASSWORD": "Test123$" }
};
const client = new CognitoIdentityProvider({ region: "eu-central-1" });
try {
console.log(client.config);
const response = await client.initiateAuth(params);
console.log(`Response is -> ${response}`)
return response;
} catch(error) {
console.log(`Err is -> ${error}`);
}
}
Also attached is the log::
> k6 run build/login.bundle.js
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
INFO[0000] {"apiVersion":"2016-04-18","disableHostPrefix":false,"logger":{},"serviceId":"Cognito Identity Provider","runtime":"browser","requestHandler":{"configProvider":{}},"tls":true,"isCustomEndpoint":false,"systemClockOffset":0,"signingEscapePath":true} source=console
INFO[0000] Err is -> ReferenceError: URL is not defined source=console
execution: local
script: build/login.bundle.js
output: -
scenarios: (100.00%) 1 scenario, 1 max VUs, 40s max duration (incl. graceful stop):
* default: 1 looping VUs for 10s (gracefulStop: 30s)
INFO[0000] {"apiVersion":"2016-04-18","disableHostPrefix":false,"logger":{},"serviceId":"Cognito Identity Provider","runtime":"browser","requestHandler":{"configProvider":{}},"tls":true,"isCustomEndpoint":false,"systemClockOffset":0,"signingEscapePath":true} source=console
INFO[0000] Err is -> ReferenceError: URL is not defined source=console
INFO[0000] {"apiVersion":"2016-04-18","disableHostPrefix":false,"logger":{},"serviceId":"Cognito Identity Provider","runtime":"browser","requestHandler":{"configProvider":{}},"tls":true,"isCustomEndpoint":false,"systemClockOffset":0,"signingEscapePath":true} source=console
INFO[0000] Err is -> ReferenceError: URL is not defined source=console