wiremock / wiremock

A tool for mocking HTTP services
https://wiremock.org/
Apache License 2.0
6.18k stars 1.41k forks source link

Ignore order of xml nodes on same level when matching request body #2747

Closed viktormelnychuk closed 2 weeks ago

viktormelnychuk commented 1 month ago

Ignores the order of nodes on the same level by applying secondary sorting by the text content of the node. This allows to match requests where there can be multiple same tags in arbitrary order Given example configuration:

    .withRequestBody(equalToXml("<body>" +
            "   <entry>1</entry>" +
            "   <entry>2</entry>" +
            "</body>", false, true))

Will match

<body>
    <entry>2</entry>
    <entry>1</entry>
</body>

and match

<body>
    <entry>1</entry>
    <entry>2</entry>
</body>

Added property to enable secondary sort by node text content. The property name is ignoreOrderOfSameNode. Tried to follow pattern similar to equalToJson implementation. Update existing equalToXml static methods to pass null value as the parameter to preserve existing behavior. Added two additional equalToXml overloads that accept the ignoreOrderOfSameNode parameter: where we don't use placeholder delimiters

  public static EqualToXmlPattern equalToXml(
      String value, boolean enablePlaceholders, boolean ignoreOrderOfSameNode) {
    return new EqualToXmlPattern(value, enablePlaceholders, ignoreOrderOfSameNode);
  }

and

  public static EqualToXmlPattern equalToXml(
      String value,
      boolean enablePlaceholders,
      String placeholderOpeningDelimiterRegex,
      String placeholderClosingDelimiterRegex,
      boolean ignoreOrderOfSameNode) {
    return new EqualToXmlPattern(
        value,
        enablePlaceholders,
        placeholderOpeningDelimiterRegex,
        placeholderClosingDelimiterRegex,
        null,
        ignoreOrderOfSameNode);
  }

where we do use placeholder delimiters.

Docs are also updated and PR raised to reflect changes:

References

Submitter checklist

tomakehurst commented 1 month ago

This is a breaking change as it stands. I think we'd need to introduce a parameter similar to the ignore order one in equalToJson to avoid this.

viktormelnychuk commented 4 weeks ago

@tomakehurst Hi! I changed the implementation per your recommendations. Added a parameter and tried to follow the equalToJson implementation

tomakehurst commented 2 weeks ago

Please can you run ./gradlew spotlessApply and push the results?

viktormelnychuk commented 2 weeks ago

Run it, sorry