Alan B.Z
04/29/2024, 11:00 AMLee Turner
04/29/2024, 1:00 PMResponseTransformerV2
is not an extension but a class that you can implement in your extensions to achieve what you are looking to build.
The idea would be to build a response transformer extension to implement the proxy behaviour that you are looking forAlan B.Z
04/29/2024, 2:17 PM# Download latest image
FROM wiremock/wiremock:latest
# Copy extension
COPY wiremock-webhooks-extension-3.0.4.jar /home/wiremock/extensions
COPY Response.jar /home/wiremock/extensions
COPY wiremock-standalone-3.5.4.jar /home/wiremock/standalone
# Copy config test
COPY test_organigrama.json /home/wiremock/mappings
COPY wiremock-config-health.json /home/wiremock/mappings
# Specify the default port for WireMock
EXPOSE 8081
# Use ENTRYPOINT to start WireMock and the extension
ENTRYPOINT ["java", "-cp", "/home/wiremock/extensions/wiremock-webhooks-extension-3.0.4.jar/home/wiremock/extensions/Response.jar/home/wiremock/standalone/wiremock-standalone-3.5.4.jar", "wiremock.Run"]
# Use CMD to specify additional arguments
CMD ["--port", "8081"]
However, when starting the pod I am encountering the following error:
Error: Could not find or load main class wiremock.Run │
│ Caused by: java.lang.ClassNotFoundException: wiremock.Run
Does anyone have any clues on how to solve it?
Thank youAlan B.Z
04/30/2024, 10:12 AMimport com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import java.util.Random;
public class CustomResponseTransformer implements ResponseTransformer {
@Override
public Response transform(Request request, Response response, com.github.tomakehurst.wiremock.stubbing.ServeEvent serveEvent) {
int randomNumber = new Random().nextInt(100) + 1; // Genera un número aleatorio entre 1 y 100
int statusCode;
if (randomNumber <= 50) {
statusCode = 200; // 50% de las respuestas son correctas (código 200)
} else if (randomNumber <= 75) {
statusCode = 404; // 25% de las respuestas son errores 404
} else {
statusCode = 501; // 25% de las respuestas son errores 501
}
return Response.Builder.like(response)
.but().status(statusCode)
.build();
}
@Override
public String getName() {
return "custom-response-transformer";
}
}
My mapping file
{
"mappings": [
{
"request": {
"urlPathPattern": "/third-party/employee/employee-organizational-unit/.*"
},
"response": {
"proxyBaseUrl": "<https://testing.pre.testing.com>",
"transformers": [
"custom-response-transformer"
]
}
}
]
}
Alan B.Z
05/03/2024, 10:22 AMLee Turner
05/03/2024, 10:39 AMResponseTransformer
is deprecated. I think you need to implement ResponseTransformerV2
. Not sure if that will solve your problem but it would be a good first stepLee Turner
05/03/2024, 10:52 AMpublic static class StatusResponseTransformer implements ResponseTransformerV2 {
@Override
public Response transform(Response response, ServeEvent serveEvent) {
return Response.Builder.like(response).but().status(500).build();
}
@Override
public String getName() {
return "status-transformer";
}
}
@Test
public void transformsStatusResponse() {
startWithExtensions(StatusResponseTransformer.class);
wm.stubFor(
get(urlEqualTo("/response-transform")).willReturn(WireMock.ok()));
assertThat(client.get("/response-transform").statusCode(), is(500));
}
So, the transformer is working fine when implementing ResponseTransformerV2
as you would expect. First up, have you added any logging to see if your transformer is being called at all. That would at least allow us to narrow down where to start looking?Lee Turner
05/03/2024, 11:04 AMAlan B.Z
05/03/2024, 12:06 PMLee Turner
05/03/2024, 1:05 PMLee Turner
05/03/2024, 1:09 PMAlan B.Z
05/06/2024, 5:59 AMLee Turner
05/06/2024, 6:49 AM