java - 在 pactdsl 请求正文中使用正则表达式

标签 java integration-testing pact pact-jvm

我希望我的 Pact 服务器在使用 header Content-Type: application/x-www-form-urlencoded 进行 POST 调用时返回自定义响应。
然而,POST 调用的主体并不总是相同的,只有前缀保持不变。
例如,无论我使用 body
input_text=LOGSomeStuffHERE 调用它,还是使用 input_text=LOGAnoutherStuff
(如你看,input_text=LOG是常量部分) 这是我尝试过的:

.uponReceiving("POST cusom body")
.path("/path")
.method("POST")
.headers(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType())
.body("input_text=LOG*")
.willRespondWith()
.status(200)
...

PactDsl 是否支持请求部分的某种正文匹配?

最佳答案

您可以match using a Regex来验证你的 body 。 DSL 中的 body 本身是应该为交互返回的实际主体(带有虚假数据),而无需实际添加额外的匹配器。如果你想要 example of this, look at the test code for the jvm consumer .

就您而言,您会这样做:

.uponReceiving("POST cusom body")
.path("/path")
.method("POST")
.headers(HttpHeaders.CONTENT_TYPE, 
   ContentType.APPLICATION_FORM_URLENCODED.getMimeType())
.body(PactDslJsonRootValue.stringMatcher("^input_text\=LOG.*", 
   "input_text=LOG_something"))
.willRespondWith()
.status(200)

第一个参数是正则表达式的字符串表示形式,第二个参数是实际传递给交互的字符串,需要通过正则表达式测试。

关于java - 在 pactdsl 请求正文中使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49449233/

相关文章:

java - 使用 Pact 和 JUnit 测试 SSL 安全 API

json - 如何在协议(protocol)中验证字符串化的 json

java - JSF 页面模板 - 样式属性值的控制参数

java - 带有@Transactional 注释的多个事务管理器

java - 正则表达式 - 解析字符串

json - 模拟 Mvc HttpMediaTypeNotSupportedException : Content type 'application/json' not supported

pact - 我可以创建 PACT 以在不同的主机名上运行吗?

java - 将文本文件读入 Stack 并按相反顺序打印出元素

java - 如何使用 JUnit 与 Spring 和 Hibernate 来模拟事务的结束以隔离 LazyInitializationException?

session - Grails-UnsupportedOperationException-集成测试