admin管理员组文章数量:1026989
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')}
?
1 Answer
Reset to default 0I 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\\]");
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')}
?
1 Answer
Reset to default 0I 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\\]");
本文标签: Spring Cloud Contract Escaping Issue with fromRequest() in Response BodyStack Overflow
版权声明:本文标题:Spring Cloud Contract: Escaping Issue with fromRequest() in Response Body - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662982a2162002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论