如何在线上创建多个变形金刚?我在文档中看到你可以在一个存根上调用两个响应变换器,但是我不确定如何创建第二个响应变换器以及给它命名?
所以这是我创造的第二个变压器
public class test2 extends ResponseDefinitionTransformer{
String message;
boolean value;
@Override
public ResponseDefinition transform(Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {
message = rqst.getBodyAsString();
value = validateXMLSchema("xxx", message);
System.out.println("SECOND TRANSFORMER EXECUTED");
System.out.println("THIS IS THE CONDITION OF THE VALUE: " + value );
if(SchemaMatches){
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(200)
.withBody("XSD SCHEMA MATCHES")
.build();
}else{
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(404)
.withBody("MISMATCH")
.build();
}
}
@Override
public boolean applyGlobally() {
return false;
}
@Override
public String getName() {
return "tests";
}
}
这是我创建的第一个变换器:
public class Stub extends ResponseDefinitionTransformer {
@Override
public ResponseDefinition transform(com.github.tomakehurst.wiremock.http.Request rqst, ResponseDefinition rd, FileSource fs, Parameters prmtrs) {
message = rqst.getBodyAsString();
if(MandatoryFieldsExist){
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(200)
.withBody("MANDATORY FIELDS PRESENT ")
.build();
}
else{
return new ResponseDefinitionBuilder()
.withHeader("Content-Type", "application/xml")
.withStatus(404)
.withBody("MISMATCH")
.build();
}
}
@Override
public boolean applyGlobally() {
return false;
}
@Override
public String getName() {
return "example";
}
最后,这是调用变换器的主要方法中的代码:
WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().port(8080).extensions(Stub.class, test2.class));
WireMock wireMockClient = new WireMock();
wireMockServer.start();
stubFor(post(urlEqualTo("/user/test"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/xml")
.withBody("XML RECIEVED")
.withTransformers("example","tests")
)
);