K6 javascript如何传递xml参数

K6 javascript how to pass xml parameter

下面是解析 XML SOAP 请求的 K6 代码...所以我想将参数传递到 XML 数据中,如何从 CSV 或定义的变量中做到这一点。

import http from 'k6/http';
import { check, sleep } from 'k6';

const soapReqBody = `
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hs="http://www.holidaywebservice.com/HolidayService_v2/">
<soapenv:Body>
    <hs:GetHolidaysAvailable>
        <hs:countryCode>UnitedStates</hs:countryCode>
    </hs:GetHolidaysAvailable>
</soapenv:Body>
</soapenv:Envelope>`;

export default function () {
  // When making a SOAP POST request we must not forget to set the content type to text/xml
  const res = http.post(
    'http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx',
    soapReqBody,
    { headers: { 'Content-Type': 'text/xml' } }
  );

  // Make sure the response is correct
  check(res, {
    'status is 200': (r) => r.status === 200,
    'black friday is present': (r) => r.body.indexOf('BLACK-FRIDAY') !== -1,
  });

  sleep(1);
}

这是常规 JavaScript template literal (template string)。可以通过将 ${…} 插入字符串来插入变量:

let yourvariable = 42;
let xml = `<yourxml>${yourvariable}</yourxml>`;
console.log(xml); // <yourxml>42</yourxml>