spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

Fixing undeterministic behavior from rewriteRequestParameterWithEncodedRemainParameters() test

Open rjuare8 opened this issue 1 month ago • 1 comments

Deflake rewriteEncodedRequestParameter by relaxing URI ordering assertion

While running the Spring Cloud Gateway MVC tests with NonDex, I observed non-deterministic failures in BeforeFilterFunctionsTests.rewriteEncodedRequestParameter().

The original test asserted that the entire URI string, including query parameters and their ordering, exactly matched a hard-coded value:

assertThat(result.uri().toString())
    .hasToString("http://localhost/path?quux=corge%2B&baz=qux&foo%5B%5D=replacement%5B%5D");

However, the order of query parameters is not guaranteed. NonDex randomizes iteration order to surface this kind of order-dependence. Under some executions, the URI contained the same parameters and encodings, but in a different order, causing the hasToString assertion to fail even though the behavior was correct.

NonDex classifies this as an ID (implementation-dependent) flaky test: the failure comes from the test relying on implementation-specific details (a particular parameter ordering) that are not part of the API contract.

This PR updates the test to verify that the URI contains the expected path and query fragments independently of their order:

assertThat(result.uri().toString())
    .contains(
        "http://localhost/path?",
        "quux=corge%2B",
        "baz=qux",
        "foo%5B%5D=replacement%5B%5D"
    );

Summary of changes spring-cloud-gateway-server-webmvc/src/test/java/org/springframework/cloud/gateway/server/mvc/filter/BeforeFilterFunctionsTests.java:

Before:

assertThat(result.uri().toString())
    .hasToString("http://localhost/path?quux=corge%2B&baz=qux&foo%5B%5D=replacement%5B%5D");

After:

assertThat(result.uri().toString())
    .contains(
        "http://localhost/path?",
        "quux=corge%2B",
        "baz=qux",
        "foo%5B%5D=replacement%5B%5D"
    );

Motivation With NonDex enabled, rewriteEncodedRequestParameter would sometimes fail solely because the query parameter ordering in the resulting URI differed from the hard-coded string, despite all parameters being present and correctly encoded.

rjuare8 avatar Nov 30 '25 00:11 rjuare8

Good

raccoonback avatar Nov 30 '25 07:11 raccoonback