WireMock Customization

在本节中,我们将展示如何自定义使用 WireMock的方式。

In this section, we show how to customize the way you work with WireMock.

Registering Your Own WireMock Extension

WireMock 允许您注册自定义扩展。默认情况下,Spring Cloud 合约会注册转换器,该转换器让您通过响应来引用请求。如果您想提供您自己的扩展,可以注册 org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions 接口的实现。由于我们使用 spring.factories 扩展方法,所以您可以在 META-INF/spring.factories 文件中创建类似于以下内容的条目:

WireMock lets you register custom extensions. By default, Spring Cloud Contract registers the transformer, which lets you reference a request from a response. If you want to provide your own extensions, you can register an implementation of the org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions interface. Since we use the spring.factories extension approach, you can create an entry similar to the following in the META-INF/spring.factories file:

Unresolved directive in wiremock-customization.adoc - include::{stubrunner_core_path}/src/test/resources/META-INF/spring.factories[]

以下示例展示了一个自定义扩展:

The following example shows a custom extension:

TestWireMockExtensions.groovy
Unresolved directive in wiremock-customization.adoc - include::{verifier_root_path}/src/test/groovy/org/springframework/cloud/contract/verifier/dsl/wiremock/TestWireMockExtensions.groovy[]

如果您希望仅对明确需要它的映射应用转换,请覆盖 applyGlobally() 方法并将其设置为 false

If you want the transformation to be applied only for a mapping that explicitly requires it, override the applyGlobally() method and set it to false .

Customization of WireMock Configuration

您可以注册 org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer 类型的 Bean,以定制 WireMock 配置(例如,为了添加自定义转换器)。以下示例展示了如何实现:

You can register a bean of type org.springframework.cloud.contract.wiremock.WireMockConfigurationCustomizer to customize the WireMock configuration (for example, to add custom transformers). The following example shows how to do so:

Unresolved directive in wiremock-customization.adoc - include::{wiremock_tests}/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockConfigurationCustomizerTests.java[]
// perform your customization here
Unresolved directive in wiremock-customization.adoc - include::{wiremock_tests}/src/test/java/org/springframework/cloud/contract/wiremock/AutoConfigureWireMockConfigurationCustomizerTests.java[]

Customization of WireMock via Metadata

通过版本 3.0.0,您可以在您的合约中设置 元数据 了。如果您设置具有等于 wiremock 的键的条目,并且此值会是有效的 WireMock 的 StubMapping JSON/映射或一个实际的 StubMapping 对象,Spring Cloud 合约会修补已生成的存根,并包含您的定制内容。我们来看一下以下示例

With version 3.0.0 you’re able to set metadata in your contracts. If you set an entry with key equal to wiremock and the value will be a valid WireMock’s StubMapping JSON / map or an actual StubMapping object, Spring Cloud Contract will patch the generated stub with part of your customization. Let’s look at the following example

Unresolved directive in wiremock-customization.adoc - include::{standalone_samples_path}/http-server/src/test/resources/contracts/yml/fraud/shouldReturnFraudStats.yml[]

metadata 部分,我们设置一个键为 wiremock 的条目,其值为一个 JSON StubMapping,其中设置了一个延迟的生成的 stub 中。此类代码能让我们获取以下合并的 WireMock JSON stub。

In the metadata section we’ve set an entry with key wiremock and its value is a JSON StubMapping that sets a delay in the generated stub. Such code allowed us to get the following merged WireMock JSON stub.

{
  "id" : "ebae49e2-a2a3-490c-a57f-ba28e26b81ea",
  "request" : {
    "url" : "/yamlfrauds",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "body" : "{\"count\":200}",
    "headers" : {
      "Content-Type" : "application/json"
    },
    "fixedDelayMilliseconds" : 2000,
    "transformers" : [ "response-template" ]
  },
  "uuid" : "ebae49e2-a2a3-490c-a57f-ba28e26b81ea"
}

当前实现允许仅处理 stub 部分(我们不更改生成的测试)。此外,不会更改响应的整个请求、主体和标头。

The current implementation allows to manipulate only the stub side (we don’t change the generated test). Also, what does not get changed are the whole request and body and headers of the response.

Customization of WireMock via Metadata and a Custom Processor

如果你想应用自定义的 WireMock StubMapping 后期处理,你可以在 META-INF/spring.factories 中的 org.springframework.cloud.contract.verifier.converter.StubProcessor 键下注册你自己的 stub 处理器实现。为了你的使用方便,我们创建了一个称为 org.springframework.cloud.contract.verifier.wiremock.WireMockStubPostProcessor 的接口,专门用于 WireMock。

If you want to apply a custom WireMock StubMapping post processing, you can under META-INF/spring.factories under the org.springframework.cloud.contract.verifier.converter.StubProcessor key register your own implementation of a stub processor. For your convenience we’ve created an interface called org.springframework.cloud.contract.verifier.wiremock.WireMockStubPostProcessor that is dedicated to WireMock.

你必须实现方法来告知 Spring Cloud Contract 后处理器是否适用于给定的契约以及后期处理应如何执行。

You’ll have to implement methods to inform Spring Cloud Contract whether the post processor is applicable for a given contract and how should the post processing look like.

在使用者端,使用 Stub Runner 时,请记住传递自定义 HttpServerStubConfigurer 实现(例如,扩展 WireMockHttpServerStubConfigurer 的实现),您可以在其中注册您选择的自定义扩展。如果不这样做,即使您在类路径上具有自定义 WireMock 扩展,WireMock 也不会注意到它,不会应用它,并且会打印一条警告消息,指出未找到给定的扩展。

On the consumer side, when using Stub Runner, remember to pass the custom HttpServerStubConfigurer implementation (e.g. the one that extends WireMockHttpServerStubConfigurer) where you’ll register a custom extension of your choosing. If you don’t do so, even you have a custom WireMock extension on the classpath, WireMock will not notice it, won’t apply it and will print out a warning statement that the given extension was not found.