此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 spring-cloud-contract 4.2.0! |
WireMock 定制
在本节中,我们将展示如何自定义使用 WireMock 的方式。
注册您自己的 WireMock 扩展
WireMock 允许您注册自定义扩展。默认情况下,Spring Cloud Contract 会注册
转换器,它允许您从响应中引用请求。如果您想
提供您自己的扩展,则可以注册org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions
接口。
由于我们使用spring.factories
extension 方法,您可以创建类似于
下面在META-INF/spring.factories
文件:
org.springframework.cloud.contract.verifier.dsl.wiremock.WireMockExtensions=\
org.springframework.cloud.contract.stubrunner.provider.wiremock.TestWireMockExtensions
org.springframework.cloud.contract.spec.ContractConverter=\
org.springframework.cloud.contract.stubrunner.TestCustomYamlContractConverter
以下示例显示了自定义扩展:
import com.github.tomakehurst.wiremock.extension.Extension
/**
* Extension that registers the default transformer and the custom one
*/
class TestWireMockExtensions implements WireMockExtensions {
@Override
List<Extension> extensions() {
return [
new DefaultResponseTransformer(),
new CustomExtension()
]
}
}
class CustomExtension implements Extension {
@Override
String getName() {
return "foo-transformer"
}
}
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
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:
@Bean
WireMockConfigurationCustomizer optionsCustomizer() {
return new WireMockConfigurationCustomizer() {
@Override
public void customize(WireMockConfiguration options) {
// perform your customization here
}
};
}
Customization of WireMock via Metadata
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
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"
}
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
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.
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.
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.