You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在mountebank处理请求前修改请求体及SOAP的wsa:To标签

Modifying SOAP Request Body in Mountebank (Replacing <wsa:To> Tag)

Absolutely! Mountebank fully supports modifying request bodies before processing them—this is exactly the kind of flexibility it’s designed for in service virtualization workflows. Let’s break down how to replace the <wsa:To> value in your SOAP request and handle request body edits in general:

Step-by-Step Implementation

You’ll use Mountebank’s transformRequest hook, which lets you intercept and tweak the incoming request before it’s processed (whether that’s proxying to a target app or returning a mock response). Here’s a complete example imposter configuration:

{
  "port": 8080,
  "protocol": "http",
  "stubs": [
    {
      "predicates": [
        {
          "matches": {
            "method": "POST",
            "headers": {
              "Content-Type": "text/xml; charset=utf-8"
            }
          }
        }
      ],
      "responses": [
        {
          "transformRequest": function (request) {
            // Define your target application endpoint
            const targetEndpoint = "http://actual-target-ip:actual-port/your-endpoint";
            
            // Replace the <wsa:To> value using regex (works for fixed XML structures)
            request.body = request.body.replace(
              /<wsa:To>http:\/\/\[ip\]:\[port\]\/<\/wsa:To>/g,
              `<wsa:To>${targetEndpoint}</wsa:To>`
            );
            
            // Return the modified request for Mountebank to process next
            return request;
          },
          // Optional: Proxy the modified request to your target app
          "proxy": {
            "to": "http://actual-target-ip:actual-port/your-endpoint",
            "mode": "proxyOnce"
          }
        }
      ]
    }
  ]
}

Key Details to Note

  • Regex vs. XML Parsing: The example uses a regular expression since your <wsa:To> tag structure is fixed. If your SOAP XML is more dynamic (e.g., varying whitespace or nested elements), you could use an XML parsing library like xmldom (Mountebank allows importing external modules if you enable the allowInjection flag). For most simple cases, regex is more than sufficient.
  • Predicates: The matches predicate ensures we only apply this transformation to SOAP POST requests—adjust the headers or method if your test app uses different values.
  • Post-Transformation Actions: After modifying the request body, you can either proxy it to your target application (as shown) or return a custom mock response instead, depending on your testing needs.

Verifying the Change

To confirm the request is being modified correctly, you can add a console.log(request.body) inside the transformRequest function and check Mountebank’s logs, or use a tool like Wireshark to inspect the traffic leaving Mountebank.

内容的提问来源于stack exchange,提问作者Shivam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:18:10