简体   繁体   English

spring cloud contract dsl 指定路径参数

[英]spring cloud contract dsl specify path parameter

I am trying to create a contract for a GET request and I'd like to use a path parameter, that can be reused in the response as well.我正在尝试为 GET 请求创建合同,并且我想使用路径参数,该参数也可以在响应中重用。 Is this at all possible?这是可能吗? I can only find examples for POST, query parameters and body's.我只能找到 POST、查询参数和正文的示例。 So if I want to define a contract that requests an entity ie /books/12345-6688, I want to reuse the specified ID in the response.因此,如果我想定义一个请求实体的合同,即 /books/12345-6688,我想在响应中重用指定的 ID。

How do I create a contract for something like this?我如何为这样的事情创建合同?

Possible since Spring Cloud Contract 1.2.0-RC1 (fixed in this issue ).自 Spring Cloud Contract 1.2.0-RC1起可能(已在此问题中修复)。

response {
    status 200      
    body(
        path: fromRequest().path(),
        pathIndex: fromRequest().path(1)  // <-- here   
    )
}

See the docs .请参阅文档

Nope that's not possible due to https://github.com/tomakehurst/wiremock/issues/383 .不,这是不可能的,因为https://github.com/tomakehurst/wiremock/issues/383 Theoretically you could create your own transformer + override the way stubs are generated in Spring Cloud Contract.从理论上讲,您可以创建自己的转换器 + 覆盖 Spring Cloud Contract 中生成存根的方式。 That way the WireMock stubs would contain a reference to your new transformer (like presented in the WireMock docs - http://wiremock.org/docs/extending-wiremock/ ).这样 WireMock 存根将包含对新变压器的引用(如 WireMock 文档中所示 - http://wiremock.org/docs/extending-wiremock/ )。 But it sounds like a lot of work for sth that seems not really that necessary.但听起来好像有很多工作似乎并不是那么必要。 Why do you need to do it like this?为什么你需要这样做? On the consumer side you want to test the integration, right?在消费者方面,您想测试集成,对吗? So just hardcode some values in the contract instead of referencing them and just check if you can parse those values.因此,只需在合同中硬编码一些值,而不是引用它们,然后检查是否可以解析这些值。

UPDATE: If you just need to parametrize the request URL but don't want to reference it in the response you can use regular expressions like here - https://cloud.spring.io/spring-cloud-contract/single/spring-cloud-contract.html#_regular_expressions更新:如果您只需要参数化请求 URL 但不想在响应中引用它,您可以使用像这里这样的正则表达式 - https://cloud.spring.io/spring-cloud-contract/single/spring- cloud-contract.html#_regular_expressions

UPDATE2: Like @laffuste has mentioned, starting from RC1 you can reference a concrete path element UPDATE2:就像@lafuste 提到的,从 RC1 开始,你可以引用一个具体的路径元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM