Response body is always null in WireMock response transformer

3,762 views
Skip to first unread message

ed...@info.nl

unread,
May 11, 2015, 4:03:13 AM5/11/15
to wiremo...@googlegroups.com
Hi,

I am trying to use a WireMock response transformer because I want to anonymize certain WireMock stub data before serving it but in my response transformer the response body is always null and I do not quite understand why. Am I doing something wrong?

My response transformer Java class is very similar to the example of the documentation:

public class AnonymizeImisMembersTransformer extends ResponseTransformer {
 
private static final Logger LOG = LoggerFactory.getLogger(AnonymizeImisMembersTransformer.class);

 
/**
 * Name of this transformer.
 */

 
public static final String ANONYMIZE_IMIS_MEMBERS_TRANSFORMER_NAME = "anonymize-imis-members-transformer";


 
@Override
 
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files) {
 
ResponseDefinition transformedResponseDefinition = responseDefinition;

 LOG
.debug("## response definition body: {}", responseDefinition.getBody());

 
// if response body is empty simply return existing response definition
 
if (null != responseDefinition.getBody()) {
 transformedResponseDefinition
= ResponseDefinitionBuilder
 
.like(responseDefinition).but()
 
.withBody(responseDefinition.getBody().replace("some...@mail.com", "someoth...@mail.com"))
 
.build();
 
}
 
return transformedResponseDefinition;
 
}


 
@Override
 
public String name() {
 
return ANONYMIZE_IMIS_MEMBERS_TRANSFORMER_NAME;
 
}

 
@Override
 
public boolean applyGlobally() {
 
return true;
 
}
}


And I enable the transformer using the .extensions :

@Rule
public WireMockRule wireMockServer = new WireMockRule(wireMockConfig()
 
.port(WIREMOCK_SERVER_TEST_PORT)
 
.withRootDirectory(WIREMOCK_STUBS_FOLDER)
 
.extensions(AnonymizeImisMembersTransformer.class)
 
.recordRequestHeadersForMatching(Collections.<String>emptyList()));


The stubs are served just fine and by WireMock and I do not use GZIPed responses.

cheers, 

Edgar

Tom Akehurst

unread,
May 11, 2015, 5:23:20 PM5/11/15
to wiremo...@googlegroups.com, ed...@info.nl
I can't see anything wrong with that. Is the body of the original ResponseDefinition null, or just the one resulting from the transformation?


On Monday, 11 May 2015 09:03:13 UTC+1, ed...@info.nl wrote:
Hi,

I am trying to use a WireMock response transformer because I want to anonymize certain WireMock stub data before serving it but in my response transformer the response body is always null and I do not quite understand why. Am I doing something wrong?

My response transformer Java class is very similar to the example of the documentation:

public class AnonymizeImisMembersTransformer extends ResponseTransformer {
 
private static final Logger LOG = LoggerFactory.getLogger(AnonymizeImisMembersTransformer.class);

 
/**
 * Name of this transformer.
 */

 
public static final String ANONYMIZE_IMIS_MEMBERS_TRANSFORMER_NAME = "anonymize-imis-members-transformer";


 
@Override
 
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files) {
 
ResponseDefinition transformedResponseDefinition = responseDefinition;

 LOG
.debug("## response definition body: {}", responseDefinition.getBody());

 
// if response body is empty simply return existing response definition
 
if (null != responseDefinition.getBody()) {
 transformedResponseDefinition
= ResponseDefinitionBuilder
 
.like(responseDefinition).but()

 
.withBody(responseDefinition.getBody().replace("someemail@mail.com", "someoth...@mail.com"))

 
.build();
 
}
 
return transformedResponseDefinition;
 
}


 
@Override
 
public String name() {
 
return ANONYMIZE_IMIS_MEMBERS_TRANSFORMER_NAME;
 
}

 
@Override
 
public boolean applyGlobally() {
 
return true;
 
}
}

ed...@info.nl

unread,
May 12, 2015, 7:38:55 AM5/12/15
to wiremo...@googlegroups.com, ed...@info.nl
Hi Tom,

Good question. How can I easily test the body of the original ResponseDefinition?

cheers

Edgar

On Monday, May 11, 2015 at 11:23:20 PM UTC+2, Tom Akehurst wrote:
I can't see anything wrong with that. Is the body of the original ResponseDefinition null, or just the one resulting from the transformation?

On Monday, 11 May 2015 09:03:13 UTC+1, ed...@info.nl wrote:
Hi,

I am trying to use a WireMock response transformer because I want to anonymize certain WireMock stub data before serving it but in my response transformer the response body is always null and I do not quite understand why. Am I doing something wrong?

My response transformer Java class is very similar to the example of the documentation:

public class AnonymizeImisMembersTransformer extends ResponseTransformer {
 
private static final Logger LOG = LoggerFactory.getLogger(AnonymizeImisMembersTransformer.class);

 
/**
 * Name of this transformer.
 */

 
public static final String ANONYMIZE_IMIS_MEMBERS_TRANSFORMER_NAME = "anonymize-imis-members-transformer";


 
@Override
 
public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource files) {
 
ResponseDefinition transformedResponseDefinition = responseDefinition;

 LOG
.debug("## response definition body: {}", responseDefinition.getBody());

 
// if response body is empty simply return existing response definition
 
if (null != responseDefinition.getBody()) {
 transformedResponseDefinition
= ResponseDefinitionBuilder
 
.like(responseDefinition).but()

 
.withBody(responseDefinition.getBody().replace("some...@mail.com", "someoth...@mail.com"))

 
.build();
 
}
 
return transformedResponseDefinition;
 
}


 
@Override
 
public String name() {
 
return ANONYMIZE_IMIS_MEMBERS_TRANSFORMER_NAME;
 
}

 
@Override
 
public boolean applyGlobally() {
 
return true;
 
}
}

Tom Akehurst

unread,
May 12, 2015, 9:53:40 AM5/12/15
to wiremo...@googlegroups.com, ed...@info.nl
Can you set a breakpoint inside your transformer class?
Message has been deleted

ed...@info.nl

unread,
May 12, 2015, 11:24:03 AM5/12/15
to wiremo...@googlegroups.com, ed...@info.nl
Sorry, yes of course..

The body of the original ResponseDefinition is also null. See:

Tom Akehurst

unread,
May 13, 2015, 3:34:04 AM5/13/15
to wiremo...@googlegroups.com, ed...@info.nl
Can you post the stub mapping that you're hitting?

Is it using a body file path?

ed...@info.nl

unread,
May 13, 2015, 11:29:51 AM5/13/15
to wiremo...@googlegroups.com, ed...@info.nl
Hi Tom,

Thanks very much for helping out!

We are using the WireMockServer#usingFilesUnderClasspath to configure our stub files and we are using the default stub mappings.

Please find the two related Java files from our project attached. This is part of a Magnolia CMS module but I think you can see how we set up WireMock. It is quite simple at this stage.

Our project layout looks like this:

WireMockStubServerConfig.java
WireMockModuleConfig.java
AnonymizeImisMembersTransformer.java

Tom Akehurst

unread,
May 13, 2015, 12:54:49 PM5/13/15
to wiremo...@googlegroups.com, ed...@info.nl
Right OK, so you've got your response bodies under __files.

You're probably seeing null bodies, because ResponseDefinition doesn't load the body from the file source (internally that's done by the ResponseRenderer).

You can fetch the body from the file in your transformer by doing something like this in your transformer:
fileSource.getBinaryFileNamed(ResponseDefinition.getBodyFileName())

ed...@info.nl

unread,
May 13, 2015, 1:54:42 PM5/13/15
to wiremo...@googlegroups.com, ed...@info.nl
thanks a lot Tom! I will let you know how I go.

What I really would like to do is to perform the transformation of the body response on the go while I am recording the WireMock stubs (before the stub files are written). Do you know if that is possible?

cheers

Tom Akehurst

unread,
May 15, 2015, 12:27:54 PM5/15/15
to wiremo...@googlegroups.com, ed...@info.nl
Probably your best bet there is to write your own version of the recorder, which is just a RequestListener. Although this is a bit messy, it's probably easiest to copy the existing one: StubMappingJsonRecorder.

ed...@info.nl

unread,
May 22, 2015, 2:54:09 PM5/22/15
to wiremo...@googlegroups.com, ed...@info.nl
thanks for your help Tom! I had a look at what you suggested and did get it working (in the StubMappingJsonRecorder#writeToMappingAndBodyFile) but only after I forked my own version of the wire mock Github project because I did not see a hook in WireMock to plug in my own recorder class since it is pretty wired up to the core of WireMock (it needs the WireMockServer class in the constructor for example). But in the end I decided it is far easier for this use case to simply use standard Java code to loop through the recorded stub files and perform the anonymization (search & replace stuff basically) there. This works fine. :-)

Still for more fine-tuning during the WireMock stub recording process it would be nice to be able to hook in more easily. Not so sure how difficult this would be to build in to be honest.

cheers
Reply all
Reply to author
Forward
0 new messages