6

I am trying to use Wiremocks record and playback feature to record a number of requests. The service I am calling returns different values depending on the headers that are sent in the request so I need the recording to capture the headers. I am using rest-assured to make the api call. Below is the code

WireMockServer proxyingService = new WireMockServer(
            wireMockConfig()
            .dynamicPort()
            .preserveHostHeader(true)
            .withRootDirectory(fileRoot.getAbsolutePath()));        

    FileSource fileSource = new SingleRootFileSource("wiremock");       
    FileSource filesFileSource = fileSource.child(FILES_ROOT);
    FileSource mappingsFileSource = fileSource.child(MAPPINGS_ROOT);


    proxyingService.enableRecordMappings(mappingsFileSource, filesFileSource);

    proxyingService.start();

    proxyingService.startRecording(recordSpec()
            .forTarget("https://dev.target.mycompany.com:447/")              
              .captureHeader("ConsumerID")
              .captureHeader("Accept")
              .captureHeader("Content-Type")
              .extractBinaryBodiesOver(10240)
              .extractTextBodiesOver(2048)
              .makeStubsPersistent(false)
              .ignoreRepeatRequests()                                 
              .matchRequestBodyWithEqualToJson(false, true));

requestSpec = new RequestSpecBuilder().setBaseUri("http://localhost:" + proxyingService.port() + "/")
            .addFilter(new ResponseLoggingFilter())// log request and response for better debugging. You can also
            .addFilter(new RequestLoggingFilter()) // only log if a requests fails.
            .build();

    request = given()
            .spec(requestSpec)
            .header("ConsumerID", "ABC")
            .header("ContextID", "DEF")
            .header("Content-Type", "text/xml; charset=ISO-8859-1")
            .contentType("text/xml").body(inputFile);

    response = request.post("myapi/endpoint");

    proxyingService.stopRecording();
    proxyingService.saveMappings();
    proxyingService.stop();

When I look in the mappings file though, I do not see any of the request headers

 {
  "id" : "e5f1d765-ed1c-30ba-ab74-997820a7d9b8",
  "request" : {
    "url" : "/myapi/endpoint",
    "method" : "POST",
    "bodyPatterns" : [ {
      "equalToXml" : "<?xml version=\"1.0\" encoding=\"UTF-8\"> "
    } ]
  },
  "response" : {
    "status" : 201,
    "bodyFileName" : "body-ccm-compiler-compile-h2LAO.txt",
    "headers" : {
      "X-Backside-Transport" : "OK OK,OK OK",
      "Connection" : "Keep-Alive",      
      "Pragma" : "no-cache",
      "Expires" : "0",
      "Strict-Transport-Security" : "max-age=31536000 ; includeSubDomains",      
      "Content-Type" : "text/plain",
      "Date" : "Fri, 08 Jun 2018 13:00:28 GMT",      
    }
  },
  "uuid" : "e5f1d765-ed1c-30ba-ab74-997820a7d9b8"
}

What am I doing wrong?

cachius
  • 1,743
  • 1
  • 8
  • 21
user889829
  • 388
  • 2
  • 7
  • 20
  • Have you confirmed that these headers are part of the data stream to WireMock? Tools like WireShark or SMSniff (Nirsoft) can help with this. – A. Kootstra Jun 13 '18 at 11:28
  • It looks like you've basically copied the example in the WireMock doc ("[Customising Your Recordings](http://wiremock.org/docs/record-playback/)") so it _should_ work. Are you sure that it's actually matching your request and you're not just seeing the results of a previous test? I ask because the fact that listening for a different protocol than you're connecting with looks suspicious to me. ("*https:*//dev.target.mycompany.com:447/" vs. "*http:*//localhost:" + proxyingService.port()) – DavidW Jun 15 '18 at 19:47

0 Answers0