【问题标题】:Using regex in pactdsl request body在 pactdsl 请求正文中使用正则表达式
【发布时间】:2018-04-04 23:21:33
【问题描述】:

我希望我的协议服务器在使用 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 是否支持请求部分的某种正文匹配?

【问题讨论】:

  • 我也尝试过使用.body(new PactDslJsonBody()...),但这也不起作用,因为请求的Content-Type不是aplication/json

标签: java integration-testing pact pact-jvm


【解决方案1】:

您可以match using a Regex 来验证您的身体。 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)

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

【讨论】:

    猜你喜欢
    • 2013-08-03
    • 2012-11-01
    • 1970-01-01
    • 2020-06-04
    • 2020-09-14
    • 1970-01-01
    • 2017-01-18
    • 2022-06-16
    相关资源
    最近更新 更多