简体   繁体   English

我的 PACT 测试有什么问题以及如何解决?

[英]What is wrong with my PACT Test and how to fix it?

I am writing a Pact Consumer and Provider Test .我正在编写 Pact Consumer and Provider Test 。 Basically I have 3 problems at the moment.基本上我目前有3个问题。

 @Pact(provider = PROVIDER, consumer = CONSUMER)
    public RequestResponsePact getAddress(PactDslWithProvider builder) {
        PactDslJsonBody body = new PactDslJsonBody()
                .stringType("key1", "73501")
                .stringType("key2", "value");
        final Map<String,String> headers = new HashMap<>();
        headers.put("Authorization","Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1");
        return builder
                .given("Something.")
                .uponReceiving("Dto")
                .path("/amz/ags")
                .query("code=9999")
                .method("GET")
                .headers(headers)
                .willRespondWith()
                .status(200)
                .body(body)
                .toPact();
    }

1) If I add a header in my Consumer Test I get a 500 status Error back 1)如果我在消费者测试中添加标题,我会收到 500 状态错误

2) As you just saw in the Consumer I tried to add Authorisation Header , but in the Provider it will be expired, so I have to find a way to hack something and provide a valid token. 2)正如您刚刚在 Consumer 中看到的那样,我尝试添加 Authorization Header ,但在 Provider 中它将过期,所以我必须找到一种方法来破解某些东西并提供一个有效的令牌。

3) In the provider REST Api other services are getting called but it seems I have to mock them at least whenever they are called an Exception gets thrown. 3)在提供者 REST Api 中,其他服务被调用,但似乎我必须至少在它们被调用时模拟它们,抛出异常。 Honestly I also don't know how to do this.老实说,我也不知道该怎么做。 In which method I need to do it.我需要采用哪种方法。 Why at all do I have to mock them because the external Apis are running.为什么我必须模拟它们,因为外部 API 正在运行。

for 2) I found a solution 2)我找到了解决方案

@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest httpRequest) {
    httpRequest.addHeader("Authorization", "Bearer " + "eyJ0eXAiOiJKV1Qi");
    context.verifyInteraction();
}

But now I get in context.verifyInteraction() an Exception.但是现在我在 context.verifyInteraction() 中得到了一个异常。 Very strange.很奇怪。

I can't answer the JVM specific question, however.但是,我无法回答 JVM 特定的问题。

  1. If I add a header in my Consumer Test I get a 500 status Error back如果我在消费者测试中添加标题,则会返回 500 状态错误

Usually, this means that you're told Pact you are going to do something but did not do it.通常,这意味着您被告知 Pact 您将要做某事但没有做。 In this case, my guess is the bearer token didn't match or it didn't receive the correct header.在这种情况下,我的猜测是不记名令牌不匹配,或者它没有收到正确的标头。 There should be logs or a junit report with the details.应该有包含详细信息的日志或 junit 报告。

  1. As you just saw in the Consumer I tried to add Authorisation Header , but in the Provider it will be expired, so I have to find a way to hack something and provide a valid token.正如您刚刚在 Consumer 中看到的那样,我尝试添加 Authorization Header ,但在 Provider 中它将过期,所以我必须找到一种方法来破解某些东西并提供一个有效的令牌。

For dealing with authentication/authorization, you may want to read the strategies here: https://docs.pact.io/provider/handling_auth为了处理身份验证/授权,您可能需要在此处阅读策略: https ://docs.pact.io/provider/handling_auth

  1. In the provider REST Api other services are getting called but it seems I have to mock them at least whenever they are called an Exception gets thrown.在提供者 REST Api 中,其他服务被调用,但似乎我必须至少在它们被调用时模拟它们,抛出异常。 Honestly I also don't know how to do this.老实说,我也不知道该怎么做。 In which method I need to do it.我需要采用哪种方法。 Why at all do I have to mock them because the external Apis are running.为什么我必须模拟它们,因为外部 API 正在运行。

Pact is intended to be closer to a unit test, running external services during Pact tests is not recommended, because it makes tests less deterministic. Pact 旨在更接近于单元测试,不建议在 Pact 测试期间运行外部服务,因为它会降低测试的确定性。 See also this section which discusses the provider test coverage: https://docs.pact.io/5-minute-getting-started-guide#scope-of-a-provider-pact-test .另请参阅讨论提供程序测试覆盖率的本节: https ://docs.pact.io/5-minute-getting-started-guide#scope-of-a-provider-pact-test。

On the question of how to handle token expiry, I did like this.关于如何处理令牌到期的问题,我确实喜欢这个。

The given:给定的:

  • JWT token contains an expire_at claim JWT 令牌包含expire_at声明

I wrote a small library to share within all the projects where I have a class like JwtTestHelper which generates any JWT with token started_at date as a constant date and expire_at is started_at plus like 100 years.我编写了一个小型库,以便在所有项目中共享,其中我有一个像JwtTestHelper这样的类,它生成任何 JWT,其标记为started_at日期作为常量日期expire_atstarted_at加上100 年。

It's important here to make it generate the same token for the same input parameters from run-to-run or the header will be different which will cause a new pact to be considered by the broker.重要的是让它在每次运行时为相同的输入参数生成相同的令牌,否则标头将不同,这将导致代理考虑新的协议 This dramatically changes the approach - you basically need to verify every new pact each time.这极大地改变了方法 - 您基本上每次都需要验证每个新协议

On the question of handling exceptions.关于处理异常的问题。 Do not cheat like this不要这样作弊

httpRequest.addHeader("Authorization", "Bearer " + "eyJ0eXAiOiJKV1Qi");

Give your client class code a chance to pass the values naturally, pass it somehow or mock using the same JwtTestHelper让您的客户端类代码有机会自然地传递值,以某种方式传递它或使用相同的JwtTestHelper进行模拟

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

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