generated from pricelees/issue-pr-template
92 lines
2.5 KiB
JavaScript
92 lines
2.5 KiB
JavaScript
import http from 'k6/http';
|
|
|
|
export const BASE_URL = __ENV.BASE_URL || 'http://localhost:8080';
|
|
|
|
export function generateRandomBase64String(length) {
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
let result = '';
|
|
for (let i = 0; i < length; i++) {
|
|
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function parseIdToString(response) {
|
|
try {
|
|
const safeJsonString = response.body.replace(/"(\w*Id|id)"\s*:\s*(\d{16,})/g, '"$1":"$2"');
|
|
return JSON.parse(safeJsonString);
|
|
} catch (e) {
|
|
console.error(`JSON parsing failed for VU ${__VU}: ${e}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function maxIterations() {
|
|
const maxIterationsRes = http.get(`http://localhost:8080/tests/max-iterations`)
|
|
if (maxIterationsRes.status !== 200) {
|
|
throw new Error('max-iterations 조회 실패')
|
|
}
|
|
|
|
return maxIterationsRes.json('count')
|
|
}
|
|
|
|
export function fetchUsers() {
|
|
const userCount = Math.round(maxIterations() * 0.5)
|
|
const userAccountRes = http.get(`http://localhost:8080/tests/users?count=${userCount}`)
|
|
|
|
if (userAccountRes.status !== 200) {
|
|
throw new Error('users 조회 실패')
|
|
}
|
|
|
|
return userAccountRes.json('results')
|
|
}
|
|
|
|
export function fetchStores() {
|
|
const storeIdListRes = http.get(`http://localhost:8080/tests/stores`)
|
|
|
|
if (storeIdListRes.status !== 200) {
|
|
throw new Error('stores 조회 실패')
|
|
}
|
|
|
|
return parseIdToString(storeIdListRes).results
|
|
}
|
|
|
|
export function login(account, password, principalType) {
|
|
const loginPayload = JSON.stringify({
|
|
account: account,
|
|
password: password,
|
|
principalType: principalType
|
|
})
|
|
const params = { headers: { 'Content-Type': 'application/json' }, tags: { name: '/auth/login' } }
|
|
|
|
const loginRes = http.post(`${BASE_URL}/auth/login`, loginPayload, params)
|
|
|
|
if (loginRes.status !== 200) {
|
|
throw new Error(`로그인 실패: ${__VU}`)
|
|
}
|
|
|
|
const body = parseIdToString(loginRes).data
|
|
if (principalType === 'ADMIN') {
|
|
return {
|
|
storeId: body.storeId,
|
|
accessToken: body.accessToken
|
|
}
|
|
} else {
|
|
return {
|
|
accessToken: body.accessToken
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getHeaders(token, endpoint) {
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
return { headers: headers, tags: { name: endpoint } };
|
|
}
|