Hi I have recently started working with wiremock....
# help
a
Hi I have recently started working with wiremock. I have managed to create a docker image with the respective configuration to later set up a pod and work in proxying mode. I would like to be able to configure proxying as follows: 50% of requests that meet "urlPathPattern" https are redirected to “proxyBaseUrl” and the remaining 50% of http requests return a 500 response code. I have been recommended to use the extension "ResponseTransformerV2" to get it. Could someone tell me how to generate a custom docker image with this extension and how to create this type of mock? Where can I find documentation or a case similar to mine? Thanks in advance
l
Hi, I think there has been a little miss-understanding here.
ResponseTransformerV2
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 for
a
Thanks for the clarification. I have created the following dockercompose file to generate my custom docker image.
# 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 you
Hi, "I've managed to create and start my customized Docker image. However, I can't seem to return different HTTP response codes (I always return 200). This is my file where I define my transformer:"
Copy code
import 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
Copy code
{
  "mappings": [
    {
      "request": {
        "urlPathPattern": "/third-party/employee/employee-organizational-unit/.*"
      },
      "response": {
        "proxyBaseUrl": "<https://testing.pre.testing.com>",
        "transformers": [
          "custom-response-transformer"
        ]
      }
    }
  ]
}
Hello everyone, somebody could help me? I have gone through many posts, searched the internet and still have not found the solution to cover my use case. Thanks in advance
l
Not sure what the problem is off the top of my head, I would need to investigate further. One thing I notice is that you are not implementing the correct interface. I think
ResponseTransformer
is deprecated. I think you need to implement
ResponseTransformerV2
. Not sure if that will solve your problem but it would be a good first step
I have just put together a simple transformer inside one of the tests in the WireMock codebase and it all works as expected ;
Copy code
public 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";
    }
  }
Copy code
@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?
It also would be good to see how you have packaged up your extension as that could impact how WireMock loads it
a
Hello Lee, Thanks for your help. I have tried to compile the class from what you have provided me and I am not able to generate the .class file. The version of java I am using is: java --version java 17.0.9 2023-10-17 LTS Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201) Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)
l
The static was there because I added it to a test in the wiremock codebase. I don't think you need it to be static
One other thing - we have a template for wiremock extensions here which might help putting this thing together - https://github.com/wiremock/wiremock-extension-template
a
Thank you very much @Lee Turner I will try to create my extension from this template. Thank you very much for your help
l
Sure thing. No problem at all. I don't know if it will solve your use case but WireMock Cloud has chaos features on the enterprise plans. Might be worth trying it out to see if it gives you what you want. When you sign up you get a trial of the enterprise plan. Something to think about maybe - https://www.wiremock.io/