菜单
开源

SOAP

虽然 k6 没有内置处理 SOAP 或 XML 数据的 API,但你仍然可以通过构建 SOAP 消息并使用 HTTP 请求 API 来轻松进行基于 SOAP 的 API 负载测试。

发起 SOAP 请求

JavaScript
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);
}