2

I am trying to write an extension where in I can store the request and response in the database. I extended PostActionServe, now there are 2 problems

  1. After starting the mock server, I submitted the post request to http://localhost:8089/__admin/mappings but neither doAction or doGlobalAction get invoked.

  2. Now if i hit the end point /some/thing then only doGlobal Action gets invoked.

I was hoping that the moment i submit the request to mappings API it should invoke this extension and i can store the request and response in DB.

Below is the code

import com.github.tomakehurst.wiremock.WireMockServer;

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

public class Main {

    public static void main(String... args) {
        WireMockServer wireMockServer = new WireMockServer(wireMockConfig()
                .extensions(new RequestRecorder()).port(8089)); //No-args constructor will start on port 8080, no HTTPS
        wireMockServer.start();
    }
}


import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.PostServeAction;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;

public class RequestRecorder extends PostServeAction {
    public static final String EXTENSION_NAME = "db-request-recorder";

    @Override
    public void doAction(final ServeEvent serveEvent, final Admin admin, final Parameters parameters) {

        System.out.println(serveEvent.getRequest());
        System.out.println(serveEvent.getResponseDefinition());
    }

    @Override
    public void doGlobalAction(ServeEvent serveEvent, Admin admin) {
        System.out.println(serveEvent.getRequest());
        System.out.println(serveEvent.getResponseDefinition());
    }
    @Override
    public String getName() {
        return EXTENSION_NAME;
    }
}

class MainTest {

    public static void main(String... aergs) {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        try {
            HttpPost request = new HttpPost("http://localhost:8089/__admin/mappings");
            StringEntity params = new StringEntity("{\n" +
                    "    \"request\": {\n" +
                    "        \"method\": \"GET\",\n" +
                    "        \"url\": \"/some/thing\"\n" +
                    "    },\n" +
                    "    \"response\": {\n" +
                    "        \"status\": 200,\n" +
                    "        \"body\": \"Hello world!\",\n" +
                    "        \"headers\": {\n" +
                    "            \"Content-Type\": \"text/plain\"\n" +
                    "        }\n" +
                    "    }\n" +
                    "}");
            request.addHeader("content-type", "application/x-www-form-urlencoded");
            request.setEntity(params);
            HttpResponse response = httpClient.execute(request);
        } catch (Exception ex) {
        } finally {
            // @Deprecated httpClient.getConnectionManager().shutdown();
        }
    }
}
Ambuj Jauhari
  • 1,187
  • 4
  • 26
  • 46

1 Answers1

0

I think you might be missing a wireMock stub call that will use the extention. Something like

wireMockServer.stubFor(WireMock.get(WireMock.urlMatching("/[a-z]*")).withPostServeAction(EXTENSION_NAME, parameters)
        .willReturn(responseObject));
Gonen I
  • 5,576
  • 1
  • 29
  • 60