简体   繁体   English

Spring Cloud Contract:从场景中的先前响应获取参数

[英]Spring cloud contract: get params from previous response in scenario

I wrote the scenario in spring cloud contract. 我在Spring Cloud Contract中编写了该方案。 On first request my app return token in response and second request should contain this token. 在第一个请求上,我的应用程序作为响应返回令牌,第二个请求应包含此令牌。 Is there any possibility to pass a value from first response to second request? 是否有可能将值从第一个响应传递到第二个请求?

Now I use in test another implementation of token generator, which returns predefined tokens, but I want to remove it and use real token generator. 现在我在测试中使用令牌生成器的另一种实现,该实现返回预定义的令牌,但是我想删除它并使用真实的令牌生成器。

Example of scenario: 方案示例:

1_getToken.groovy 1_getToken.groovy

org.springframework.cloud.contract.spec.Contract.make {
    description = 'receive token'
    request {
        method 'POST'
        url '/getToken'
        body("""
            {
            }
         """)
        headers {
            header('Content-Type', 'application/json')
        }
    }
    response {
        status 200
        body("""
            {
                "token":"123"
            }
         """)
    }
}

2_addSmth.groovy 2_addSmth.groovy

org.springframework.cloud.contract.spec.Contract.make {
    description = 'add somethingh'
    request {
        method 'POST'
        url '/addSmth'
        body("""
            {
                "token":"123",
                "value":"something"
            }
         """)
        headers {
            header('Content-Type', 'application/json')
        }
    }
    response {
        status 200
        body("""
            {
            }
         """)
    }
}

ie I want pass parameter "token" from first response to second request. 即我想要从第一个响应到第二个请求传递参数“令牌”。 Is it possible? 可能吗?

I don't think that it's possible at this point. 我认为目前尚不可能。 You should hard code it. 您应该对其进行硬编码。 If you need very concrete values then most likely your contract tests are too complicated. 如果您需要非常具体的价值,那么您的合同测试很可能过于复杂。 Or even most likely they are no longer contract tests but you try to use contracts to verify the business behaviour 甚至最有可能的是,它们不再是合同测试,而是您尝试使用合同来验证业务行为

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

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