最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Spring Cloud Contract: Escaping Issue with fromRequest() in Response Body - Stack Overflow

matteradmin7PV0评论

I am using Spring Cloud Contract for testing, and I have a Groovy contract test with the following response body:

message: ["method.page": "The sort fields [${fromRequest().query('sort')}] are not within the allowed values: [sortField]"]

When I use a hardcoded value wrong-sort instead of ${fromRequest().query('sort')}, the generated test has a proper assertion like this:

assertThatJson(parsedJson).field("['message']").field("['method.page']")
    .isEqualTo("The sort fields [wrong-sort] are not within the allowed values: [sortField]");

However, when using ${fromRequest().query('sort')}, the generated assertion looks like this:

assertThatJson(parsedJson).field("['message']").field("['method.page']")
    .matches("The sort fields \\\\[wrong-sort\\\\] are not within the allowed values: \\\\[sortField\\\\]");

It appears that unnecessary escaping (\\) is being added to the brackets, which causes the test to fail.

I tried: message: ["method.page": $("The sort fields [${fromRequest().query('sort')}] are not within the allowed values: [sortField]"]) but the result was the same.

How can I configure the contract to ensure the generated test passes while dynamically including the value of ${fromRequest().query('sort')}?

I am using Spring Cloud Contract for testing, and I have a Groovy contract test with the following response body:

message: ["method.page": "The sort fields [${fromRequest().query('sort')}] are not within the allowed values: [sortField]"]

When I use a hardcoded value wrong-sort instead of ${fromRequest().query('sort')}, the generated test has a proper assertion like this:

assertThatJson(parsedJson).field("['message']").field("['method.page']")
    .isEqualTo("The sort fields [wrong-sort] are not within the allowed values: [sortField]");

However, when using ${fromRequest().query('sort')}, the generated assertion looks like this:

assertThatJson(parsedJson).field("['message']").field("['method.page']")
    .matches("The sort fields \\\\[wrong-sort\\\\] are not within the allowed values: \\\\[sortField\\\\]");

It appears that unnecessary escaping (\\) is being added to the brackets, which causes the test to fail.

I tried: message: ["method.page": $("The sort fields [${fromRequest().query('sort')}] are not within the allowed values: [sortField]"]) but the result was the same.

How can I configure the contract to ensure the generated test passes while dynamically including the value of ${fromRequest().query('sort')}?

Share Improve this question asked Nov 16, 2024 at 8:26 Ruslan MacariRuslan Macari 266 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I resolved this issue using bodyMatchers {}. The problem was with an incorrect jsonPath in my initial attempt. After correcting it, this worked:

response {
    message: [
        "method.page": "The sort fields [${fromRequest().query('sort')}] are not within the allowed values: [sortField]"
    ]
}

bodyMatchers {
    jsonPath('$.message["method.page"]', byRegex('The sort fields \\[.*\\] are not within the allowed values: \\[sortField\\]'))
}

This generated a valid test assertion:

assertThat(parsedJson.read("$.message[\"method.page\"]", String.class))
    .matches("The sort fields \\[.*\\] are not within the allowed values: \\[sortField\\]");
Post a comment

comment list (0)

  1. No comments so far