yandex-tank-ammo-generator
Version:
Cartridge generation for Yandex tank
42 lines (41 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Создание патрона
* @param method
* @param host
* @param path
* @param tag
* @param agent
* @param body
* @param headers
* @return {string}
*/
function createAmmo(method, host, path, tag, agent, body, headers) {
function getBytes(string) {
return Buffer.from(string).length;
}
let tmpFile = '';
tmpFile += `${method.toUpperCase().trim()} ${encodeURI(path)} HTTP/1.1\n`;
tmpFile += `Host: ${host}\n`;
tmpFile += `User-Agent: ${agent}\n`;
tmpFile += `Content-Type: application/json; charset=utf-8\n`;
const headersKeys = Object.keys(headers);
if (headersKeys.length) {
for (const item of headersKeys) {
tmpFile += `${item}: ${headers[item]}\n`;
}
}
switch (method.toUpperCase()) {
case 'GET':
case 'DELETE':
case 'PUT':
case 'POST': {
tmpFile += `Content-Length: ${getBytes(body)}\n\r\n`;
tmpFile += body;
break;
}
}
return `${getBytes(tmpFile) + 1} ${tag || ''} \n` + tmpFile + '\n\r\n';
}
exports.createAmmo = createAmmo;